分解C中的代码块

时间:2017-10-19 14:20:55

标签: c menu

我遇到了我的功能运行太多线路的情况。显然有重复的因素,我不知道如何分解它。我尝试将相同的代码放入另一个函数中,但它需要几乎相同数量的代码行。什么更好的方法来分解它?感谢。

int purchase_meal() {

    FILE *fptr;

    char meal_choice[5];
    char *item;
    float price, sumtax, tax;
    unsigned int quantity;
    float total=0, grand_total=0;

    while(strcmp(meal_choice, "-1") != 0) {

        printf("Please enter a meal order option and how many orders. Example: C0001 2 \n");
        scanf("%s %d", meal_choice, &quantity);

        if (meal_choice[0] == '-' && meal_choice[1] == '1') {
            print_receipt();
            order();
        } 
        else if (meal_choice[0] == 'C' && meal_choice[1] == '0' && meal_choice[2] == '0' && meal_choice[3] == '0') {
            fptr = fopen("receipt.txt", "a+"); //opens receipt txt file and appends
            if(fptr == NULL) //check whether the file is empty or not
            {
                printf("File cannot be found\n");

            } 
            else {
                switch (meal_choice[4]) {
                    case '1':    // assuming they chose set 1 with code C0001
                        price = combo1.price;
                        item = combo1.name;
                        tax = taxing(combo1.price, quantity);
                        total = (combo1.price * quantity) + tax;
                        grand_total += total;
                        sumtax += tax;
                        csum.combo_trans++;
                        sum.total = grand_total;
                        sum.tax = sumtax;

                        fprintf(fptr, "%-15d%-27s%-17.2f\n", quantity, combo1.name, combo1.price);
                        print_order(quantity, item, price, grand_total);

                        break;
                    case '2':
                        price = combo2.price;
                        item = combo2.name;
                        tax = taxing(combo2.price, quantity);
                        total = (combo2.price * quantity) + tax;
                        grand_total += total;
                        sumtax += tax;
                        csum.combo_trans++;
                        sum.total = grand_total;
                        sum.tax = sumtax;

                        fprintf(fptr, "%-15d%-27s%-17.2f\n", quantity, combo2.name, combo2.price);
                        print_order(quantity, item, price, grand_total);

                        break;
                    case '3':
                        price = combo3.price;
                        item = combo3.name;
                        tax = taxing(combo3.price, quantity);
                        total = (combo3.price * quantity) + tax;
                        grand_total += total;
                        sumtax += tax;
                        csum.combo_trans++;
                        sum.total = grand_total;
                        sum.tax = sumtax;

                        fprintf(fptr, "%-15d%-27s%-17.2f\n", quantity, combo3.name, combo3.price);
                        print_order(quantity, item, price, grand_total);


                        break;
                    case '4':
                        price = combo4.price;
                        item = combo4.name;
                        tax = taxing(combo4.price, quantity);
                        total = (combo4.price * quantity) + tax;
                        grand_total += total;
                        sumtax += tax;
                        csum.combo_trans++;
                        sum.total = grand_total;
                        sum.tax = sumtax;

                        fprintf(fptr, "%-15d%-27s%-17.2f\n", quantity, combo4.name, combo4.price);
                        print_order(quantity, item, price, grand_total);

                        break;
                    case '5':
                        price = combo5.price;
                        item = combo5.name;
                        tax = taxing(combo5.price, quantity);
                        total = (combo5.price * quantity) + tax;
                        grand_total += total;
                        sumtax += tax;
                        csum.combo_trans++;
                        sum.total = grand_total;
                        sum.tax = sumtax;

                        fprintf(fptr, "%-15d%-27s%-17.2f\n", quantity, combo5.name, combo5.price);
                        print_order(quantity, item, price, grand_total);

                        break;
                    default:
                        puts("Invalid meal option!");

                }
                fclose(fptr); //close the file
            }
        } 
        else if (meal_choice[0] == 'A' && meal_choice[1] == '0' && meal_choice[2] == '0' && meal_choice[3] == '0') {
            fptr = fopen("receipt.txt", "a"); //opens txt file and READS only
            if(fptr == NULL) //check whether the file is empty or not
            {
                printf("File cannot be found\n");

            } else {
                switch (meal_choice[4]) {
                    case '1':  // assuming they chose set 1 with code C0001 
                        price = addon1.price;
                        item = addon1.name;
                        tax = taxing(addon1.price, quantity);
                        total = (addon1.price * quantity) + tax;
                        grand_total += total;
                        sumtax += tax;
                        asum.ala_trans++;
                        sum.total = grand_total;
                        sum.tax = sumtax;

                        fprintf(fptr, "%d\t%s\t\t\t%.2f\n", quantity, addon1.name, addon1.price);
                        print_order(quantity, item, price, grand_total);

                        break;
                    case '2':
                        price = addon2.price;
                        item = addon2.name;
                        tax = taxing(addon2.price, quantity);
                        total = (addon2.price * quantity) + tax;
                        grand_total += total;
                        sumtax += tax;
                        asum.ala_trans++;
                        sum.total = grand_total;
                        sum.tax = sumtax;

                        fprintf(fptr, "%d\t%s\t\t\t%.2f\n", quantity, addon2.name, addon2.price);
                        print_order(quantity, item, price, grand_total);

                        break;
                    case '3':
                        price = addon3.price;
                        item = addon3.name;
                        tax = taxing(addon3.price, quantity);
                        total = (addon3.price * quantity) + tax;
                        grand_total += total;
                        sumtax += tax;
                        asum.ala_trans++;
                        sum.total = grand_total;
                        sum.tax = sumtax;

                        fprintf(fptr, "%d\t%s\t\t\t%.2f\n", quantity, addon3.name, addon3.price);
                        print_order(quantity, item, price, grand_total);


                        break;
                    case '4':
                        price = addon4.price;
                        item = addon4.name;
                        tax = taxing(addon4.price, quantity);
                        total = (addon4.price * quantity) + tax;
                        grand_total += total;
                        sumtax += tax;
                        asum.ala_trans++;
                        sum.total = grand_total;
                        sum.tax = sumtax;

                        fprintf(fptr, "%d\t%s\t\t\t%.2f\n", quantity, addon4.name, addon4.price);
                        print_order(quantity, item, price, grand_total);

                        break;
                    case '5':
                        price = addon5.price;
                        item = addon5.name;
                        tax = taxing(addon5.price, quantity);
                        total = (addon5.price * quantity) + tax;
                        grand_total += total;
                        sumtax += tax;
                        asum.ala_trans++;
                        sum.total = grand_total;
                        sum.tax = sumtax;

                        fprintf(fptr, "%d\t%s\t\t\t%.2f\n", quantity, addon5.name, addon5.price);
                        print_order(quantity, item, price, grand_total);

                        break;
                    default:
                        puts("Invalid meal option!");

                }
                fclose(fptr); //close the file
            } //end else for file found
        } //end else for menu option A or C
    } //end of while loop

    return 0;
}

