CashBox Code

实验二源代码

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <stddef.h>

struct logData{
    long logId;  
    char logDate[11];  
    char logNote[20]; 
    double charge;   
    double total;  
};

long size = sizeof(struct logData);

int inputChoice(){
    printf("\nplease choice:\n");
    printf("1-Add a new cash LOG.\n");
    printf("2-List All Cash LOG.\n");
    printf("3-Query Last Cash LOG.\n");
    printf("4-Change Data.\n");
    printf("0-End program.\n");
    int choice;
    scanf("%d",&choice);
    return choice;
}

void addCash(FILE* fp){
    struct logData theLogData;
    printf("date(%%.%%.%%): ");
    scanf("%s",theLogData.logDate);
    printf("note: ");
    scanf("%s",theLogData.logNote);
    printf("charge: ");
    scanf("%lf",&theLogData.charge);

    int result = fseek(fp,-size,SEEK_END);

    if (result!=0){
        printf("create new file.\n");
        theLogData.logId = 1;
        theLogData.total = theLogData.charge;
    }
    else{
        struct logData lastLogData;
        fread(&lastLogData, size, 1, fp);
        theLogData.logId = lastLogData.logId+1;
        theLogData.total = lastLogData.total+theLogData.charge;
    }
    fwrite(&theLogData,size,1,fp);
    fclose(fp);
}

FILE* openfile(char* mode){
    FILE* fp = fopen("notebook.db",mode);
    if (fp==NULL){
        printf("open file failed!");
        exit(1);
    }
    return fp;
}

void listCash(FILE* fp){
    struct logData thisLogData;
    fread(&thisLogData,size,1,fp);   //fp不变,_r变化,新添加的_r小

    while (!feof(fp)){
        printf("id    data       note         charge    total     \n");
        printf("%-6ld%-11s%-13s%-10.2lf%-10.2lf\n",thisLogData.logId,thisLogData.logDate,thisLogData.logNote,
               thisLogData.charge,thisLogData.total);
        printf("-----------------------------------------------------\n");
        fread(&thisLogData,size,1,fp); 
    }
}

void listLastCash(FILE* fp){
    fseek(fp,-size,SEEK_END);
    struct logData lastLogData;
    fread(&lastLogData, size, 1, fp);
    printf("total: %lf\n", lastLogData.total);
}

void update(FILE* fp, int n){
    fseek(fp,(n-1)*size, SEEK_SET);
    struct logData thisLogData;
    fread(&thisLogData,size,1,fp);
    int choice;
    printf("change: 1.data 2.note 3.charge\n");
    scanf("%d", &choice);

    if (choice == 1) {
        long offset = sizeof(thisLogData.logId);
        scanf("%s", thisLogData.logDate);
        fseek(fp, -size+offset, SEEK_CUR);
        fwrite(&thisLogData.logDate, sizeof(thisLogData.logDate), 1, fp);

    } else if (choice == 2) {
        long offset = sizeof(thisLogData.logDate)+sizeof(thisLogData.logId);
        scanf("%s", thisLogData.logNote);
        fseek(fp, -size+offset, SEEK_CUR);
        fwrite(&thisLogData.logNote, sizeof(thisLogData.logNote), 1, fp);

    } else if (choice == 3) {
        long offset = offsetof(struct logData, charge);
        // long offset2 = sizeof(thisLogData.logId)+sizeof(thisLogData.logNote)+ sizeof(thisLogData.logDate);
        // printf("offset1:%ld,2:%ld", offset, offset2); //offset1:40, offset2:39
        double oldCharge = thisLogData.charge;
        scanf("%lf", &thisLogData.charge);
        double diff = oldCharge-thisLogData.charge;

        if(diff!=0){
            fseek(fp, -size+offset, SEEK_CUR);
            thisLogData.total = thisLogData.total-diff;
            printf("that: %lf ",thisLogData.total);
            fwrite(&thisLogData.charge, sizeof(thisLogData.charge), 1, fp);
            fwrite(&thisLogData.total, sizeof(thisLogData.total), 1, fp);

            rewind(fp);

            fseek(fp, n*size, SEEK_SET);
            struct logData nextLogData;
            fread(&nextLogData,size,1,fp);

            while (!feof(fp)){
                nextLogData.total = thisLogData.total+nextLogData.charge;
                fseek(fp, -size, SEEK_CUR);
                printf("saad:%lf",nextLogData.total);
                fwrite(&nextLogData, size, 1, fp);
                thisLogData = nextLogData;
                fread(&nextLogData,size,1,fp);
            }

        }
        else{
            fclose(fp);
            return;
        }
    }
    fclose(fp);
}

int main() {
    int choice;
    FILE* fp;
    while ((choice = inputChoice())!=0){
        switch (choice){
            case 1:
                fp = openfile("ab+");
                addCash(fp);
                printf("successfully!\n");
                break;
            case 2:
                fp = openfile("rb");
                listCash(fp);
                printf("\n=====END=====");
                break;
            case 3:
                fp = openfile("rb");
                listLastCash(fp);
                break;
            case 4:
                fp = openfile("rb+");
                int n;
                printf("input the id which you want to change: ");
                scanf("%d", &n);
                update(fp,n);
                break;
        }
    }
    return 0;
}

PPT

最后更新于

这有帮助吗?