C语言 如何显示所有来自if else语句的输入字符串?

axr492tv  于 2023-01-01  发布在  其他
关注(0)|答案(2)|浏览(138)

在程序结束之前,说明总成本,我想该计划列出的项目所选择的用户。

以下是我的代码:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

struct menuDetails {
    double total, priceA, priceB, priceC, priceD, priceE, priceF;
} md;

int main()
{
    int itemTotal, itemName, itemAll;
    int sum=0;
    char item;
    char itemA[10]= "Nasi Ayam";
    char itemB[20]= "Nasi Ayam Merah";
    char itemC[15]= "Nasi Kerabu";
    char itemD[10]= "Sotong";
    char itemE[10]= "Teh Peng";
    char itemF[5]= "Kopi";

    printf("\t========= Menu =========\n");
    printf("Item \t Name \t\t\t Price (RM)\n");
    printf("A \t Nasi Ayam \t\t 3.50\n");
    printf("B \t Nasi Ayam Merah \t 4.00\n");
    printf("C \t Nasi Kerabu \t\t 5.50\n");
    printf("D \t Sotong \t\t 2.00\n");
    printf("E \t Teh Peng \t\t 2.00\n");
    printf("F \t Kopi \t\t\t 2.00\n");
    
    printf("\nHow many items would you like to purchase: ");
    scanf("%d", &itemTotal);
    printf("You have chose to order %d items\n\n", itemTotal);
    
    for(itemName = 1; itemName <= itemTotal; itemName++)
    {
        printf("Item %d [Enter Item (A-F)]: ", itemName);
        scanf(" %c", &item);
        
        md.priceA = 3.50;
        md.priceB = 4.00;
        md.priceC = 5.50;
        md.priceD = 2.00;
        md.priceE = 2.00;
        md.priceF = 2.00;
        
        if (item == 'A' || item == 'a') {
            printf("%s = RM%.2f\n\n", itemA, md.priceA);
            md.total = md.total + md.priceA;
        }else if (item == 'B' || item == 'b') {
            printf("%s = RM%.2f\n\n", itemB, md.priceB);
            md.total = md.total + md.priceB;
        }else if (item == 'C' || item == 'c') {
            printf("%s = RM%.2f\n\n", itemC, md.priceC);
            md.total = md.total + md.priceC;
        }else if (item == 'D' || item == 'd') {
            printf("%s = RM%.2f\n\n", itemD, md.priceD);
            md.total = md.total + md.priceD;
        }else if (item == 'E' || item == 'e') {
            printf("%s = RM%.2f\n\n", itemE, md.priceE);
            md.total = md.total + md.priceE;
        }else if (item == 'F' || item == 'f') {
            printf("%s = RM%.2f\n\n", itemF, md.priceF);
            md.total = md.total + md.priceF;            
        }
    } 
    printf("The total is RM%.2f", md.total);
    
    time_t my_time = time(NULL);
    printf("\nDate: %s", ctime(&my_time));
    
    return 0;
}

输出:

========= Menu =========
Item     Name                    Price (RM)
A        Nasi Ayam               3.50
B        Nasi Ayam Merah         4.00
C        Nasi Kerabu             5.50
D        Sotong                  2.00
E        Teh Peng                2.00
F        Kopi                    2.00

How many items would you like to purchase: 3
You have chose to order 3 items

Item 1 [Enter Item (A-F)]: a
Nasi Ayam = RM3.50

Item 2 [Enter Item (A-F)]: b
Nasi Ayam Merah = RM4.00

Item 3 [Enter Item (A-F)]: c
Nasi Kerabu = RM5.50

The total is RM13.00
Date: Thu Dec 29 17:02:49 2022

预期产出:

========= Menu =========
Item     Name                    Price (RM)
A        Nasi Ayam               3.50
B        Nasi Ayam Merah         4.00
C        Nasi Kerabu             5.50
D        Sotong                  2.00
E        Teh Peng                2.00
F        Kopi                    2.00

How many items would you like to purchase: 3
You have chose to order 3 items

