//2020年3月12日22:28:39 #include<stdio.h> #include<stdlib.h> typedef struct foods { int size; int capicity; int *ptr; } Food; void printWelcome(); void showMenu(); void printBill(int nums[], int len); //定义主菜单 char menu[4][5][20] = { {"红烧排骨 30¥", "油焖大虾 28¥", "黄焖牛肉 35¥", "肉末茄子 22¥", "鱼香肉丝 20¥"}, {"米饭 4¥", "面条 5¥", "馒头 3¥", "水饺 10¥", "包子 10¥"}, {"百事可乐 7¥", "可口可乐 8¥", "王老吉 6¥", "加多宝 5¥", "果粒橙 9¥"}, {"水果布丁 10¥", "牛奶布丁 9¥", "水果捞 8¥", "桂花糕 6¥", "南瓜饼 6¥"} }; //定义价位表 int price[4][5] = { {30, 28, 35, 22, 20}, {4, 5, 3, 10, 10}, {7, 8, 6, 5, 9}, {10, 9, 8, 6, 6} }; int main() { //定义用户输入的一级菜单编号 int order1; //定义用户输入的二级菜单编号 int order2; //定义数量 int num = 0; //定义消费金额 int money = 0; //创建一个动态以为数组,存储用户输入的编号 // int *nums; // int len = 0; // nums = (int *)malloc(sizeof(int)*len); // char *foods; int res; printWelcome(); printf("请您点餐,中间用空格隔开 "); printf(" "); showMenu(); Food food; food.capicity = 6; food.size = 0; // food.ptr = (int *) malloc(food.capicity * sizeof(int)); food.ptr = (int *)calloc(food.capicity, sizeof(int)); // gets(foods); // puts(foods); while (scanf("%d", &res) == 1) { food.ptr[food.size] = res; food.size++; if(food.size == food.capicity) { food.ptr = (int *)realloc(food.ptr, food.capicity*2*sizeof(int)); food.capicity = food.capicity*2; } } for (int i = 0; i < food.size; i++) { // printf("%d ",food.ptr[i]); money = money + price[(food.ptr[i]-1) / 5][(food.ptr[i]-1) % 5]; } // printf("money = %d", money); printBill(food.ptr, food.size); printf("总计消费: %d ¥ ", money); return 0; } //打印欢迎标语 void printWelcome(){ printf("**************************************************************************************************************** "); printf("* TomHe快餐店,欢迎您点餐 * "); printf(" "); } //打印菜单 void showMenu(){ int order = 1; for(int i = 0; i < 4; i++) { for(int j = 0; j < 5; j++){ printf("%-2d%s ",order,menu[i][j]); order++; } printf(" "); printf(" "); } } //打印账单 void printBill(int nums[], int len){ printf("您的消费账单如下: "); // printf("菜名 单价 "); for (int i = 0; i < len; i++) { // printf("%d. ",len); printf("%s ",menu[(nums[i]-1) / 5][(nums[i]-1) % 5]); // printf("%s ",price[(nums[i]-1) / 5][(nums[i]-1) % 5]); } }