1 个答案:

答案 0 :(得分:0)

因此,通过将总和放在switch语句的末尾,这是我能够最好的重构。是的,验证适用于不接受无效的膳食代码,如C0001,C0002,C0003,C0004,C0005,A0001,A0002,A0003,A0004,A0005(如果您想知道什么是膳食代码),但是如果用户输入是浮点数,它将导致我仍然在工作的无限循环。我还将信息添加到退出循环,其中sentinel值为-1。感谢大家的反馈。

int purchase_meal() {

    FILE *fptr; //flie pointer for receipt text file
    char meal_choice[5]; //user input meal code
    char *item;
    float price;
    unsigned int quantity=0;    
    float total=0, grand_total=0;   

    while(strcmp(meal_choice, "-1") != 0) {     
        printf("Please enter a meal order option (-1 to exit). Example: C0001.\n");     
        scanf("%s", meal_choice);

        if (meal_choice[0] == '-' && meal_choice[1] == '1') {           
            print_receipt();                
            order();
        } 
        else if (strlen(meal_choice) == 5 && meal_choice[0] == 'C' && meal_choice[1] == '0' && meal_choice[2] == '0' && meal_choice[3] == '0'){
            printf("Enter quantity of order. \n");
            scanf("%d", &quantity);

            fptr = fopen("receipt.txt", "a+"); //opens receipt txt file and appends
            if(fptr == NULL) //check whether the file is empty or not
            {
                printf("File cannot be found\n");

            } 
            else {              
                switch (meal_choice[4]) {                                           
                    case '1':           // assuming they chose set 1 with code C0001                
                        price = combo1.price;               
                        item = combo1.name;                                         
                        total = combo1.price * quantity;
                        grand_total += total;                       
                        csum.combo_trans++;
                        fprintf(fptr, "%-15u%-27s%-17.2f\n", quantity, combo1.name, combo1.price);                  
                        print_order(quantity, item, price, grand_total);                
                        break;
                    case '2':
                        price = combo2.price;
                        item = combo2.name;                     
                        total = combo2.price * quantity;
                        grand_total += total;                       
                        csum.combo_trans++;                                                                             
                        fprintf(fptr, "%-15u%-27s%-17.2f\n", quantity, combo2.name, combo2.price);
                        print_order(quantity, item, price, grand_total);                            
                        break;
                    case '3':
                        price = combo3.price;
                        item = combo3.name;                     
                        total = combo3.price * quantity;
                        grand_total += total;                   
                        csum.combo_trans++;                                                                                         
                        fprintf(fptr, "%-15u%-27s%-17.2f\n", quantity, combo3.name, combo3.price);
                        print_order(quantity, item, price, grand_total);                            
                        break;
                    case '4':
                        price = combo4.price;
                        item = combo4.name;                     
                        total = combo4.price * quantity;
                        grand_total += total;                       
                        csum.combo_trans++;                         
                        fprintf(fptr, "%-15u%-27s%-17.2f\n", quantity, combo4.name, combo4.price);
                        print_order(quantity, item, price, grand_total);                        
                        break;
                    case '5':
                        price = combo5.price;
                        item = combo5.name;                     
                        total = combo5.price * quantity;
                        grand_total += total;                       
                        csum.combo_trans++;                                                             
                        fprintf(fptr, "%-15u%-27s%-17.2f\n", quantity, combo5.name, combo5.price);
                        print_order(quantity, item, price, grand_total);                        
                        break;           
                    default:
                        total =0;                                                                   
                }                   
                fclose(fptr); //close the file              
            }       
        } 
        else if (strlen(meal_choice) == 5 && meal_choice[0] == 'A' && meal_choice[1] == '0' && meal_choice[2] == '0' && meal_choice[3] == '0') {
            printf("Enter quantity of order. \n");
            scanf("%d", &quantity);

            fptr = fopen("receipt.txt", "a"); //opens txt file and READS only
            if(fptr == NULL) //check whether the file is empty or not
            {
                printf("File cannot be found\n");

            } else {                
                switch (meal_choice[4]) {                                           
                    case '1':           // assuming they chose set 1 with code C0001 
                        price = addon1.price;               
                        item = addon1.name;                                 
                        total = addon1.price * quantity;
                        asum.ala_trans++;
                        grand_total += total;                                                                                                                           
                        fprintf(fptr, "%d\t%s\t\t\t%.2f\n", quantity, addon1.name, addon1.price);                   
                        print_order(quantity, item, price, grand_total);                
                        break;
                    case '2':
                        price = addon2.price;
                        item = addon2.name;                     
                        total = addon2.price * quantity;
                        asum.ala_trans++;
                        grand_total += total;                                                                                                                                   
                        fprintf(fptr, "%d\t%s\t\t\t%.2f\n", quantity, addon2.name, addon2.price);
                        print_order(quantity, item, price, grand_total);                            
                        break;
                    case '3':
                        price = addon3.price;
                        item = addon3.name;                     
                        total = addon3.price * quantity;
                        asum.ala_trans++;
                        grand_total += total;                                                                                                   
                        fprintf(fptr, "%d\t%s\t\t\t%.2f\n", quantity, addon3.name, addon3.price);
                        print_order(quantity, item, price, grand_total);                            
                        break;
                    case '4':
                        price = addon4.price;
                        item = addon4.name;                     
                        total = addon4.price * quantity;                        
                        asum.ala_trans++;
                        grand_total += total;                                                                                                   
                        fprintf(fptr, "%d\t%s\t\t\t%.2f\n", quantity, addon4.name, addon4.price);
                        print_order(quantity, item, price, grand_total);                        
                        break;
                    case '5':
                        price = addon5.price;
                        item = addon5.name;                     
                        total = addon5.price * quantity;
                        asum.ala_trans++;
                        grand_total += total;                                                                                                           
                        fprintf(fptr, "%d\t%s\t\t\t%.2f\n", quantity, addon5.name, addon5.price);
                        print_order(quantity, item, price, grand_total);                        
                        break;           
                    default:
                        total =0;                                                   
                }       
                fclose(fptr); //close the file                                              
            } //end else for file found         
        } else {
            quantity = 0; //declare quantity 0 because to prevent dummy data
            price = 0; //declare price of certain order 0 because to prevent added into total
            total =0; //declare total in order 0 because to prevent added into grand total
            printf("Invalid meal code. \n");
        } //end else for menu option A or C         
        sum.total = grand_total;
        sum.grand_total += total;                   
    } //end of while loop       
    return 0;
}