Item 1 [Enter Item (A-F)]: a
Nasi Ayam = RM3.50

Item 2 [Enter Item (A-F)]: b
Nasi Ayam Merah = RM4.00

Item 3 [Enter Item (A-F)]: c
Nasi Kerabu = RM5.50

Receipt:
Nasi Ayam
Nasi Ayam Merah
Nasi Kerabu

The total is RM13.00
Date: Thu Dec 29 17:02:49 2022

我想显示用户选择的项目列表。

chhqkbe1

chhqkbe11#

您可以通过添加更多的数组,并创建一个for循环来打印出接收器来实现这一点。

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#define MAX_ITEMS 10 

struct menuDetails {
    double total, priceA, priceB, priceC, priceD, priceE, priceF;
} md;

int main()
{
    int itemTotal, itemName, itemAll;
    int sum=0; // you can remove this, it is unused
    char item;

    char itemA[10]= "Nasi Ayam";
    char itemB[20]= "Nasi Ayam Merah";
    char itemC[15]= "Nasi Kerabu";
    char itemD[10]= "Sotong";
    char itemE[10]= "Teh Peng";
    char itemF[5]= "Kopi";

    // arrays to store the chosen items and their quantities
    char chosenItems[MAX_ITEMS];
    int quantities[MAX_ITEMS];
    int numChosenItems = 0;

    printf("\t========= Menu =========\n");
    printf("Item \t Name \t\t\t Price (RM)\n");
    printf("A \t Nasi Ayam \t\t 3.50\n");
    printf("B \t Nasi Ayam Merah \t 4.00\n");
    printf("C \t Nasi Kerabu \t\t 5.50\n");
    printf("D \t Sotong \t\t 2.00\n");
    printf("E \t Teh Peng \t\t 2.00\n");
    printf("F \t Kopi \t\t\t 2.00\n");

    printf("\nHow many items would you like to purchase: ");
    scanf("%d", &itemTotal);
    printf("You have chose to order %d items\n\n", itemTotal);

    for(itemName = 1; itemName <= itemTotal; itemName++)
    {
        printf("Item %d [Enter Item (A-F)]: ", itemName);
        scanf(" %c", &item);
    
        md.priceA = 3.50;
        md.priceB = 4.00;
        md.priceC = 5.50;
        md.priceD = 2.00;
        md.priceE = 2.00;
        md.priceF = 2.00;
    
        // store chosen item and the quantity in array
        if (item == 'A' || item == 'a') {
            printf("%s = RM%.2f\n\n", itemA, md.priceA);
            md.total = md.total + md.priceA;
            chosenItems[numChosenItems] = 'A';
            quantities[numChosenItems] = 1;
            numChosenItems++;
                }else if (item == 'B' || item == 'b') {
            printf("%s = RM%.2f\n\n", itemB, md.priceB);
            md.total = md.total + md.priceB;
            chosenItems[numChosenItems] = 'B';
            quantities[numChosenItems] = 1;
            numChosenItems++;
        }else if (item == 'C' || item == 'c') {
            printf("%s = RM%.2f\n\n", itemC, md.priceC);
            md.total = md.total + md.priceC;
            chosenItems[numChosenItems] = 'C';
            quantities[numChosenItems] = 1;
            numChosenItems++;
        }else if (item == 'D' || item == 'd') {
            printf("%s = RM%.2f\n\n", itemD, md.priceD);
            md.total = md.total + md.priceD;
            chosenItems[numChosenItems] = 'D';
            quantities[numChosenItems] = 1;
            numChosenItems++;
        }else if (item == 'E' || item == 'e') {
            printf("%s = RM%.2f\n\n", itemE, md.priceE);
            md.total = md.total + md.priceE;
            chosenItems[numChosenItems] = 'E';
            quantities[numChosenItems] = 1;
            numChosenItems++;
        }else if (item == 'F' || item == 'f') {
            printf("%s = RM%.2f\n\n", itemF, md.priceF);
            md.total = md.total + md.priceF;
            chosenItems[numChosenItems] = 'F';
            quantities[numChosenItems] = 1;
            numChosenItems++;            
        }
    } 
    printf("The total is RM%.2f\n\n", md.total);

    // Print out the receipt
    printf("===== Receipt =====\n");
    for (int i = 0; i < numChosenItems; i++) {
        if (chosenItems[i] == 'A') {
            printf("%d x %s\n", quantities[i], itemA, md.priceA);
        } else if (chosenItems[i] == 'B') {
            printf("%d x %s\n", quantities[i], itemB, md.priceB);
        } else if (chosenItems[i] == 'C') {
            printf("%d x %s\n", quantities[i], itemC, md.priceC);
        } else if (chosenItems[i] == 'D') {
            printf("%d x %s\n", quantities[i], itemD, md.priceD);
        } else if (chosenItems[i] == 'E') {
            printf("%d x %s\n", quantities[i], itemE, md.priceE);
        } else if (chosenItems[i] == 'F') {
            printf("%d x %s\n", quantities[i], itemF, md.priceF);
        }
    }
    printf("Total \t\t\t RM%.2f\n", md.total);

    time_t my_time = time(NULL);
    printf("Date: %s", ctime(&my_time));

    return 0;
}

