🧸🧸🧸🧸🧸
  • 🧸's Blog
  • CodeJam
    • Kickstart Round H 2018 A Big Buttons
    • Kickstart Round H 2018 B Mural
  • C++/C
    • CashBox Code
    • for迭代数组
    • 字符串操作
    • 在函数中,int与int&的区别
    • sizeof()
    • memset的用法
    • 传值&传引用&传指针
    • STL
  • 经典算法
    • n皇后问题
  • Java
    • servlet从网址传入参数中文乱码
  • SQL
    • 左外连接与右外连接的区别
  • API
    • DeepGTAV v2
    • VPilot
    • SantosNet
    • deepdrive
    • iceb.link API
  • Spring Boot
    • Entity实体
    • 是否加@service的区别
    • Entity内字段表中名字不能为system
由 GitBook 提供支持
在本页

这有帮助吗?

  1. C++/C

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

上一页Kickstart Round H 2018 B Mural下一页for迭代数组

最后更新于6年前

这有帮助吗?