在程序结束之前,说明总成本,我想该计划列出的项目所选择的用户。
以下是我的代码:
#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
我想显示用户选择的项目列表。
2条答案
按热度按时间chhqkbe11#
您可以通过添加更多的数组,并创建一个for循环来打印出接收器来实现这一点。
输出:
guicsvcw2#
如果你想列出用户存储的菜单项,并允许用户多次点同一个菜单项,那么你可以为每个菜单项添加一个额外的计数器变量,或者更好的方法是创建一个数组,在下面的代码中我将其命名为
item_counters
:此程序具有以下行为:
注意我已经修改了你的程序,你可以很容易地改变菜单中的项目数,程序会自动适应。
到
并在列表中添加一项:
如你所见,程序会自动调整:
菜单输出是自动适应的,现在它要求和接受从
A
到G
的输入,而不是从A
到F
的输入。这就是程序中除了一个地方之外不硬编码任何东西的优点。值得注意的是,这个程序假设字母表的字符代码是连续存储在字符集中的,这是大多数字符集的情况,包括ASCII字符集。然而,在一些更奇特的平台上可能不是这样的。在那些平台上,代码将不起作用。