2019-04-09

作者: cjs2019 | 来源:发表于2019-04-09 13:57 被阅读2次

    //先随手写一个吧,请喷

    整体自评:

    函数封装还说得过去,但是没有做到真正的“高内聚低耦合”;
    如果是团队合作的话,可能还不够安全……
    总之就是“还有很大的提升空间”……

    coding效率……

    T1: <1h
    T2: 1h 左右
    T3:昨天晚上 2h,今天4h……
    (但是……仍然……可能……有bug……😂😂😂
    总之就是“还有很大的提升空间”……

    T1

    // 也没有几行代码,就不封装成函数了
    // 挺简单的,就不写详细的注释了
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <math.h>
    #include <time.h>
    #include <unistd.h>
    
    void print(long long cnt_A_Z,long long cnt_a_z,long long cnt0_9,long long cnt_others,long long cnt_line,long long cnt_len_max,long long cnt_len_min,long long* cnt){
        printf("The result is:\nTask1:\ncapital:%lld\n",cnt_A_Z);
        printf("lowercase:%lld\n",cnt_a_z);
        printf("digit:%lld\n",cnt0_9);
        printf("others:%lld\n",cnt_others);
        printf("\n");
        printf("Task2:\nline:%lld\n",cnt_line);
        printf("%lld characters in max line.\n",cnt_len_max);
        printf("%lld characters in min lien.\n",cnt_len_min);
        printf("\n");
        printf("Task3:\n");
        printf("CAPITAL:");
        for (int i = 'A'; i <= 'Z'; ++i) {
            char tem_ch=(char)i;
            if ((i-'A')%4==0){
                printf("\n%c:%lld",tem_ch,cnt[i]);
            }
            else{
                printf("\tc:%lld",tem_ch,cnt[i]);
            }
        }
        printf("\nLOWERCASE:");
        for (int i = 'a'; i <= 'z'; ++i) {
            char tem_ch=(char)i;
            if ((i-'a')%4==0){
                printf("\n%c:%lld",tem_ch,cnt[i]);
            }
            else{
                printf("\t%c:%lld",tem_ch,cnt[i]);
            }
        }
    }
    
    int main(){
        long long cnt_A_Z,cnt_a_z,cnt0_9,cnt_others,cnt[1025];
        memset(cnt,0, sizeof(cnt));
        long long cnt_line,cnt_len_max,cnt_len_min,len_of_current_line=0;
        cnt_A_Z=cnt_a_z=cnt0_9=cnt_others=cnt_line=cnt_len_max=cnt_len_min=0;
        FILE *fptr=NULL;
        char filename[200];
        scanf("%s",filename);
        fptr=fopen(filename,"r");
        char ch;
        while ((ch=fgetc(fptr))!=EOF){
            if (ch!='\n'){
                cnt[ch]++;
                len_of_current_line++;
                if (ch>='A' && ch<='Z'){
                    cnt_A_Z++;
                }
                else if (ch>='a' && ch<='z'){
                    cnt_a_z++;
                }
                else if (ch>='0' && ch<='9'){
                    cnt0_9++;
                }
                else{
                    cnt_others++;
                }
            }
            else{
                cnt_line++;
                if (len_of_current_line<cnt_len_min){
                    cnt_len_min=len_of_current_line;
                }
                else if (len_of_current_line>cnt_len_max){
                    cnt_len_max=len_of_current_line;
                }
                len_of_current_line=0;
            }
        }
        print(cnt_A_Z,cnt_a_z,cnt0_9,cnt_others,cnt_line,cnt_len_max,cnt_len_min,cnt);
    
        fclose(fptr);
    
        return 0;
    }
    

    T2

    // 直接处理高级要求:要能处理printf中的//或者/**/
    // 基本要求:假设//或者/**/不会出现在printf语句中。
    // 二者的唯一区别就是对printf的识别;
    // 有限状态自动机……
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <math.h>
    #include <time.h>
    #include <unistd.h>
    
    FILE** initialize_and_open_files(void){
        char file_in_name[200],file_out_name[200];
        scanf("%s",file_in_name);
        getchar();
        scanf("%s",file_out_name);
        FILE **fptr=NULL;
        fptr=(FILE**)malloc(sizeof(FILE*)*2);
        fptr[0]=fopen(file_in_name,"r");
        fptr[1]=fopen(file_out_name,"w");
        rewind(fptr[0]);
        return fptr;
    }
    int print_flag_func(int print_flag,char ch){
        //printf检测;
        char printf_string[20]={"printf(\""};
        if (ch==printf_string[0]&&print_flag==0){
            print_flag=1;
        }
        else if (ch==printf_string[print_flag]&&print_flag<=5){
            print_flag++;
        }
        else if (print_flag==6){
            print_flag=6;
        }
        else {
            print_flag=0;
        }
        return print_flag;
    }
    
    int main(){
        FILE **fptr=NULL;
        fptr=initialize_and_open_files();
        if (fptr==NULL){
            printf("Initialization and files opening Error!\n");
        }
        else{
            char ch=fgetc(fptr[0]);
            int flag=0,print_flag=0,special_flag=0;
            while (!feof(fptr[0])){
                print_flag=print_flag_func(print_flag,ch);
                if (print_flag>=1){
                    //如果 print_flag>=1,说明当前扫描位置在printf中
                    fputc(ch,fptr[1]);
                    ch=fgetc(fptr[0]);
                    continue;
                }
    
                if (flag==0){ //非注释状态
                    if (ch=='/'){
                        flag=1; //"扫到"第一个'/',"准备"进入注释状态;
                    }
                    else{
                        fputc(ch,fptr[1]);
                    }
                }
                else if (flag==1){
                    if (ch=='/'){
                        flag=2; //"//"型注释,跳过一行;
                    }
                    else if(ch=='*'){
                        flag=3; //"/*"型注释,跳过之后所有,直到遇到一个"*/";
                    }
                    else{
                        flag=0;
                        fputc(ch,fptr[1]);
                    }
                }
                else if (flag==2){
                    if (ch=='\n'){
                        //遇到了换行符,由于是单行注释,只需要删除注释,但是不能"顺便"删除哪一行的换行符,否则本行和下一行将会连在一起
                        fputc(ch,fptr[1]);
                        flag=0;//更新状态,回到q0状态;
                    }
                }
                else if (flag==3){
                    if (special_flag==0){
                        if (ch=='*'){
                            special_flag=1;//准备结束;
                        }
                    }
                    else if (special_flag==1){
                        if (ch=='/'){
                            special_flag=0;//宏观上,结束注释状态;
                            flag=0;
                        }
                    }
                }
                ch=fgetc(fptr[0]);
            }
        }
    
        fclose(fptr[0]);
        fclose(fptr[1]);
    
        return 0;
    }
    

    T3

    // 由于本题题干已经给出了解题的思路和注意事项,
    // 所以解法实际上是一目了然的,
    // 这里就不给出详细的注释了,
    // 希望代码可读性说的过去;
    // Because the method and the detailed steps to solve this problem have been given,
    // I will not give detailed notes.
    // I hope that my code is readable,
    // as I have solved this problem to the best of my ability.
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <math.h>
    #define Required_PATH "commodity.dat"
    char filename[200];
    
    struct record{
        int id=-1;
        char name[40]="";
        int num=-1;
        double price=-1;
    };
    
    void print_initialization_information(void);
    void error_print(int error_type);
    void func1_create_blank_file(FILE* fptr);
    void func2_input_record(FILE *fptr);
    void func3_update(FILE *fptr);
    void func4_delete_record(FILE *fptr);
    void func5(FILE *fptr);
    void func6(void);
    void func7_output_all_the_records(FILE *fptr);
    int func8_count_records(FILE *fptr,int *loc);
    void function_choose(FILE *fptr,int flag);
    
    int main(){
        int flag=0;
        printf("Because of the unknown difference of the path search among Mac OS X, Windows and Ubuntu,\n"
               "Please input the path of the project\n");
        scanf("%s",filename);//filename==the path of the project;
        strcat(filename,Required_PATH);
        while (flag!=6){
            print_initialization_information();
            FILE *fptr = NULL;
            scanf("%d",&flag);
            function_choose(fptr,flag);
        }
    
        return 0;
    }
    
    void print_initialization_information(void){
        printf("请输入您的选择:\n"
               "1--创建一个100条空记录的文件\n"
               "2--输入商品记录\n"
               "3--更新商品记录\n"
               "4--删除商品记录\n"
               "5--输出全部商品记录\n"
               "6--退出程序\n");
    }
    void error_print(int error_type){ //错误信息打印;
        if (error_type==1){
            printf("The file can't be opened!\n");
        }
        else if (error_type==2){
    
        }
    }
    void func1_create_blank_file(FILE* fptr){
        fptr=fopen(filename,"wb");
        if (fptr==NULL){
            error_print(1);
        }
        else{
            struct record blank_record;
            for (int i = 0; i < 100; ++i) {
                blank_record.id=i;
                fwrite(&blank_record, sizeof(record),1,fptr);
            }
        }
        printf("100条空记录创建完毕\n");
        fclose(fptr);
    }
    void func2_input_record(FILE *fptr){
        record tem_record;
        fptr=fopen(filename,"rb+");
        if (fptr==NULL){
            error_print(1);
        }
        else{
            printf("请输入商品信息[ID为-1代表输入结束]:\n商品ID:");
            scanf("%d",&tem_record.id);
            while (tem_record.id!=-1){
                fseek(fptr,sizeof(record)*tem_record.id,SEEK_SET);
                printf("商品名:");
                scanf("%s",tem_record.name);
                printf("数量:");
                scanf("%d",&tem_record.num);
                printf("价格:");
                scanf("%lf",&tem_record.price);
                fwrite(&tem_record, sizeof(record),1,fptr);
                printf("请输入商品信息[ID为-1代表输入结束]:\n商品ID:");
                scanf("%d",&tem_record.id);
            }
        }
        printf("商品信息输入结束\n");
        fclose(fptr);
        func7_output_all_the_records(fptr);
    }
    void func3_update(FILE *fptr){
        printf("请输入待更新商品ID[ID为-1代表结束更新]:\n");
        struct record tem_record;
        scanf("%d",&tem_record.id);
    
        fptr=fopen(filename,"rb+");
        if (fptr==NULL){
            error_print(1);
        }
        else {
            while (tem_record.id != -1) {
                fseek(fptr, sizeof(record) * tem_record.id, SEEK_SET);
                fread(&tem_record, sizeof(record), 1, fptr);
                if (fabs(tem_record.price + 1.000000) < 1e-5) {
                    printf("对不起,记录号为%d的商品不存在,无法更新\n",tem_record.id);
                }
                else {
                    printf("原商品信息如下:\n");
                    printf("记录号(商品ID)    商品名          数量    价格\n");
                    printf("%d%35s%10d%15lf\n", tem_record.id, tem_record.name, tem_record.num,tem_record.price);
                    printf("商品名:");
                    scanf("%s", tem_record.name);
                    printf("数量:");
                    scanf("%d", &tem_record.num);
                    printf("价格:");
                    scanf("%lf", &tem_record.price);
                    printf("更新后商品信息如下:\n");
                    printf("记录号(商品ID)    商品名          数量    价格\n");
                    printf("%d%35s%10d%15lf\n", tem_record.id, tem_record.name, tem_record.num,tem_record.price);
                    fseek(fptr, sizeof(record) * tem_record.id, SEEK_SET);
                    fwrite(&tem_record, sizeof(record), 1, fptr);
                }
                printf("请输入待更新商品ID[记录号为-1代表结束更新]:\n");
                scanf("%d", &tem_record.id);
            }
        }
        printf("更新工作结束");
        fclose(fptr);
    }
    void func4_delete_record(FILE *fptr){
        printf("请输入待删除商品ID[记录号为-1代表结束删除]:\n");
        fptr=fopen(filename,"rb+");
        if (fptr==NULL){
            error_print(1);
        }
        else{
            struct record tem_record;
            scanf("%d",&tem_record.id);
            while (tem_record.id!=-1){
                fseek(fptr, sizeof(record)*tem_record.id, SEEK_SET);
                fread(&tem_record, sizeof(record),1,fptr);
                if (tem_record.num==-1){
                    printf("对不起,记录号为%d的商品不存在,无法进行删除操作\n",tem_record.id);
                }
                else{
                    printf("该商品信息如下:");
                    printf("记录号(商品ID)    商品名          数量    价格\n");
                    printf("%d%35s%10d%15lf\n", tem_record.id, tem_record.name, tem_record.num,tem_record.price);
                    printf("是否确实删除(确认按Y,取消按N)?\n");
                    char confirm_flag='N';
                    getchar();
                    scanf("%c",&confirm_flag);
                    if (confirm_flag=='Y'){
                        strcpy(tem_record.name,"\0");
                        tem_record.num=-1;
                        tem_record.price=-1;
                        fseek(fptr, sizeof(record)*tem_record.id, SEEK_SET);
                        fwrite(&tem_record, sizeof(record), 1, fptr);
                        printf("记录号为%d的商品成功删除\n",tem_record.id);
                    }
                    else{
                        printf("您选择不删除记录号为%d的商品\n",tem_record.id);
                    }
                    func7_output_all_the_records(fptr);
                }
                printf("请输入待删除商品ID[记录号为-1代表结束删除]:\n");
                scanf("%d",&tem_record.id);
            }
        }
        printf("删除工作结束");
    }
    void func5(FILE *fptr){
        func7_output_all_the_records(fptr);
    }
    void func6(void){
        printf("程序运行结束,再见!\n");
    }
    void func7_output_all_the_records(FILE *fptr){
        int loc[100]={-1},cnt=0;
        if (fptr!=NULL) {
            fclose(fptr);
        }
        cnt=func8_count_records(fptr,loc);
        fptr=fopen(filename,"rb");
        if (fptr==NULL){
            error_print(1);
        }
        else{
            struct record tem_record;
            printf("商品信息如下:\n"
                   "记录号(商品ID)    商品名        数量  价格\n");
            for (int i = 0; i <= cnt-1; ++i) {
                fseek(fptr, sizeof(record)*loc[i], SEEK_SET);
                fread(&tem_record, sizeof(record),1,fptr);
                printf("%d%35s%10d%15lf\n",tem_record.id,tem_record.name,tem_record.num,tem_record.price);
            }
            fclose(fptr);
        }
    }
    int func8_count_records(FILE *fptr,int *loc){
        int cnt=0;
        fptr=fopen(filename,"rb");
        if (fptr==NULL){
            error_print(1);
        }
        else{
            struct record tem_record;
            for (int i = 0; i < 100; ++i) {
                fseek(fptr, sizeof(record)*i,SEEK_SET);
                fread(&tem_record, sizeof(record),1,fptr);
                if (fabs(tem_record.price+1.0000)<1e-5){
                    continue;
                }
                else{
                    loc[cnt]=i;
                    cnt++;
                }
            }
            fclose(fptr);
        }
        return cnt;
    }
    void function_choose(FILE *fptr,int flag){
        switch (flag){
            case 1: {
                func1_create_blank_file(fptr);
            }
                break;
            case 2:{
                func2_input_record(fptr);
            }
                break;
            case 3:{
                func3_update(fptr);
            }
                break;
            case 4:{
                func4_delete_record(fptr);
            }
                break;
            case 5:{
                func5(fptr);
            }
                break;
            case 6:{
                func6();
            }
                break;
            default:
                break;
        }
    }

    相关文章

      网友评论

        本文标题:2019-04-09

        本文链接:https://www.haomeiwen.com/subject/nsybiqtx.html