输出:

========= Menu =========
Item     Name                    Price (RM)
A        Nasi Ayam               3.50
B        Nasi Ayam Merah         4.00
C        Nasi Kerabu             5.50
D        Sotong                  2.00
E        Teh Peng                2.00
F        Kopi                    2.00

How many items would you like to purchase: 3
You have chose to order 3 items

Item 1 [Enter Item (A-F)]: a
Nasi Ayam = RM3.50

Item 2 [Enter Item (A-F)]: b
Nasi Ayam Merah = RM4.00

Item 3 [Enter Item (A-F)]: c
Nasi Kerabu = RM5.50

The total is RM13.00

===== Receipt =====
1 x Nasi Ayam
1 x Nasi Ayam Merah
1 x Nasi Kerabu
Total                    RM13.00
Date: Thu Dec 29 11:11:24 2022
guicsvcw

guicsvcw2#

如果你想列出用户存储的菜单项,并允许用户多次点同一个菜单项,那么你可以为每个菜单项添加一个额外的计数器变量,或者更好的方法是创建一个数组,在下面的代码中我将其命名为item_counters

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <time.h>

struct menu_item
{
    const char *name;
    double price;
};

#define NUM_MENU_ITEMS 6

static const struct menu_item menu_items[NUM_MENU_ITEMS] = {
    { "Nasi Ayam", 3.50 },
    { "Nasi Ayam Merah", 4.00 },
    { "Nasi Kerabu", 5.50 },
    { "Sotong", 2.00 },
    { "Teh Peng", 2.00 },
    { "Kopi", 2.00 }
};

int main( void )
{
    int total_items;
    int item_counters[NUM_MENU_ITEMS] = {0};
    double total_cost = 0.0;

    //print the menu using a loop
    printf("\t========= Menu =========\n");
    printf("Item     Name                   Price (RM)\n");
    for ( int i = 0; i < NUM_MENU_ITEMS; i++ )
    {
        printf(
            "%-8c %-22s %lf\n",
            'A'+i, menu_items[i].name, menu_items[i].price
        );
    }
    
    printf( "\nHow many items would you like to purchase: ");
    if ( scanf( "%d", &total_items ) != 1 )
    {
        fprintf( stderr, "Input error!\n" );
        exit( EXIT_FAILURE );
    }

    printf( "You have chosen to order %d items.\n\n", total_items );

    //ask user to select items
    for( int i = 0; i < total_items; i++ )
    {
        char item;

        //prompt user to select item
        printf( "Item %d [Enter Item (A-%c)]: ", i, 'A'+(NUM_MENU_ITEMS-1) );

        //attempt to read one character
        if ( scanf( " %c", &item ) != 1 )
        {
            fprintf( stderr, "Input error!\n" );
            exit( EXIT_FAILURE );
        }

        //make input upper-case, if it is lower-case
        item = toupper( (unsigned char)item );

        //verify that character is valid
        if ( item < 'A' || item > 'A' + (NUM_MENU_ITEMS-1) )
        {
            printf( "Invalid input!\n" );
            exit( EXIT_FAILURE );
        }

        //increment counter for selected item
        item_counters[item-'A']++;
    }

    //print back the order to the user, and calculate total cost
    printf( "\nYou ordered:\n" );
    for ( int i = 0; i < NUM_MENU_ITEMS; i++ )
    {
        if ( item_counters[i] != 0 )
        {
            double total_cost_for_item;

            total_cost_for_item = item_counters[i] * menu_items[i].price;

            printf(
                "%d %s for %lf\n",
                item_counters[i], menu_items[i].name, total_cost_for_item
            );

            total_cost += total_cost_for_item;
        }
    }

    //print total cost
    printf( "\nThe total is RM%.2f\n", total_cost );

    //print time
    time_t my_time = time(NULL);
    printf( "Date: %s\n", ctime(&my_time));
    
    return 0;
}

