可以帮我完善到能运行吗
时间: 2024-03-24 16:38:40 浏览: 14
好的,我可以给您提供一个完整的代码,实现了用户注册登录、菜品管理和订单管理等功能。
```c
#include <stdio.h>
#include <string.h>
struct user {
char name[20];
char phone[11];
char address[100];
};
struct dish {
char name[20];
float price;
char description[100];
};
struct order {
int id;
struct user customer;
struct dish dish;
float total_price;
};
void print_menu(struct dish menu[]) {
int i;
printf("Menu:\n");
for (i = 0; i < 5; i++) {
printf("%d. %s: $%.2f\n", i+1, menu[i].name, menu[i].price);
printf(" %s\n", menu[i].description);
}
}
void add_dish(struct dish menu[]) {
int i;
for (i = 0; i < 5; i++) {
if (strlen(menu[i].name) == 0) {
printf("Please enter the name of the dish: ");
scanf("%s", menu[i].name);
printf("Please enter the price of the dish: ");
scanf("%f", &menu[i].price);
printf("Please enter the description of the dish: ");
scanf("%s", menu[i].description);
break;
}
}
}
void modify_dish(struct dish menu[]) {
int i, choice;
printf("Please enter the number of the dish you would like to modify: ");
scanf("%d", &choice);
printf("Please enter the new name of the dish: ");
scanf("%s", menu[choice-1].name);
printf("Please enter the new price of the dish: ");
scanf("%f", &menu[choice-1].price);
printf("Please enter the new description of the dish: ");
scanf("%s", menu[choice-1].description);
}
void delete_dish(struct dish menu[]) {
int i, choice;
printf("Please enter the number of the dish you would like to delete: ");
scanf("%d", &choice);
strcpy(menu[choice-1].name, "");
menu[choice-1].price = 0;
strcpy(menu[choice-1].description, "");
}
void place_order(struct order orders[], int *order_count, struct user current_user, struct dish menu[]) {
int i, choice;
printf("Please enter the number of the dish you would like to order: ");
scanf("%d", &choice);
printf("You have ordered %s.\n", menu[choice-1].name);
struct order new_order;
new_order.id = *order_count + 1;
new_order.customer = current_user;
new_order.dish = menu[choice-1];
new_order.total_price = menu[choice-1].price;
orders[*order_count] = new_order;
*order_count = *order_count + 1;
}
void view_orders(struct order orders[], int order_count) {
int i;
printf("Orders:\n");
for (i = 0; i < order_count; i++) {
printf("%d. %s ordered %s for $%.2f.\n", orders[i].id, orders[i].customer.name, orders[i].dish.name, orders[i].total_price);
}
}
int main() {
struct user current_user;
struct dish menu[5];
struct order orders[100];
int order_count = 0;
int i, choice;
// 用户注册登录
printf("Please enter your name: ");
scanf("%s", current_user.name);
printf("Please enter your phone number: ");
scanf("%s", current_user.phone);
printf("Please enter your address: ");
scanf("%s", current_user.address);
printf("Welcome, %s!\n", current_user.name);
// 菜单初始化
for (i = 0; i < 5; i++) {
strcpy(menu[i].name, "");
menu[i].price = 0;
strcpy(menu[i].description, "");
}
// 菜单管理
while (1) {
printf("\nMain menu:\n1. View menu\n2. Add dish\n3. Modify dish\n4. Delete dish\n5. Place order\n6. View orders\n7. Exit\n");
printf("Please enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
print_menu(menu);
break;
case 2:
add_dish(menu);
break;
case 3:
modify_dish(menu);
break;
case 4:
delete_dish(menu);
break;
case 5:
place_order(orders, &order_count, current_user, menu);
break;
case 6:
view_orders(orders, order_count);
break;
case 7:
return 0;
default:
printf("Invalid choice!\n");
}
}
return 0;
}
```
该代码实现了用户注册登录、菜品管理和订单管理等功能,包括添加、修改、删除菜品信息,查看菜单,下单和查看订单等。需要注意的是,该代码只是一个简单的示例,实际的订餐管理系统需要更加完善和复杂的设计。