请用c语言制作一个简易的点餐系统
时间: 2024-03-03 13:47:24 浏览: 20
好的,以下是一个简单的点餐系统的C语言代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// 定义菜品结构体
typedef struct {
char name[20];
float price;
int quantity;
} Dish;
// 定义菜单数组
Dish menu[] = {
{"烤肉拌饭", 20.0, 100},
{"红烧肉", 30.0, 50},
{"麻辣烫", 15.0, 80},
{"炒面", 12.0, 120},
{"炒饭", 15.0, 100},
};
// 定义函数:显示菜单
void showMenu() {
printf("菜单:\n");
for (int i = 0; i < sizeof(menu) / sizeof(menu[0]); i++) {
printf("%d. %s ¥%.2f\n", i + 1, menu[i].name, menu[i].price);
}
printf("\n");
}
// 定义函数:点餐
void order() {
int choice, quantity;
float total = 0.0;
Dish orderList[10];
int orderCount = 0;
while (1) {
showMenu();
printf("请输入您要点的菜品编号(输入0结束点餐):");
scanf("%d", &choice);
if (choice == 0) {
break;
}
printf("请输入您要点的菜品数量:");
scanf("%d", &quantity);
Dish dish = menu[choice - 1];
if (dish.quantity < quantity) {
printf("菜品数量不足!\n");
continue;
}
dish.quantity = quantity;
orderList[orderCount++] = dish;
total += dish.price * quantity;
printf("已添加 %dx %s 到您的订单中\n", quantity, dish.name);
}
printf("您的订单如下:\n");
for (int i = 0; i < orderCount; i++) {
Dish dish = orderList[i];
printf("%d. %s ¥%.2f x %d\n", i + 1, dish.name, dish.price, dish.quantity);
}
printf("总计:¥%.2f\n", total);
}
// 定义主函数
int main() {
order();
return 0;
}
```
这段代码实现了一个简单的点餐系统,用户可以在菜单中选择菜品和数量,然后程序会根据用户的选择计算出订单总价并输出订单详情。