此程序具有以下行为:

========= Menu =========
Item     Name                   Price (RM)
A        Nasi Ayam              3.500000
B        Nasi Ayam Merah        4.000000
C        Nasi Kerabu            5.500000
D        Sotong                 2.000000
E        Teh Peng               2.000000
F        Kopi                   2.000000

How many items would you like to purchase: 5
You have chosen to order 5 items.

Item 0 [Enter Item (A-F)]: a
Item 1 [Enter Item (A-F)]: b
Item 2 [Enter Item (A-F)]: c
Item 3 [Enter Item (A-F)]: a
Item 4 [Enter Item (A-F)]: d

You ordered:
2 Nasi Ayam for 7.000000
1 Nasi Ayam Merah for 4.000000
1 Nasi Kerabu for 5.500000
1 Sotong for 2.000000

The total is RM18.50
Date: Thu Dec 29 15:54:04 2022

注意我已经修改了你的程序,你可以很容易地改变菜单中的项目数,程序会自动适应。

#define NUM_MENU_ITEMS 6

#define NUM_MENU_ITEMS 7

并在列表中添加一项:

static const struct menu_item menu_items[NUM_MENU_ITEMS] = {
    { "Nasi Ayam", 3.50 },
    { "Nasi Ayam Merah", 4.00 },
    { "Nasi Kerabu", 5.50 },
    { "Sotong", 2.00 },
    { "Teh Peng", 2.00 },
    { "Kopi", 2.00 },
    { "Cheese Sandwich", 3.00 }
};

如你所见,程序会自动调整:

========= Menu =========
Item     Name                   Price (RM)
A        Nasi Ayam              3.500000
B        Nasi Ayam Merah        4.000000
C        Nasi Kerabu            5.500000
D        Sotong                 2.000000
E        Teh Peng               2.000000
F        Kopi                   2.000000
G        Cheese Sandwich        3.000000

How many items would you like to purchase: 5
You have chosen to order 5 items.

Item 0 [Enter Item (A-G)]: A
Item 1 [Enter Item (A-G)]: G
Item 2 [Enter Item (A-G)]: C
Item 3 [Enter Item (A-G)]: G
Item 4 [Enter Item (A-G)]: F

You ordered:
1 Nasi Ayam for 3.500000
1 Nasi Kerabu for 5.500000
1 Kopi for 2.000000
2 Cheese Sandwich for 6.000000

The total is RM17.00
Date: Thu Dec 29 16:00:03 2022

菜单输出是自动适应的,现在它要求和接受从AG的输入,而不是从AF的输入。这就是程序中除了一个地方之外不硬编码任何东西的优点。
值得注意的是,这个程序假设字母表的字符代码是连续存储在字符集中的,这是大多数字符集的情况,包括ASCII字符集。然而,在一些更奇特的平台上可能不是这样的。在那些平台上,代码将不起作用。

相关问题