用c语言编写一个外卖系统】
时间: 2023-12-07 09:02:31 浏览: 39
当然可以!以下是一个简单的外卖系统的C语言代码示例:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct MenuItem {
char name[50];
float price;
};
struct Order {
struct MenuItem item;
int quantity;
};
void displayMenu(struct MenuItem menu[], int size) {
printf("==== 外卖菜单 ====\n");
for (int i = 0; i < size; i++) {
printf("%d. %s - ¥%.2f\n", i+1, menu[i].name, menu[i].price);
}
}
void placeOrder(struct MenuItem menu[], int size, struct Order orders[], int *orderCount) {
int choice, quantity;
displayMenu(menu, size);
printf("请选择要点的菜品编号(输入0退出): ");
scanf("%d", &choice);
if (choice == 0) {
printf("已退出外卖系统\n");
return;
}
printf("请输入数量: ");
scanf("%d", &quantity);
if (choice > 0 && choice <= size) {
struct Order newOrder;
newOrder.item = menu[choice-1];
newOrder.quantity = quantity;
orders[*orderCount] = newOrder;
(*orderCount)++;
printf("已成功下单!\n");
} else {
printf("无效的菜品编号!\n");
}
}
void displayOrder(struct Order orders[], int orderCount) {
float total = 0.0;
printf("==== 我的订单 ====\n");
for (int i = 0; i < orderCount; i++) {
struct Order order = orders[i];
printf("%d. %s - ¥%.2f × %d\n", i+1, order.item.name, order.item.price, order.quantity);
total += order.item.price * order.quantity;
}
printf("总计: ¥%.2f\n", total);
}
int main() {
struct MenuItem menu[5] = {
{"宫保鸡丁", 25.0},
{"鱼香肉丝", 20.0},
{"水煮肉片", 30.0},
{"回锅肉", 28.0},
{"麻婆豆腐", 15.0}
};
struct Order orders[10];
int orderCount = 0;
int choice;
while (1) {
printf("==== 外卖系统 ====\n");
printf("1. 查看菜单\n");
printf("2. 下单\n");
printf("3. 查看订单\n");
printf("4. 退出\n");
printf("请输入选择: ");
scanf("%d", &choice);
switch (choice) {
case 1:
displayMenu(menu, sizeof(menu)/sizeof(menu[0]));
break;
case 2:
placeOrder(menu, sizeof(menu)/sizeof(menu[0]), orders, &orderCount);
break;
case 3:
displayOrder(orders, orderCount);
break;
case 4:
printf("谢谢使用,再见!\n");
exit(0);
default:
printf("无效的选择!\n");
break;
}
printf("\n");
}
return 0;
}
```
这个代码示例实现了一个简单的外卖系统。它包括了菜单的展示、下单、查看订单等功能。你可以根据自己的需求对代码进行修改和扩展。希望对你有帮助!如果有任何问题,请随时提问。
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)