用c语言编写一个程序模拟餐馆点餐系统。 1)用户分为管理员和用餐者两类,分别显示不同文本格式菜单,通过菜单项对应数字进行选择。 2)用餐者菜单包括菜品分类(大类)、菜品列表(含每道菜品的构成信息)、点餐、付费、查询等功能。 3)管理员菜单包括菜品信息和用餐者信息录入、修改和删除。菜品信息至少应包括:编号、菜名、类别、价格等;用餐者信息至少应包括:编号、姓名、点餐信息、付费金额、用餐状态(就餐或打包)等。可根据菜品名称或编号进行菜品构成查询;可查询用餐者点餐情况、费用情况;可统计菜品的日点餐量、日营业额等(选做)。 命令行参数如下: Restb –a (-u) xxxx 第一个参数为可执行程序名称;第二个参数为用户身份,-a表示管理员,-u表示用餐者;第三个参数为用餐者名称,请写出详细框架
时间: 2023-06-10 10:07:15 浏览: 268
下面是一个简单的框架:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct menu_item {
int id;
char name[50];
char category[20];
double price;
struct menu_item *next;
} MenuItem;
typedef struct order_item {
int id;
char name[50];
int quantity;
double price;
struct order_item *next;
} OrderItem;
typedef struct customer {
int id;
char name[50];
OrderItem *order_head;
double total_price;
char status[10];
struct customer *next;
} Customer;
void print_menu();
void print_admin_menu();
void print_customer_menu();
MenuItem *add_menu_item(MenuItem *head, int id, char *name, char *category, double price);
void print_menu_items(MenuItem *head);
MenuItem *find_menu_item(MenuItem *head, char *name);
void delete_menu_item(MenuItem *head, char *name);
void add_customer(Customer *head, char *name);
Customer *find_customer(Customer *head, char *name);
void add_order(Customer *head, char *name, int id, char *item_name, int quantity);
void print_order(Customer *head, char *name);
void print_customer_list(Customer *head);
void calculate_statistics(MenuItem *menu_head, Customer *customer_head);
int main(int argc, char *argv[]) {
// 解析命令行参数,确定用户身份和名称
char *user_type = NULL;
char *user_name = NULL;
if (argc < 3) {
printf("Usage: %s -a/-u user_name\n", argv[0]);
exit(EXIT_FAILURE);
}
user_type = argv[1];
user_name = argv[2];
// 初始化菜单
MenuItem *menu_head = NULL;
menu_head = add_menu_item(menu_head, 1, "鱼香肉丝", "川菜", 26.0);
menu_head = add_menu_item(menu_head, 2, "宫保鸡丁", "川菜", 24.0);
menu_head = add_menu_item(menu_head, 3, "红烧肉", "家常菜", 28.0);
menu_head = add_menu_item(menu_head, 4, "糖醋排骨", "家常菜", 30.0);
menu_head = add_menu_item(menu_head, 5, "炒青菜", "素菜", 16.0);
menu_head = add_menu_item(menu_head, 6, "鱼香茄子", "素菜", 18.0);
// 初始化顾客列表
Customer *customer_head = NULL;
add_customer(customer_head, "张三");
add_order(customer_head, "张三", 1, "鱼香肉丝", 2);
add_order(customer_head, "张三", 3, "红烧肉", 1);
add_customer(customer_head, "李四");
add_order(customer_head, "李四", 2, "宫保鸡丁", 1);
add_order(customer_head, "李四", 6, "鱼香茄子", 2);
// 根据用户身份显示不同的菜单
if (strcmp(user_type, "-a") == 0) {
print_admin_menu();
// 管理员菜单
// ...
} else if (strcmp(user_type, "-u") == 0) {
print_customer_menu();
// 用餐者菜单
// ...
} else {
printf("Invalid user type: %s\n", user_type);
exit(EXIT_FAILURE);
}
// 释放内存
// ...
return 0;
}
void print_menu() {
printf("========== 菜单 ==========\n");
printf("%-5s%-20s%-10s%-10s\n", "编号", "名称", "分类", "价格");
printf("--------------------------\n");
// 输出菜单项
printf("\n");
}
void print_admin_menu() {
printf("========== 管理员菜单 ==========\n");
printf("1. 添加菜品\n");
printf("2. 修改菜品\n");
printf("3. 删除菜品\n");
printf("4. 添加用餐者\n");
printf("5. 删除用餐者\n");
printf("6. 查询菜品构成\n");
printf("7. 查询用餐者点餐情况\n");
printf("8. 查询营业额\n");
printf("9. 退出\n\n");
}
void print_customer_menu() {
printf("========== 用餐者菜单 ==========\n");
printf("1. 查看菜单\n");
printf("2. 点餐\n");
printf("3. 查看订单\n");
printf("4. 修改订单\n");
printf("5. 删除订单\n");
printf("6. 结算\n");
printf("7. 退出\n\n");
}
MenuItem *add_menu_item(MenuItem *head, int id, char *name, char *category, double price) {
// 创建新的菜单项节点
MenuItem *new_item = (MenuItem *)malloc(sizeof(MenuItem));
new_item->id = id;
strcpy(new_item->name, name);
strcpy(new_item->category, category);
new_item->price = price;
new_item->next = NULL;
// 将新的菜单项节点插入链表
if (head == NULL) {
head = new_item;
} else {
MenuItem *p = head;
while (p->next != NULL) {
p = p->next;
}
p->next = new_item;
}
return head;
}
void print_menu_items(MenuItem *head) {
MenuItem *p = head;
while (p != NULL) {
printf("%-5d%-20s%-10s%-10.2lf\n", p->id, p->name, p->category, p->price);
p = p->next;
}
}
MenuItem *find_menu_item(MenuItem *head, char *name) {
MenuItem *p = head;
while (p != NULL) {
if (strcmp(p->name, name) == 0) {
return p;
}
p = p->next;
}
return NULL;
}
void delete_menu_item(MenuItem *head, char *name) {
// 找到要删除的节点
MenuItem *p = head;
MenuItem *prev = NULL;
while (p != NULL) {
if (strcmp(p->name, name) == 0) {
break;
}
prev = p;
p = p->next;
}
// 删除节点
if (p != NULL) {
if (prev == NULL) {
head = p->next;
} else {
prev->next = p->next;
}
free(p);
}
}
void add_customer(Customer *head, char *name) {
// 创建新的顾客节点
Customer *new_customer = (Customer *)malloc(sizeof(Customer));
new_customer->id = rand();
strcpy(new_customer->name, name);
new_customer->order_head = NULL;
new_customer->total_price = 0.0;
strcpy(new_customer->status, "就餐");
new_customer->next = NULL;
// 将新的顾客节点插入链表
if (head == NULL) {
head = new_customer;
} else {
Customer *p = head;
while (p->next != NULL) {
p = p->next;
}
p->next = new_customer;
}
}
Customer *find_customer(Customer *head, char *name) {
Customer *p = head;
while (p != NULL) {
if (strcmp(p->name, name) == 0) {
return p;
}
p = p->next;
}
return NULL;
}
void add_order(Customer *head, char *name, int id, char *item_name, int quantity) {
// 找到对应的顾客节点和菜单项节点
Customer *customer = find_customer(head, name);
MenuItem *item = find_menu_item(menu_head, item_name);
// 创建新的订单项节点
OrderItem *new_order_item = (OrderItem *)malloc(sizeof(OrderItem));
new_order_item->id = id;
strcpy(new_order_item->name, item_name);
new_order_item->quantity = quantity;
new_order_item->price = item->price * quantity;
new_order_item->next = NULL;
// 将新的订单项节点插入链表
if (customer->order_head == NULL) {
customer->order_head = new_order_item;
} else {
OrderItem *p = customer->order_head;
while (p->next != NULL) {
p = p->next;
}
p->next = new_order_item;
}
// 更新顾客的总消费金额
customer->total_price += new_order_item->price;
}
void print_order(Customer *head, char *name) {
Customer *customer = find_customer(head, name);
if (customer == NULL) {
printf("顾客 %s 不存在\n", name);
return;
}
printf("========== %s 的订单 ==========\n", name);
printf("%-5s%-20s%-10s%-10s\n", "编号", "名称", "数量", "价格");
printf("--------------------------\n");
OrderItem *p = customer->order_head;
while (p != NULL) {
printf("%-5d%-20s%-10d%-10.2lf\n", p->id, p->name, p->quantity, p->price);
p = p->next;
}
printf("总金额:%.2lf\n\n", customer->total_price);
}
void print_customer_list(Customer *head) {
printf("========== 用餐者列表 ==========\n");
printf("%-5s%-20s%-10s%-10s%-10s\n", "编号", "姓名", "消费金额", "状态", "订单数量");
printf("---------------------------------\n");
Customer *p = head;
while (p != NULL) {
printf("%-5d%-20s%-10.2lf%-10s%-10d\n", p->id, p->name, p->total_price, p->status, count_orders(p->order_head));
p = p->next;
}
printf("\n");
}
void calculate_statistics(MenuItem *menu_head, Customer *customer_head) {
// 统计菜品的日点餐量和营业额
MenuItem *p = menu_head;
while (p != NULL) {
int count = count_orders_by_item(customer_head, p->name);
double revenue = count * p->price;
printf("%s 点餐量:%d,营业额:%.2lf\n", p->name, count, revenue);
p = p->next;
}
// 统计总营业额
double total_revenue = calculate_total_revenue(customer_head);
printf("总营业额:%.2lf\n", total_revenue);
}
```
阅读全文