这是报错:
这是代码:
#include <stdio.h>
#include <stdlib.h>
#include<time.h>
// 定义菜品结构体
typedef struct {
int id; // 菜品编号
char name[20]; // 菜品名称
int price; // 菜品价格
} Dish;
// 定义订单结构体
typedef struct {
int floor; // 楼层
Dish dishes[10]; // 菜品列表
char remarks[50]; // 备注
int total_price; // 总价
} Order;
// 定义三个楼层的菜单
Dish menu_1[] = {
{1, "麻辣香锅", 25},
{2, "烧烤拼盘", 35},
{3, "回锅肉", 18},
{0, "", 0} // 结束标志
};
Dish menu_2[] = {
{4, "宫保鸡丁", 20},
{5, "鱼香肉丝", 22},
{6, "清蒸鲈鱼", 30},
{0, "", 0} // 结束标志
};
Dish menu_3[] = {
{7, "糖醋排骨", 22},
{8, "麻婆豆腐", 16},
{9, "水煮鱼", 30},
{0, "", 0} // 结束标志
};
// 遍历输出菜单
void print_menu(Dish *menu) {
printf("菜单如下:\n");
int i = 0;
while (menu[i].id != 0) {
printf("%d\t%-20s %d元\n", menu[i].id, menu[i].name, menu[i].price);
i++;
}
}
// 获取用户选择的菜品列表
void select_dishes(Order *order, Dish *menu) {
int dish_id;
while (1) {
printf("请输入要点的菜品编号(输入0结束):");
scanf("%d", &dish_id);
if (dish_id == 0) {
break;
}
int i = 0;
while (menu[i].id != 0) {
if (menu[i].id == dish_id) {
order->dishes[dish_id - 1] = menu[i];
printf("%s已添加到购物车!\n", menu[i].name);
break;
}
i++;
}
if (menu[i].id == 0) {
printf("菜品编号不存在!\n");
}
}
}
// 添加备注信息
void add_remarks(Order *order) {
printf("请输入备注信息:");
scanf("%s", order->remarks);
}
// 计算订单总价
void calc_total_price(Order *order) {
int i = 0;
while (order->dishes[i].id != 0) {
order->total_price += order->dishes[i].price;
i++;
}
}
// 完成支付
void do_payment(Order *order) {
printf("您的订单详情如下:\n");
printf("楼层:%d\n", order->floor);
printf("菜品列表:\n");
int i = 0;
while (order->dishes[i].id != 0) {
printf("\t%s\n", order->dishes[i].name);
i++;
}
printf("备注:%s\n", order->remarks);
printf("总价:%d元\n", order->total_price);
printf("请扫描二维码完成支付。\n");
}
int main() {
int floor;
printf("请输入楼层:");
scanf("%d", &floor);
// 根据楼层选择菜单
Dish *menu;
if (floor == 1) {
menu = menu_1;
} else if (floor == 2) {
menu = menu_2;
} else if (floor == 3) {
menu = menu_3;
} else {
printf("楼层不存在!\n");
return 0;
}
// 输出菜单
print_menu(menu);
// 用户选择菜品
Order order;
memset(&order, 0, sizeof(Order));
order.floor = floor;
select_dishes(&order, menu);
// 添加备注信息
add_remarks(&order);
// 计算订单总价
calc_total_price(&order);
// 完成支付
do_payment(&order);
//return 0;
// 自主点餐模式
void self_order() {
Order order = {0};
print_menu(menu_1);
select_dishes(&order, menu_1);
print_menu(menu_2);
select_dishes(&order, menu_2);
print_menu(menu_3);
select_dishes(&order, menu_3);
add_remarks(&order);
calc_total_price(&order);
do_payment(&order);
}
// 外卖盲盒模式
void random_order() {
printf("请选择盲盒类型:\n");
printf("1. 食品类别随机抽取\n");
printf("2. 不同楼层随机抽取\n");
printf("3. 根据辣度随机抽取\n");
int choice;
scanf("%d", &choice);
switch (choice) {
case 1: {
int rand_num = rand() % 3 + 1;
printf("您获得的食品类别为:");
switch (rand_num) {
case 1:
printf("面食\n");
break;
case 2:
printf("冒菜\n");
break;
case 3:
printf("炒菜\n");
break;
}
break;
}
case 2: {
int rand_num = rand() % 3 + 1;
printf("您获得的楼层为%d楼\n", rand_num);
break;
}
case 3: {
int rand_num = rand() % 3 + 1;
printf("您获得的辣度为:");
switch (rand_num) {
case 1:
printf("微辣\n");
break;
case 2:
printf("中辣\n");
break;
case 3:
printf("特辣\n");
break;
}
break;
}
default:
printf("无效的选择!\n");
break;
}
}
int main() {
srand((unsigned) time(NULL)); // 初始化随机数种子
printf("欢迎使用大学食堂点餐系统!\n");
printf("请选择模式:\n");
printf("1. 自主点餐\n");
printf("2. 外卖盲盒\n");
int mode;
scanf("%d", &mode);
switch (mode) {
case 1:
self_order();
break;
case 2:
random_order();
break;
default:
printf("无效的选择!\n");
break;
}
return 0;
}
这是框架图: