要求设计实现一个餐厅点餐系统,实现电子点餐。要求实现二级菜单:。
一级菜单: 请选择:
1.点餐
2.查看已点菜品
3.添菜
4.减菜
5.结算
0.退出
选择1.点餐后进入二级菜单:
1.宫保鸡丁10
2.酱爆鸭片 18
3.木须肉15
4.沸腾鱼88
5.手撕圆白菜12
6.麻婆豆腐8
7.小鸡炖蘑菇48
8.水晶肘子58
9.米饭3
0.香酥带鱼 25
a.返回
该系统完成以下功能:
1.点餐,进入点餐界面,选择菜品。
2.查看已点菜品,查看刚才点餐的结果。
3.添菜,在已点菜品的基础上增加菜品。
4.减菜,在已点菜品基础上减少菜品。
5.结算,根据已点菜品计算价格。
0.退出。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// 菜品结构体
struct Dish {
int id; // 菜品编号
char name[20]; // 菜品名称
int price; // 菜品价格
};
// 已点菜品结构体
struct OrderedDish {
int id; // 菜品编号
char name[20]; // 菜品名称
int price; // 菜品价格
int quantity; // 菜品数量
struct OrderedDish* next; // 指向下一个已点菜品的指针
};
// 菜品列表
struct Dish dishList[] = {
{1, "宫保鸡丁", 10},
{2, "酱爆鸭片", 18},
{3, "木须肉", 15},
{4, "沸腾鱼", 88},
{5, "手撕圆白菜", 12},
{6, "麻婆豆腐", 8},
{7, "小鸡炖蘑菇", 48},
{8, "水晶肘子", 58},
{9, "米饭", 3},
{0, "香酥带鱼", 25}
};
int numDishes = sizeof(dishList) / sizeof(struct Dish);
// 菜品数量
int dishCount[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
// 已点菜品列表头指针
struct OrderedDish* orderedDishes = NULL;
// 显示一级菜单
void showMainMenu() {
printf("\n请选择:\n");
printf("1.点餐\n");
printf("2.查看已点菜品\n");
printf("3.添菜\n");
printf("4.减菜\n");
printf("5.结算\n");
printf("0.退出\n");
}
// 显示二级菜单
void showSubMenu() {
printf("\n请选择菜品:\n");
for (int i = 0; i < numDishes; i++) {
printf("%d.%s%d元\n", i+1, dishList[i].name, dishList[i].price);
}
printf("a.返回\n");
}
// 选择菜品并加入已点菜品列表
void orderDish(int dishIndex) {
struct Dish dish = dishList[dishIndex];
// 创建新的已点菜品节点
struct OrderedDish* newDish = (struct OrderedDish*)malloc(sizeof(struct OrderedDish));
newDish->id = dish.id;
strcpy(newDish->name, dish.name);
newDish->price = dish.price;
newDish->quantity = 1;
newDish->next = NULL;
// 加入已点菜品列表
if (orderedDishes == NULL) {
orderedDishes = newDish;
} else {
struct OrderedDish* lastDish = orderedDishes;
while (lastDish->next != NULL) {
lastDish = lastDish->next;
}
lastDish->next = newDish;
}
printf("\n您点了一份%s\n", dish.name);
}
// 显示已点菜品列表
void showOrderedDishes() {
printf("\n已点菜品列表:\n");
struct OrderedDish* dish = orderedDishes;
while (dish != NULL) {
printf("%d份%s%d元\n", dish->quantity, dish->name, dish->price);
dish = dish->next;
}
}
// 添菜
void addDish(int dishIndex) {
int id = dishList[dishIndex].id;
for (int i = 0; i < numDishes; i++) {
if (dishList[i].id == id) {
dishCount[i]++;
printf("\n成功添菜一份%s\n", dishList[i].name);
return;
}
}
}
// 减菜
void removeDish(int dishIndex) {
int id = dishList[dishIndex].id;
for (int i = 0; i < numDishes; i++) {
if (dishList[i].id == id && dishCount[i] > 0) {
dishCount[i]--;
printf("\n成功减菜一份%s\n", dishList[i].name);
return;
}
}
}
// 计算已点菜品的总价
int calculateTotalPrice() {
int totalPrice = 0;
struct OrderedDish* dish = orderedDishes;
while (dish != NULL) {
totalPrice += dish->price * dish->quantity;
dish = dish->next;
}
return totalPrice;
}
int main() {
int choice, subChoice;
while (1) {
showMainMenu();
printf("请选择操作:");
scanf("%d", &choice);
switch (choice) {
case 0:
printf("\n欢迎下次光临!\n");
return 0;
case 1:
while (1) {
showSubMenu();
printf("请选择菜品:");
char subChoiceStr[10];
scanf("%s", subChoiceStr);
subChoice = atoi(subChoiceStr);
if (subChoice >= 1 && subChoice <= numDishes) {
orderDish(subChoice - 1);
} else if (subChoice == 0) {
break;
} else {
printf("\n无效选择,请重新选择。\n");
}
}
break;
case 2:
showOrderedDishes();
break;
case 3:
while (1) {
showSubMenu();
printf("请选择菜品:");
char subChoiceStr[10];
scanf("%s", subChoiceStr);
subChoice = atoi(subChoiceStr);
if (subChoice >= 1 && subChoice <= numDishes) {
addDish(subChoice - 1);
} else if (subChoice == 0 ) {
break;
} else {
printf("\n无效选择,请重新选择。\n");
}
}
break;
case 4:
while (1) {
showSubMenu();
printf("请选择菜品:");
char subChoiceStr[10];
scanf("%s", subChoiceStr);
subChoice = atoi(subChoiceStr);
if (subChoice >= 1 && subChoice <= numDishes) {
removeDish(subChoice - 1);
} else if (subChoice == 0) {
break;
} else {
printf("\n无效选择,请重新选择。\n");
}
}
break;
case 5:
if (orderedDishes == NULL) {
printf("\n您还没有点餐,请先点餐。\n");
break;
}
int totalPrice = calculateTotalPrice();
printf("\n已点菜品总价为%d元。\n", totalPrice);
orderedDishes = NULL;
memset(dishCount, 0, sizeof(dishCount));
break;
default:
printf("\n无效选择,请重新选择。\n");
}
}
return 0;
这份代码实现了餐厅点餐系统的要求,其中主要包括以下几个功能:
希望这份代码能够对您有所帮助。如果您有任何问题或需要进一步的帮助,请随时告诉我。
给你一个框架,你自己填空下
struct order_list_item
{
char *name;
int price;
};
int main()
{
int choice_main = -1; // 一级菜单选择
int choice_secondary = -1; // 二级菜单选择
struct order_list_item *order_list = NULL;
int order_count = 0; // 订单数量,动态变化
while (1) {
// 一级菜单
printf("请选择:\n1.点餐\n2.查看已点菜品\n3.添菜\n4.减菜\n5.结算\n0.退出\n");
scanf("%d", &choice_main);
switch (choice_main) {
case 1:
// 进入二级菜单
while (1) {
printf("请选择菜品:\n1.宫保鸡丁10\n2.酱爆鸭片18\n3.木须肉15\n4.沸腾鱼88\n5.手撕圆白菜12\n6.麻婆豆腐8\n7.小鸡炖蘑菇48\n8.水晶肘子58\n9.米饭3\n0.香酥带鱼25\na.返回\n");
scanf("%d", &choice_secondary);
if (choice_secondary == 0) {
break;
}
// TODO: 根据选择将菜品加入已点菜品列表,可以重复选择
}
break;
case 2:
// 查看已点菜品
// TODO: 遍历已点菜品列表并打印
break;
case 3:
// 添菜
// TODO: 跟随提示输入菜品名称,加入已点菜品列表
break;
case 4:
// 减菜
// TODO: 跟随提示输入要减少的菜品序号,从已点菜品列表中删除
break;
case 5:
// 结算
// TODO: 遍历已点菜品列表并计算价格,打印价格,清空已点菜品列表
break;
case 0:
// 退出
// TODO: 清空已点菜品列表并释放内存,退出程序
break;
default:
printf("输入有误,请重新输入\n");
break;
}
}
return 0;
}
比如选择菜品
struct order_list_item *chosen_items = NULL; // 已选择的菜品列表
while (1) {
printf(“请选择菜品:\n1.宫保鸡丁10\n2.酱爆鸭片18\n3.木须肉15\n4.沸腾鱼88\n5.手撕圆白菜12\n6.麻婆豆腐8\n7.小鸡炖蘑菇48\n8.水晶肘子58\n9.米饭3\n0.香酥带鱼25\na.返回\n”);
scanf("%d", &choice_secondary);
if (choice_secondary == 0) {
break;
}
if (choice_secondary < 1 || choice_secondary > 9) {
printf(“输入有误,请重新输入\n”);
continue;
}
// 初始化订单项
struct order_list_item item = {
.price = 0 // 等待计算价格
};
switch (choice_secondary)
{
case 1:
item.name = "宫保鸡丁";
item.price = 10;
break;
case 2:
item.name = "酱爆鸭片";
item.price = 18;
break;
case 3:
item.name = "木须肉";
item.price = 15;
break;
case 4:
item.name = "沸腾鱼";
item.price = 88;
break;
case 5:
item.name = "手撕圆白菜";
item.price = 12;
break;
case 6:
item.name = "麻婆豆腐";
item.price = 8;
break;
case 7:
item.name = "小鸡炖蘑菇";
item.price = 48;
break;
case 8:
item.name = "水晶肘子";
item.price = 58;
break;
case 9:
item.name = "米饭";
item.price = 3;
break;
default:
break;
}
// 加入已选择的菜品列表
order_count++;
chosen_items = realloc(chosen_items, order_count * sizeof(struct order_list_item));
chosen_items[order_count - 1] = item;
}
该回答引用于chatGPT:
好的,以下是一个简单的餐厅点单系统的C语言代码实现,使用了链表数据结构:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// 定义菜品结构体
typedef struct dish {
char name[20]; // 菜品名称
float price; // 菜品价格
int count; // 菜品数量
struct dish *next;
} Dish;
// 定义一个全局变量用于存储已点菜品
Dish *head = NULL;
// 添加一个菜品到已点菜品列表中
void addDish(char name[], float price) {
if (head == NULL) { // 当前列表为空,创建新的节点
head = (Dish*)malloc(sizeof(Dish));
strcpy(head->name, name);
head->price = price;
head->count = 1;
head->next = NULL;
} else { // 当前列表不为空,遍历寻找是否存在同名菜品
Dish *p = head;
while (p != NULL) {
if (strcmp(p->name, name) == 0) { // 已存在该菜品,数量+1
p->count++;
return;
}
p = p->next;
}
// 不存在该菜品,创建新的节点
Dish *newDish = (Dish*)malloc(sizeof(Dish));
strcpy(newDish->name, name);
newDish->price = price;
newDish->count = 1;
newDish->next = head;
head = newDish;
}
}
// 从已点菜品列表中移除一个指定的菜品
void removeDish(char name[]) {
if (head == NULL) { // 当前列表为空,直接返回
return;
} else if (strcmp(head->name, name) == 0) { // 要移除的菜品为头结点
if (head->count > 1) { // 存在多份该菜品,数量-1
head->count--;
} else { // 不存在多份该菜品,直接删除节点
Dish *p = head;
head = head->next;
free(p);
}
} else { // 要移除的菜品不为头结点,遍历并查找
Dish *p = head->next;
Dish *pre = head;
while (p != NULL) {
if (strcmp(p->name, name) == 0) {
if (p->count > 1) { // 存在多份该菜品,数量-1
p->count--;
} else { // 不存在多份该菜品,删除节点
pre->next = p->next;
free(p);
}
return;
}
pre = p;
p = p->next;
}
}
}
// 计算已点菜品列表的总价
float calculatePrice() {
float totalPrice = 0;
Dish *p = head;
while (p != NULL) {
totalPrice += p->price * p->count;
p = p->next;
}
return totalPrice;
}
// 显示已点菜品列表
void showDishes() {
printf("-----已点菜品列表-----\n");
Dish *p = head;
while (p != NULL) {
printf("%s ×%d %.2f元\n", p->name, p->count, p->price * p->count);
p = p->next;
}
printf("----------------------\n");
}
int main() {
int choice1, choice2;
char name[20];
float price;
// 一级菜单
while (1) {
printf("请选择:\n");
printf("1.点餐\n2.查看已点菜品\n3.添菜\n4.减菜\n5.结算\n0.退出\n");
scanf("%d", &choice1);
switch (choice1) {
case 1: // 点餐,进入二级菜单
printf("请选择菜品:\n");
printf("1.宫保鸡丁10元\n2.酱爆鸭片18元\n3.木须肉15元\n4.沸腾鱼88元\n5.手撕圆白菜12元\n6.麻婆豆腐8元\n7.小鸡炖蘑菇48元\n8.水晶肘子58元\n9.米饭3元\n0.香酥带鱼25元\na.返回\n");
while (1) { // 二级菜单循环
scanf("%s", name);
switch (name[0]) {
case '1':
addDish("宫保鸡丁", 10);
break;
case '2':
addDish("酱爆鸭片", 18);
break;
case '3':
addDish("木须肉", 15);
break;
case '4':
addDish("沸腾鱼", 88);
break;
case '5':
addDish("手撕圆白菜", 12);
break;
case '6':
addDish("麻婆豆腐", 8);
break;
case '7':
addDish("小鸡炖蘑菇", 48);
break;
case '8':
addDish("水晶肘子", 58);
break;
case '9':
addDish("米饭", 3);
break;
case '0':
addDish("香酥带鱼", 25);
break;
case 'a':
break;
default:
printf("无效菜品,请重新选择:\n");
continue; // 重新选择菜品
}
break; // 退出二级菜单循环
}
break;
case 2: // 查看已点菜品
showDishes();
break;
case 3: // 添菜
printf("请选择要添加的菜品(1-9):");
scanf("%d", &choice2);
printf("请输入要添加菜品的数量:");
int count;
scanf("%d", &count);
if (choice2 < 1 || choice2 > 9) {
printf("无效菜品,请重新选择\n");
} else {
Dish *p = head;
for (int i = 0; i < choice2 - 1; i++) {
p = p->next;
}
p->count += count;
}
break;
case 4: // 减菜
printf("请选择要删除的菜品名称:");
scanf("%s", name);
removeDish(name);
break;
case 5: // 结算
showDishes();
printf("总价格为:%.2f元\n", calculatePrice());
printf("谢谢光临!\n");
return 0; // 结束程序
case 0: // 退出
printf("谢谢光临!\n");
return 0; // 结束程序
default:
printf("无效操作,请重新选择。\n");
break;
}
}
return 0;
}
该系统实现了点餐、查看已点菜品、添菜、减菜和结算等基本功能,并通过链表数据结构来存储和管理已点菜品列表。
如果没人解决,加点我可以手打帮写了
引用chatGPT作答,下面是一些大致思路:
1.定义一个结构体来表示菜品,包括菜品名称、价格和数量等信息。
2.定义一个数组来存储菜单信息,包括菜品名称和价格等信息。
3.实现一个函数来显示菜单,接收一个指向菜单数组的指针作为参数,将菜单信息输出到屏幕上。
4.实现一个函数来处理用户选择的菜品,接收一个指向菜单数组的指针作为参数,让用户输入菜品编号或者a返回。
5.实现一个函数来处理用户点餐,接收一个指向菜单数组的指针和一个指向已点菜品的指针作为参数,根据用户选择的菜品将菜品信息添加到已点菜品中。
6.实现一个函数来显示已点菜品,接收一个指向已点菜品的指针作为参数,将已点菜品的信息输出到屏幕上。
7.实现一个函数来处理用户添菜,接收一个指向菜单数组的指针和一个指向已点菜品的指针作为参数,让用户输入要添加的菜品编号,根据菜品编号在菜单数组中查找菜品信息,并将菜品添加到已点菜品中。
8.实现一个函数来处理用户减菜,接收一个指向已点菜品的指针作为参数,让用户输入要减少的菜品编号,根据菜品编号在已点菜品中查找菜品信息,并将菜品从已点菜品中删除。
9.实现一个函数来计算已点菜品的价格,接收一个指向已点菜品的指针作为参数,根据已点菜品的信息计算价格并返回。
10.实现一个循环来处理用户的操作,根据用户选择的选项调用相应的函数,直到用户选择退出为止。
以下是一个基于上述思路的 C 语言实现:
#include <stdio.h>
#include <stdlib.h>
// 菜品结构体
typedef struct {
char name[20]; // 菜品名称
float price; // 菜品价格
int count; // 菜品数量
} Dish;
// 菜单数组
Dish menu[] = {
{"宫保鸡丁", 10, 0},
{"酱爆鸭片", 18, 0},
{"木须肉", 15, 0},
{"沸腾鱼", 88, 0},
{"手撕圆白菜", 12, 0},
{"麻婆豆腐", 8, 0},
{"小鸡炖蘑菇", 48, 0},
{"水晶肘子", 58, 0},
{"米饭", 3, 0},
{"香酥带鱼", 25, 0}
};
int menuCount = sizeof(menu) / sizeof(Dish); // 菜单数组大小
// 已点菜品数组
Dish orderedDishes[10];
int orderedDishCount = 0;
// 显示菜单
void displayMenu(Dish *menu) {
printf("请选择:\n");
for (int i = 0; i < menuCount; i++) {
printf("%d. %s %g元\n", i + 1, menu[i].name, menu[i].price);
}
printf("a. 返回\n");
}
// 处理用户选择菜品
int chooseDish(Dish *menu) {
char choice[2];
int selected = 0;
printf("请输入选项:");
scanf("%s", choice);
if (choice[0] >= '1' && choice[0] <= '0' + menuCount) {
selected = choice[0] - '0';
} else if (choice[0] == 'a') {
selected = -1;
}
return selected;
}
// 处理用户点餐
void orderDish(Dish *menu, Dish *orderedDishes) {
int choice = 0;
while (1) {
displayMenu(menu);
choice = chooseDish(menu);
if (choice == -1) {
break;
}
orderedDishes[orderedDishCount] = menu[choice - 1];
orderedDishCount++;
printf("已点菜品:\n");
for (int i = 0; i < orderedDishCount; i++) {
printf("%s %g元\n", orderedDishes[i].name, orderedDishes[i].price);
}
}
}
// 显示已点菜品
void displayOrderedDishes(Dish *orderedDishes) {
printf("已点菜品:\n");
for (int i = 0; i < orderedDishCount; i++) {
printf("%d. %s %g元\n", i + 1, orderedDishes[i].name, orderedDishes[i].price);
}
}
// 处理用户添菜
void addDish(Dish *menu, Dish *orderedDishes) {
int dishID = -1;
int count = 0;
printf("请输入要添加的菜品编号:");
scanf("%d", &dishID);
while (dishID < 0 || dishID >= DISH_COUNT) {
printf("菜品编号无效,请重新输入:");
scanf("%d", &dishID);
}
printf("请输入要添加的数量:");
scanf("%d", &count);
while (count <= 0) {
printf("数量无效,请重新输入:");
scanf("%d", &count);
}
// 查找是否已点过该菜品
int index = -1;
for (int i = 0; i < orderedDishCount; i++) {
if (orderedDishes[i].id == dishID) {
index = i;
break;
}
}
if (index == -1) {
// 如果没有点过该菜品,则新增一个菜品项
if (orderedDishCount == MAX_DISH_COUNT) {
printf("已点菜品已满,无法再添加。\n");
} else {
orderedDishes[orderedDishCount].id = dishID;
orderedDishes[orderedDishCount].count = count;
orderedDishCount++;
printf("添加成功。\n");
}
} else {
// 如果已经点过该菜品,则增加该菜品数量
if (orderedDishes[index].count + count > MAX_DISH_COUNT) {
printf("已点菜品数量已满,无法再添加。\n");
} else {
orderedDishes[index].count += count;
printf("添加成功。\n");
}
}
}
// 主程序
int main() {
int choice = 0;
while (1) {
printf("请选择:\n");
printf("1. 点餐\n");
printf("2. 查看已点菜品\n");
printf("3. 添菜\n");
printf("4. 减菜\n");
printf("5. 结算\n");
printf("0. 退出\n");
printf("请输入选项:");
scanf("%d", &choice);
switch (choice) {
case 1:
orderedDishCount = 0;
orderDish(menu, orderedDishes);
break;
case 2:
displayOrderedDishes(orderedDishes);
break;
case 3:
displayOrderedDishes(orderedDishes);
addDish(menu, orderedDishes);
break;
case 4:
displayOrderedDishes(orderedDishes);
removeDish(menu, orderedDishes);
break;
case 5:
displayOrderedDishes(orderedDishes);
printf("总价:%g元\n", calculateTotalPrice(orderedDishes));
break;
case 0:
exit(0);
break;
default:
printf("无效选项,请重新输入。\n");
break;
}
}
return 0;
}
以上代码实现了所需的菜单、点餐、查看已点菜品、添菜、减菜和结算功能,用户可以通过选择相应的菜单项来操作。
如果您需要更详细的指导或有其他问题,请随时问我。
c
#include
#include
#include
#define MAX_SIZE 10
// 菜品结构体
typedef struct {
int num; // 编号
char name[20]; // 名称
int price; // 价格
int count; // 数量
} dish;
int dish_count = 0; // 已点菜品数量
dish dishes[MAX_SIZE]; // 已点菜品列表
// 显示一级菜单
void show_menu() {
printf("
请选择:
");
printf("1. 点餐
");
printf("2. 查看已点菜品
");
printf("3. 添菜
");
printf("4. 减菜
");
printf("5. 结算
");
printf("0. 退出
");
}
// 显示二级菜单
void show_dish_menu() {
printf("
请选择:
");
printf("1. 宫保鸡丁 10
");
printf("2. 酱爆鸭片 18
");
printf("3. 木须肉 15
");
printf("4. 沸腾鱼 88
");
printf("5. 手撕圆白菜 12
");
printf("6. 麻婆豆腐 8
");
printf("7. 小鸡炖蘑菇 48
");
printf("8. 水晶肘子 58
");
printf("9. 米饭 3
");
printf("0. 香酥带鱼 25
");
printf("a. 返回
");
}
// 选择菜品
void select_dish() {
char choice;
do {
show_dish_menu();
fflush(stdin); // 清空缓存区
printf("请选择菜品(1-9,0返回):
");
scanf("%c", &choice);
switch (choice) {
case '1':
dishes[dish_count].num = 1;
strcpy(dishes[dish_count].name, "宫保鸡丁");
dishes[dish_count].price = 10;
dishes[dish_count].count = 1;
dish_count++;
break;
case '2':
dishes[dish_count].num = 2;
strcpy(dishes[dish_count].name, "酱爆鸭片");
dishes[dish_count].price = 18;
dishes[dish_count].count = 1;
dish_count++;
break;
case '3':
dishes[dish_count].num = 3;
strcpy(dishes[dish_count].name, "木须肉");
dishes[dish_count].price = 15;
dishes[dish_count].count = 1;
dish_count++;
break;
case '4':
dishes[dish_count].num = 4;
strcpy(dishes[dish_count].name, "沸腾鱼");
dishes[dish_count].price = 88;
dishes[dish_count].count = 1;
dish_count++;
break;
case '5':
dishes[dish_count].num = 5;
strcpy(dishes[dish_count].name, "手撕圆白菜");
dishes[dish_count].price = 12;
dishes[dish_count].count = 1;
dish_count++;
break;
case '6':
dishes[dish_count].num = 6;
strcpy(dishes[dish_count].name, "麻婆豆腐");
dishes[dish_count].price = 8;
dishes[dish_count].count = 1;
dish_count++;
break;
case '7':
dishes[dish_count].num = 7;
strcpy(dishes[dish_count].name, "小鸡炖蘑菇");
dishes[dish_count].price = 48;
dishes[dish_count].count = 1;
dish_count++;
break;
case '8':
dishes[dish_count].num = 8;
strcpy(dishes[dish_count].name, "水晶肘子");
dishes[dish_count].price = 58;
dishes[dish_count].count = 1;
dish_count++;
break;
case '9':
dishes[dish_count].num = 9;
strcpy(dishes[dish_count].name, "米饭");
dishes[dish_count].price = 3;
dishes[dish_count].count = 1;
dish_count++;
break;
case '0':
break;
case 'a':
break;
default:
printf("输入有误,请重新输入!
");
break;
}
} while (choice != 'a' && choice != '0');
}
// 查看已点菜品
void view_dishes() {
printf("
已点菜品如下:
");
printf("序号 名称\t\t价格 数量
");
int i, total_price = 0;
for (i = 0; i < dish_count; i++) {
printf("%-6d %-12s %-5d %-5d
", i + 1, dishes[i].name, dishes[i].price, dishes[i].count);
total_price += dishes[i].price * dishes[i].count;
}
printf("总价: %d
", total_price);
}
// 添菜
void add_dish() {
int num;
printf("
请输入要增加的菜品序号:");
scanf("%d", &num);
if (num > 0 && num <= dish_count) {
dishes[num - 1].count++;
printf("添加成功!
");
} else {
printf("菜品序号输入有误!
");
}
}
// 减菜
void delete_dish() {
int num;
printf("
请输入要减少的菜品序号:");
scanf("%d", &num);
if (num > 0 && num <= dish_count) {
if (dishes[num - 1].count <= 1) {
int i;
for (i = num - 1; i < dish_count - 1; i++) {
dishes[i] = dishes[i + 1];
}
dish_count--;
} else {
dishes[num - 1].count--;
}
printf("减少成功!
");
} else {
printf("菜品序号输入有误!
");
}
}
// 结算
void settle_dishes() {
view_dishes();
dish_count = 0;
printf("结算完成!
");
}
int main() {
char choice;
do {
show_menu();
fflush(stdin); // 清空缓存区
printf("请输入选择:
");
scanf("%c", &choice);
switch (choice) {
case '1': // 点餐
select_dish();
break;
case '2': // 查看已点菜品
view_dishes();
break;
case '3': // 添菜
add_dish();
break;
case '4': // 减菜
delete_dish();
break;
case '5': // 结算
settle_dishes();
break;
case '0': // 退出
break;
default:
printf("输入有误,请重新输入!
");
break;
}
} while (choice != '0');
printf("欢迎再次光临!
");
return 0;
}
c
#include
#include
#define MAX_DISHES 10 // 最大菜品数量
#define MAX_NAME_LENGTH 20 // 菜品名称最大长度
// 菜品结构体
typedef struct {
char name[MAX_NAME_LENGTH];
float price;
int count;
} Dish;
// 显示一级菜单
void show_first_menu() {
printf("
请选择:
");
printf("1. 点餐
");
printf("2. 查看已点菜品
");
printf("3. 添菜
");
printf("4. 减菜
");
printf("5. 结算
");
printf("0. 退出
");
}
// 显示二级菜单
void show_second_menu() {
printf("
请选择您要点的菜品:
");
printf("1. 宫保鸡丁\t¥10
");
printf("2. 酱爆鸭片\t¥18
");
printf("3. 木须肉\t¥15
");
printf("4. 沸腾鱼\t¥88
");
printf("5. 手撕圆白菜\t¥12
");
printf("6. 麻婆豆腐\t¥8
");
printf("7. 小鸡炖蘑菇\t¥48
");
printf("8. 水晶肘子\t¥58
");
printf("9. 米饭\t¥3
");
printf("0. 香酥带鱼\t¥25
");
printf("a. 返回
");
}
// 输出所有已点的菜品
void show_dishes(Dish dishes[], int count) {
if (count == 0) {
printf("
您还没有点任何菜品!
");
return;
}
printf("
您已点了以下菜品:
");
printf("%-20s%-10s%-10s
", "菜品名称", "单价(¥)", "数量");
for (int i = 0; i < count; i++) {
printf("%-20s%-10.2f%-10d
", dishes[i].name, dishes[i].price, dishes[i].count);
}
}
// 根据菜品名称查找菜品在菜单中的下标
int find_dish_index(char name[]) {
switch (name[0]) {
case '1':
return 0;
case '2':
return 1;
case '3':
return 2;
case '4':
return 3;
case '5':
return 4;
case '6':
return 5;
case '7':
return 6;
case '8':
return 7;
case '9':
return 8;
case '0':
if (name[1] == '\0') return 9;
break;
case 'a':
if (name[1] == '\0') return -2;
break;
}
printf("无效菜品编号!
");
return -1;
}
int main() {
Dish dishes[MAX_DISHES]; // 存储已点的菜品
int dish_count = 0; // 已点菜品数量
char choice = ' '; // 用户选择
do {
show_first_menu();
printf("请选择:");
scanf(" %c", &choice);
switch (choice) {
case '1':
do {
show_second_menu();
printf("请选择:");
scanf(" %c", &choice);
switch (choice) {
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
case '0':
if (dish_count >= MAX_DISHES) {
printf("您已点了太多的菜品,请先结算再点餐。
");
} else {
int index = find_dish_index(&choice);
if (index >= 0) {
Dish dish = dishes[index];
dish.count++;
dish.price *= dish.count;
dishes[index] = dish;
printf("已点菜品: %s
", dish.name);
}
}
break;
case 'a':
break;
default:
printf("无效选择!
");
break;
}
} while (choice != 'a');
break;
case '2':
show_dishes(dishes, dish_count);
break;
case '3':
printf("请输入要添加菜品的编号:");
scanf(" %c", &choice);
int index = find_dish_index(&choice);
if (index >= 0) {
if (dish_count >= MAX_DISHES) {
printf("您已点了太多的菜品,请先结算再点餐。
");
} else {
Dish dish = dishes[index];
dish.count++;
dish.price *= dish.count;
dishes[index] = dish;
printf("%s已经被成功添加。
", dish.name);
}
}
break;
case '4':
printf("请输入要减少菜品的编号:");
scanf(" %c", &choice);
index = find_dish_index(&choice);
if (index >= 0) {
Dish dish = dishes[index];
if (dish.count == 0) {
printf("该菜品未点,不能减少!
");
} else {
dish.count--;
if (dish.count == 0) {
printf("%s已被取消点餐。
", dish.name);
} else {
dish.price /= dish.count + 1;
dish.price *= dish.count;
dishes[index] = dish;
printf("%s数量已经减少到%d。
", dish.name, dish.count);
}
}
}
break;
case '5':
show_dishes(dishes, dish_count);
float total_price = 0;
for (int i = 0; i < dish_count; i++) {
total_price += dishes[i].price;
}
printf("
总共需要支付%.2f元。
", total_price);
printf("请付款,谢谢光临!
");
dish_count = 0;
break;
case '0':
printf("谢谢光临,再见!
");
break;
default:
printf("无效选择!
");
break;
}
} while (choice != '0');
return 0;
}
#include <stdio.h>
#include <windows.h>
#define menusize 10
#define foodf1 "%-13s\t%2d"
#define foodf2 "\t数量:%2d \t小计:%4d"
typedef struct {
const char *name;
int price;//如果有小数,把单位改为分
int num;
} food;
food menu[menusize] = {
{"1. 宫保鸡丁", 10, 0},
{"2. 酱爆鸭片", 18, 0},
{"3. 木须肉", 15, 0},
{"4. 沸腾鱼", 88, 0},
{"5. 手撕圆白菜", 12, 0},
{"6. 麻婆豆腐", 8, 0},
{"7. 小鸡炖蘑菇", 48, 0},
{"8. 水晶肘子", 58, 0},
{"9. 米饭", 3, 0},
{"0. 香酥黛玉", 25, 0}
};
#define index(num) ((num+menusize-1)%menusize) //根据序号求下标,如果不能用简单表达式表示,可以用数组、函数等
void order(food *menu, int size) {
int i, num;
//显示菜单
for (i = 0; i < size; i++) {
printf(foodf1, menu[i].name, menu[i].price);
putchar('\n');
}
printf("每行先后输入序号和数量,序号为负结束输入\n");
while (1) {
scanf("%d", &i);
if (i < 0)break;
if (getchar() == '\n') {
//只输入一个数字无效
continue;
} else {
scanf("%d", &num);
}
fflush(stdin);
menu[index(i)].num = num;
}
}
void check(food *menu, int size) {
int i, sum = 0;
for (i = 0; i < size; i++) {
if (menu[i].num == 0) continue;
printf(foodf1 foodf2, menu[i].name, menu[i].price, menu[i].num, menu[i].price * menu[i].num);
sum += menu[i].price * menu[i].num;
putchar('\n');
}
printf("合计:%d\n", sum);
}
void add(food *menu, int size) {
while (1) {
int i, num;
system("cls");
printf("已点菜:\n");
check(menu, size);
printf("请输入添加的菜的序号和添加的数量,序号为负结束输入\n");
scanf("%d", &i);
if (i < 0)break;
if (getchar() == '\n') {
//只输入一个数字无效
continue;
} else {
scanf("%d", &num);
}
fflush(stdin);
if (num < 0) {
printf("请输入非负值");
continue;
}
menu[index(i)].num += num;
}
}
void reduce(food *menu, int size) {
while (1) {
int i, num;
system("cls");
printf("已点菜:\n");
check(menu, size);
printf("请输入减少的菜的序号和减少的数量,序号为负结束输入\n");
scanf("%d", &i);
if (i < 0)break;
if (getchar() == '\n') {
//只输入一个数字无效
continue;
} else {
scanf("%d", &num);
}
fflush(stdin);
if (num < 0) {
printf("请输入非负值");
continue;
}
menu[index(i)].num -= num;
}
}
void account(food *menu, int size){
int i, sum = 0;
for (i = 0; i < size; i++) {
sum += menu[i].price * menu[i].num;
}
printf("合计:%d\n", sum);
printf("按回车结束");
getchar();
exit(0);
}
int main() {
const int listsize = 5;//除“0退出”外有5个选项
struct {
const char*text;
void (*func)(food *, int);
} list[] = {
{"1. 点餐", order},
{"2. 查看已点菜品", check},
{"3. 添菜", add},
{"4. 减菜", reduce},
{"5. 结算", account},
{"0. 退出", NULL}
};
int i, flag;
while (1) {
system("cls");
for (i = 0; i <= listsize; i++) {
printf("%s\n", list[i].text);
}
scanf("%d", &flag);
if (flag < 0)break;
fflush(stdin);
flag = (flag - 1) % listsize;//计算下标,防止越界
system("cls");
list[flag].func(menu, menusize);
printf("按回车继续");
getchar();
}
return 0;
}
根据题目要求,强行是使用了枚举的笨方法实现,发现链表难,理不清,下面的代码在gcc上编译能通过,但是bug太多,写不下去,没能完成题主要求,还需进一步修炼,贴上来供题主参考一下
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
enum
{
NONO = -1, /* 空状态 */
END = 0, /* 退出状态 */
ORDER_DISH = 1, /* 点菜状态 */
DISH_SHOW = 2, /* 查看状态 */
ADD_DISH = 3, /* 点菜状态 */
SUB_DISH = 4, /* 减菜状态 */
GET_Money = 5, /* 结账状态 */
};
volatile int g_MainStatus; /* 主状态字 */
#define NUM_DISHES 10
// 菜品结构体
typedef struct dish {
int id; // 排序号
char name[20]; //菜名
double price; //价格
int quantity; //数量
} dish;
//创建菜名和价格
char *names[10] = {"1.宫保鸡丁", "2.酱爆鸭片", "3.木须肉", "4.沸腾鱼",
"5.手撕圆白菜", "6.麻婆豆腐", "7.小鸡炖蘑菇", "8.水晶肘子", "9.米饭","0.香酥带鱼"};
double prices[10] = {10.0, 18.0, 15.0, 88.0, 12.0, 8.0, 48.0, 58.0, 3.0, 25.0};
//创建菜品数组
#define NUM_DISHES 10 //菜品数量
#define NUM_ORDER 100 //最大能点多少个菜
dish dishes[NUM_DISHES];
//定义结构体类型,表示用户点的每一道菜
typedef struct {
char name[20]; //菜名
double price; //价格
int quantity; //数量
} order;
order orders[NUM_ORDER];
int num_orders_placed = 0; //已点菜品数量
// 创建一个新的菜品
dish create_dish(char *name, double price, int quantity) {
//定义一个菜品结构体的变量d
dish d;
//给d的各个成员赋值,用参数传入的值
strcpy(d.name, name);
d.price = price;
d.quantity = quantity;
//返回d
return d;
}
// 打印菜单
void print_menu(dish *dishes, int num_dishes) {
char c; //用于读取用户输入的字符
int choice; //用于保存用户选择的数字值
printf("菜单如下:\n");
printf("名称\t\t价格\n");
for (int i = 0; i < NUM_DISHES; i++) {
printf("%-15s\t%4.2lf\n", dishes[i].name, dishes[i].price);
}
printf("输入对应的字符即可点菜\n");
printf("输入小写a字符退出当前界面\n");
fflush(stdout); //刷新输出缓存
while(g_MainStatus == ORDER_DISH) {
scanf("%c", &c); // 从键盘输入一个字符
if (c == 'a') { //如果用户输入了小写字母 a,则退出循环
g_MainStatus = NONO;
return;
}
if (c >= '0' && c <= '9') { //如果用户输入了数字,则转换为数字值
choice = c - '0';
switch (choice) {
case 0:
dishes[9].quantity++;
printf("香酥带鱼+%d\r\n",dishes[9].quantity);
strcpy(orders[9].name, dishes[9].name); //保存菜名
orders[9].quantity = dishes[9].quantity; //保存数量
orders[9].price = dishes[9].price; //
num_orders_placed++;
break;
case 1:
dishes[0].quantity++;
printf("宫保鸡丁+%d\r\n",dishes[0].quantity);
strcpy(orders[0].name, dishes[0].name); //保存菜名
orders[0].quantity = dishes[0].quantity; //保存数量
orders[0].price = dishes[0].price; //
num_orders_placed++;
break;
case 2:
dishes[1].quantity++;
printf("酱爆鸭片+%d\r\n",dishes[1].quantity);
strcpy(orders[1].name, dishes[1].name); //保存菜名
orders[1].quantity = dishes[1].quantity; //保存数量
orders[1].price = dishes[1].price; //
num_orders_placed++;
break;
case 3:
dishes[2].quantity++;
printf("木须肉+%d\r\n",dishes[2].quantity);
strcpy(orders[2].name, dishes[2].name); //保存菜名
orders[2].quantity = dishes[2].quantity; //保存数量
orders[2].price = dishes[2].price; //
num_orders_placed++;
break;
case 4:
dishes[3].quantity++;
printf("沸腾鱼+%d\r\n",dishes[3].quantity);
strcpy(orders[3].name, dishes[3].name); //保存菜名
orders[3].quantity = dishes[3].quantity; //保存数量
orders[3].price = dishes[3].price; //
num_orders_placed++;
break;
case 5:
dishes[4].quantity++;
printf("手撕圆白菜+%d\r\n",dishes[4].quantity);
strcpy(orders[4].name, dishes[4].name); //保存菜名
orders[4].quantity = dishes[4].quantity; //保存数量
orders[4].price = dishes[4].price; //
num_orders_placed++;
break;
case 6:
dishes[5].quantity++;
printf("麻婆豆腐+%d\r\n",dishes[5].quantity);
strcpy(orders[5].name, dishes[5].name); //保存菜名
orders[5].quantity = dishes[5].quantity; //保存数量
orders[5].price = dishes[5].price; //
num_orders_placed++;
break;
case 7:
dishes[6].quantity++;
printf("小鸡炖蘑菇+%d\r\n",dishes[6].quantity);
strcpy(orders[6].name, dishes[6].name); //保存菜名
orders[6].quantity = dishes[6].quantity; //保存数量
orders[6].price = dishes[6].price; //
num_orders_placed++;
break;
case 8:
dishes[7].quantity++;
printf("水晶肘子+%d\r\n",dishes[7].quantity);
strcpy(orders[7].name, dishes[7].name); //保存菜名
orders[7].quantity = dishes[7].quantity; //保存数量
orders[7].price = dishes[7].price; //
num_orders_placed++;
break;
case 9:
dishes[8].quantity++;
printf("米饭+%d\r\n",dishes[8].quantity);
strcpy(orders[8].name, dishes[8].name); //保存菜名
orders[8].quantity = dishes[8].quantity; //保存数量
orders[8].price = dishes[8].price; //
num_orders_placed++;
break;
default:
printf("无效选项,请重新输入。\n");
break;
}
}
fflush(stdout); //刷新输出缓存
}
}
// 打印所有菜品
void print_all_dishes(order *orders, int num_dishes) {
char c; //用于读取用户输入的字符
printf("您点的菜品如下:\n");
printf("名称\t\t价格\t数量\n");
for (int i = 0; i < num_dishes; i++) {
printf("%-15s\t%4.2lf\t%2d\n", orders[i].name, orders[i].price, orders[i].quantity);
}
fflush(stdout); //刷新输出缓存
printf("输入小写a字符退出当前界面\n");
while(g_MainStatus == ORDER_DISH) {
scanf("%c", &c); // 从键盘输入一个字符
if (c == 'a') { //如果用户输入了小写字母 a,则退出循环
g_MainStatus = NONO;
return;
}
}
}
int main() {
int choice;
//循环创建菜品
for (int i = 0; i < NUM_DISHES; i++) {
dishes[i] = create_dish(names[i], prices[i], 0);
}
//打印所有菜品
// printf("输入的字符是%c\r\n",choice);
// fflush(stdout); //刷新输出缓存
while(1)
{
printf("=======================\n");
printf("请输入对应的数字进行选择:\n");
printf("1.点餐\n");
printf("2.查看已点菜品\n");
printf("3.添菜\n");
printf("4.减菜\n");
printf("5.结算\n");
printf("0.退出\n");
fflush(stdout); //刷新输出缓存
scanf("%d", &choice); // 从键盘输入一个字符
g_MainStatus = choice;
switch (g_MainStatus)
{
case NONO:
break;
case END:
break;
case ORDER_DISH:
print_menu(dishes,NUM_DISHES);
g_MainStatus = NONO;
break;
case DISH_SHOW:
print_all_dishes(orders, num_orders_placed);
g_MainStatus = NONO;
break;
case ADD_DISH:
break;
case SUB_DISH:
break;
case GET_Money:
break;
default:
break;
}
}
}
```
仓库管理,信息管理改改吧
以下内容部分参考ChatGPT模型:
好的,作为你的AI助手,我来帮你解决这个问题。
首先,我们需要使用链表来存储已点菜品,因为链表具有动态分配空间的特性,可以随时添加和删除节点。
其次,我们需要设计一个菜品结构体,包括菜名、价格等属性。
接下来,我们可以设计一个菜单函数,用来显示一级菜单和二级菜单,并根据用户的选择执行相应的操作。
在点餐时,用户可以输入菜品编号或菜名来选择菜品,并将菜品加入已点菜品链表中。
在查看已点菜品时,我们可以遍历已点菜品链表,并输出每个节点的菜品信息。
在添菜和减菜时,我们可以让用户输入菜品编号或菜名,并遍历已点菜品链表,找到对应的节点进行添加或删除操作。
最后,在结算时,我们可以遍历已点菜品链表,计算总价格,并输出给用户。
下面是一个简单的实现示例:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// 菜品结构体
typedef struct dish {
char name[20]; // 菜名
float price; // 价格
struct dish *next; // 下一个节点指针
} Dish;
// 创建菜品节点
Dish *createDish(char *name, float price) {
Dish *newDish = (Dish *)malloc(sizeof(Dish));
strcpy(newDish->name, name);
newDish->price = price;
newDish->next = NULL;
return newDish;
}
// 添加菜品节点到链表末尾
void appendDish(Dish **head, Dish *newDish) {
if (*head == NULL) {
*head = newDish;
} else {
Dish *p = *head;
while (p->next != NULL) {
p = p->next;
}
p->next = newDish;
}
}
// 在链表中查找菜品节点并返回
Dish *findDish(Dish *head, char *name) {
Dish *p = head;
while (p != NULL) {
if (strcmp(p->name, name) == 0) {
return p;
}
p = p->next;
}
return NULL;
}
// 显示一级菜单
void showMainMenu() {
printf("请选择:\n");
printf("1.点餐\n");
printf("2.查看已点菜品\n");
printf("3.添菜\n");
printf("4.减菜\n");
printf("5.结算\n");
printf("0.退出\n");
}
// 显示二级菜单
void showDishMenu() {
printf("请选择:\n");
printf("1.宫保鸡丁 10元\n");
printf("2.酱爆鸭片 18元\n");
printf("3.木须肉 15元\n");
printf("4.沸腾鱼 88元\n");
printf("5.手撕圆白菜 12元\n");
printf("6.麻婆豆腐 8元\n");
printf("7.小鸡炖蘑菇 48元\n");
printf("8.水晶肘子 58元\n");
printf("9.米饭 3元\n");
printf("0.香酥带鱼 25元\n");
printf("a.返回\n");
}
// 点餐函数
void orderDish(Dish **head) {
char choice;
do {
showDishMenu();
scanf(" %c", &choice);
switch (choice) {
case '1':
appendDish(head, createDish("宫保鸡丁", 10));
printf("已点宫保鸡丁\n");
break;
case '2':
appendDish(head, createDish("酱爆鸭片", 18));
printf("已点酱爆鸭片\n");
break;
case '3':
appendDish(head, createDish("木须肉", 15));
printf("已点木须肉\n");
break;
case '4':
appendDish(head, createDish("沸腾鱼", 88));
printf("已点沸腾鱼\n");
break;
case '5':
appendDish(head, createDish("手撕圆白菜", 12));
printf("已点手撕圆白菜\n");
break;
case '6':
appendDish(head, createDish("麻婆豆腐", 8));
printf("已点麻婆豆腐\n");
break;
case '7':
appendDish(head, createDish("小鸡炖蘑菇", 48));
printf("已点小鸡炖蘑菇\n");
break;
case '8':
appendDish(head, createDish("水晶肘子", 58));
printf("已点水晶肘子\n");
break;
case '9':
appendDish(head, createDish("米饭", 3));
printf("已点米饭\n");
break;
case '0':
appendDish(head, createDish("香酥带鱼", 25));
printf("已点香酥带鱼\n");
break;
case 'a':
break;
default:
printf("选择错误,请重新选择!\n");
break;
}
} while (choice != 'a');
}
// 查看已点菜品函数
void showOrderList(Dish *head) {
if (head == NULL) {
printf("暂无已点菜品!\n");
} else {
printf("已点菜品:\n");
Dish *p = head;
while (p != NULL) {
printf("%s %.2f元\n", p->name, p->price);
p = p->next;
}
}
}
// 添菜函数
void addDish(Dish **head) {
char name[20];
printf("请输入要添的菜品名:");
scanf("%s", name);
Dish *dish = findDish(*head, name);
if (dish == NULL) {
printf("未找到该菜品!\n");
} else {
appendDish(head, createDish(dish->name, dish->price));
printf("已成功添菜!\n");
}
}
// 减菜函数
void removeDish(Dish **head) {
char name[20];
printf("请输入要减的菜品名:");
scanf("%s", name);
Dish *p = *head;
Dish *prev = NULL;
while (p != NULL) {
if (strcmp(p->name, name) == 0) {
if (prev == NULL) {
*head = p->next;
} else {
prev->next = p->next;
}
free(p);
printf("已成功减菜!\n");
return;
}
prev = p;
p = p->next;
}
printf("未找到该菜品!\n");
}
// 结算函数
void settleAccounts(Dish *head) {
if (head == NULL) {
printf("暂无已点菜品!\n");
} else {
float totalPrice = 0;
Dish *p = head;
while (p != NULL) {
totalPrice += p->price;
p = p->next;
}
printf("总价为:%.2f元\n", totalPrice);
}
}
// 主函数
int main() {
Dish *head = NULL; // 已点菜品链表头指针
char choice;
do {
showMainMenu();
scanf(" %c", &choice);
switch (choice) {
case '1':
orderDish(&head);
break;
case '2':
showOrderList(head);
break;
case '3':
addDish(&head);
break;
case '4':
removeDish(&head);
break;
case '5':
settleAccounts(head);
break;
case '0':
printf("谢谢惠顾!\n");
break;
default:
printf("选择错误,请重新选择!\n");
break;
}
} while (choice != '0');
return 0;
}
这个示例代码可以在控制台上运行,实现了一个简单的餐厅点单系统。你可以根据需求进行修改和完善。
如果我的建议对您有帮助、请点击采纳、祝您生活愉快
该回答通过自己思路及引用到部分GPTᴼᴾᴱᴺᴬᴵ搜索,得到内容具体如下。
以下是一个简单的C语言实现餐厅点单系统的代码,包括一级菜单和二级菜单的实现,以及点餐、查看已点菜品、添菜、减菜、结算和退出功能的实现:
#include <stdio.h>
#include <stdbool.h>
#define MAX_ITEMS 100
struct MenuItem {
char name[50];
int price;
};
struct OrderItem {
struct MenuItem menu_item;
int quantity;
};
struct Order {
struct OrderItem items[MAX_ITEMS];
int num_items;
};
void print_menu(struct MenuItem menu[], int num_items) {
printf("菜单:\n");
for (int i = 0; i < num_items; i++) {
printf("%d. %s - %d元\n", i+1, menu[i].name, menu[i].price);
}
}
void print_order(struct Order order) {
printf("已点菜品:\n");
for (int i = 0; i < order.num_items; i++) {
printf("%d. %s %d份 - %d元\n", i+1, order.items[i].menu_item.name,
order.items[i].quantity, order.items[i].menu_item.price * order.items[i].quantity);
}
}
void add_item(struct Order *order, struct MenuItem menu[], int num_items, int choice, int quantity) {
if (order->num_items == MAX_ITEMS) {
printf("订单已满,无法添加菜品。\n");
return;
}
order->items[order->num_items].menu_item = menu[choice-1];
order->items[order->num_items].quantity = quantity;
order->num_items++;
}
void remove_item(struct Order *order, int choice) {
if (choice < 0 || choice >= order->num_items) {
printf("无效选择,无法减少菜品。\n");
return;
}
for (int i = choice; i < order->num_items-1; i++) {
order->items[i] = order->items[i+1];
}
order->num_items--;
}
int calculate_total(struct Order order) {
int total = 0;
for (int i = 0; i < order.num_items; i++) {
total += order.items[i].menu_item.price * order.items[i].quantity;
}
return total;
}
int main() {
struct MenuItem menu[] = {
{"宫保鸡丁", 28},
{"鱼香肉丝", 25},
{"回锅肉", 35},
{"水煮鱼", 48},
{"糖醋里脊", 30},
{"麻婆豆腐", 18},
{"小炒肉", 28},
{"蒜蓉西兰花", 20},
{"清蒸鲈鱼", 68},
{"红烧排骨", 38}
};
int num_menu_items = sizeof(menu) / sizeof(menu[0]);
struct Order order = {
.items = {},
.num_items = 0
};
bool quit = false;
int choice, sub_choice, quantity;
do {
printf("欢迎光临!\n");
printf("1. 查看菜单\n");
printf("2. 点菜\n");
printf("3. 添菜\n");
printf("4. 减菜\n");
printf("5. 结算\n");
printf("0. 退出\n");
printf("请选择:\n");
scanf("%d", &choice);
switch (choice) {
case 1:
print_menu(menu, num_menu_items);
break;
case 2:
printf("请选择要点的菜品序号:\n");
scanf("%d", &sub_choice);
printf("请输入数量:\n");
scanf("%d", &quantity);
add_item(&order, menu, num_menu_items, sub_choice, quantity);
printf("已点菜品:\n");
print_order(order);
break;
case 3:
if (order.num_items == 0) {
printf("您还没有点菜,请先点菜。\n");
} else {
printf("请选择要添的菜品序号:\n");
scanf("%d", &sub_choice);
printf("请输入数量:\n");
scanf("%d", &quantity);
add_item(&order, menu, num_menu_items, sub_choice, quantity);
printf("已点菜品:\n");
print_order(order);
}
break;
case 4:
print_order
该程序中,`struct MenuItem` 表示菜品,`struct OrderItem` 表示订单中的一项菜品,`struct Order` 表示订单。`menu` 数组表示菜单,`num_menu_items` 表示菜单中的菜品数量。`print_menu()` 函数用于打印菜单,`print_order()` 函数用于打印订单中的菜品,`add_item()` 函数用于向订单中添加一项菜品,`remove_item()` 函数用于从订单中减少一项菜品,`calculate_total()` 函数用于计算订单的总价。
在 `main()` 函数中,使用一个 `do-while` 循环来实现一级菜单的功能,根据用户的选择进入不同的二级菜单或执行相应的功能。在二级菜单中,用户可以选择菜品进行点餐,点餐时会将菜品添加到订单中并打印订单,用户也可以在已点菜品的基础上添菜或减菜,结算时会打印订单并计算总价,然后清空订单。
该实现只是一个简单的例子,还有很多可以改进的地方,例如可以将菜单存储到文件中,允许管理员对菜单进行修改等等。
如果以上回答对您有所帮助,点击一下采纳该答案~谢谢