include
define MAX_DISHES 100
define MAX_NAME_LENGTH 50
// 菜品结构体
struct Dish {
char name[MAX_NAME_LENGTH];
float price;
};
// 用户订单结构体
struct Order {
struct Dish dishes[MAX_DISHES];
int count;
float total;
};
// 菜单数据(示例)
struct Dish menu[MAX_DISHES] = {
{ "凉菜", 15.0}, { "热菜", 20.0}, { "饮料", 5.0},
// 添加更多菜品...
};
// 用户订单(示例)
struct Order user_order = { .count = 0, .total = 0.0 };
// 显示菜单函数
void display_menu(struct Dish *menu, int max_dishes) {
printf("菜单编号\t菜品名称\t价格\n");
for (int i = 0; i < max_dishes; i++) {
printf("%d.%s\t%.2f\n", i+1, menu[i].name, menu[i].price);
}
}
// 添加菜品到订单函数(简化实现)
void add_dish(struct Order *order, struct Dish *menu, int max_dishes) {
if (order->count < MAX_DISHES) {
printf("请输入菜品编号或名称:");
scanf("%s", menu->name);
float price = 0.0;
for (int i = 0; i < max_dishes; i++) {
if (strcmp(menu[i].name, menu->name) == 0) {
price = menu[i].price;
break;
}
}
order->dishes[order->count].name = menu->name;
order->dishes[order->count].price = price;
order->count++;
order->total += price;
} else {
printf("订单已满!\n");
}
}
// 结算订单函数
void checkout(struct Order *order) {
printf("订单总价:%.2f元\n", order->total);
}
int main() {
int choice, num;
char input;
while (1) {
display_menu(menu, MAX_DISHES);
printf("输入0退出,其他数字点菜:");
scanf("%d", &choice);
switch (choice) {
case 0:
printf("感谢用餐!\n");
break;
case 1:
add_dish(&user_order, menu, MAX_DISHES);
break;
case 2:
printf("当前总价:%.2f元\n", user_order.total);
break;
case 3:
checkout(&user_order);
break;
default:
printf("无效选择,请重试!\n");
}
printf("按任意键继续或退出:");
getchar();
}
return 0;
}
```
四、扩展建议
数据持久化
使用文件读写操作将菜单和订单数据保存到磁盘,程序启动时加载数据。
功能完善
添加删除菜品、修改订单、服务请求等模块,提升系统实用性。
用户体验优化
支持多语言界面、图形化菜单展示等