如何在C语言点餐程序中输出用户选择的所有商品列表?
C语言点餐程序:添加已选商品清单功能
需求说明
我编写的C语言点餐程序目前可计算用户所选商品的总费用,但需要在显示总费用前,列出用户选择的所有商品。
现有代码
#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
修改后的代码
#include <stdio.h> #include <stdlib.h> #include <time.h> #include <string.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"; // 存储已选商品名称的动态数组 char** selectedItems = NULL; 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); // 为数组分配内存 selectedItems = (char**)malloc(itemTotal * sizeof(char*)); for(itemName = 0; itemName < itemTotal; itemName++) { printf("Item %d [Enter Item (A-F)]: ", itemName + 1); 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; selectedItems[itemName] = (char*)malloc(strlen(itemA) + 1); strcpy(selectedItems[itemName], itemA); }else if (item == 'B' || item == 'b') { printf("%s = RM%.2f\n\n", itemB, md.priceB); md.total = md.total + md.priceB; selectedItems[itemName] = (char*)malloc(strlen(itemB) + 1); strcpy(selectedItems[itemName], itemB); }else if (item == 'C' || item == 'c') { printf("%s = RM%.2f\n\n", itemC, md.priceC); md.total = md.total + md.priceC; selectedItems[itemName] = (char*)malloc(strlen(itemC) + 1); strcpy(selectedItems[itemName], itemC); }else if (item == 'D' || item == 'd') { printf("%s = RM%.2f\n\n", itemD, md.priceD); md.total = md.total + md.priceD; selectedItems[itemName] = (char*)malloc(strlen(itemD) + 1); strcpy(selectedItems[itemName], itemD); }else if (item == 'E' || item == 'e') { printf("%s = RM%.2f\n\n", itemE, md.priceE); md.total = md.total + md.priceE; selectedItems[itemName] = (char*)malloc(strlen(itemE) + 1); strcpy(selectedItems[itemName], itemE); }else if (item == 'F' || item == 'f') { printf("%s = RM%.2f\n\n", itemF, md.priceF); md.total = md.total + md.priceF; selectedItems[itemName] = (char*)malloc(strlen(itemF) + 1); strcpy(selectedItems[itemName], itemF); } } // 打印已选商品清单 printf("Receipt:\n"); for(itemName = 0; itemName < itemTotal; itemName++){ printf("%s\n", selectedItems[itemName]); free(selectedItems[itemName]); } free(selectedItems); printf("\n"); printf("The total is RM%.2f", md.total); time_t my_time = time(NULL); printf("\nDate: %s", ctime(&my_time)); return 0; }
修改说明
- 新增
char** selectedItems动态数组,根据用户输入的商品数量分配内存,用于存储已选商品名称 - 在每个商品选择分支中,为当前商品名称分配内存并复制到数组中
- 在输出总费用前,遍历数组打印所有已选商品,同时释放分配的内存避免泄漏
- 调整循环索引为0起始,打印序号时用
itemName + 1保持原有显示逻辑
内容的提问来源于stack exchange,提问作者Nozakai
相关产品推荐
相关产品推荐

