用c语言编写一个程序模拟餐馆点餐系统。 1)用户分为管理员和用餐者两类,分别显示不同文本格式菜单,通过菜单项对应数字进行选择。 2)用餐者菜单包括菜品分类(大类)、菜品列表(含每道菜品的构成信息)、点餐、付费、查询等功能。 3)管理员菜单包括菜品信息和用餐者信息录入、修改和删除。菜品信息至少应包括:编号、菜名、类别、价格等;用餐者信息至少应包括:编号、姓名、点餐信息、付费金额、用餐状态(就餐或打包)等。可根据菜品名称或编号进行菜品构成查询;可查询用餐者点餐情况、费用情况;可统计菜品的日点餐量、日营业额等(选做)。 命令行参数如下: Restb –a (-u) xxxx 第一个参数为可执行程序名称;第二个参数为用户身份,-a表示管理员,-u表示用餐者;第三个参数为用餐者名称,请写出详细框架
时间: 2023-06-10 20:07:25 浏览: 238
基于C语言实现点餐系统
5星 · 资源好评率100%
以下是一个简单的餐馆点餐系统的C语言实现,包含管理员和用餐者两类用户,并提供了菜品信息和用餐者信息的录入、修改和删除功能,以及查询和统计功能。
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_MENU_ITEMS 100 // 菜单项最大数量
#define MAX_ORDERS 100 // 订单最大数量
#define MAX_NAME_LEN 20 // 名称最大长度
#define MAX_ITEM_NAME_LEN 30 // 菜品名称最大长度
#define MAX_ITEM_DESC_LEN 50 // 菜品描述最大长度
#define MAX_ORDER_ITEMS 10 // 订单中最大菜品数量
// 菜品信息结构体
typedef struct {
int id; // 菜品编号
char name[MAX_ITEM_NAME_LEN]; // 菜品名称
char desc[MAX_ITEM_DESC_LEN]; // 菜品描述
char category[MAX_NAME_LEN]; // 菜品类别
float price; // 菜品价格
} MenuItem;
// 订单菜品信息结构体
typedef struct {
int id; // 菜品编号
int quantity; // 菜品数量
} OrderItem;
// 订单信息结构体
typedef struct {
int id; // 订单编号
char name[MAX_NAME_LEN];// 用餐者姓名
OrderItem items[MAX_ORDER_ITEMS]; // 订单菜品信息
int num_items; // 订单中菜品数量
float total_price; // 订单总金额
int dine_in; // 是否堂食(1表示堂食,0表示打包)
} Order;
// 菜单项结构体
typedef struct {
char text[MAX_ITEM_NAME_LEN]; // 菜单项文本
int number; // 菜单项对应数字
} Menu;
// 菜单项数组
Menu main_menu[] = {
{"点餐", 1},
{"付款", 2},
{"查询订单", 3},
{"退出", 0}
};
Menu admin_menu[] = {
{"添加菜品", 1},
{"修改菜品", 2},
{"删除菜品", 3},
{"查询菜品", 4},
{"查询订单", 5},
{"统计信息", 6},
{"退出", 0}
};
// 菜品数组
MenuItem menu_items[MAX_MENU_ITEMS];
int num_menu_items = 0;
// 订单数组
Order orders[MAX_ORDERS];
int num_orders = 0;
// 显示菜单项
void display_menu(Menu menu[], int num_items)
{
printf("--------------------------------\n");
for (int i = 0; i < num_items; i++) {
printf("%d. %s\n", menu[i].number, menu[i].text);
}
printf("--------------------------------\n");
}
// 显示菜品列表
void display_menu_items()
{
printf("--------------------------------\n");
printf("%-6s%-20s%-20s%-10s\n", "编号", "菜名", "类别", "价格");
printf("--------------------------------\n");
for (int i = 0; i < num_menu_items; i++) {
printf("%-6d%-20s%-20s%-10.2f\n",
menu_items[i].id, menu_items[i].name,
menu_items[i].category, menu_items[i].price);
}
printf("--------------------------------\n");
}
// 显示订单信息
void display_order(Order *order)
{
printf("--------------------------------\n");
printf("订单编号:%d\n", order->id);
printf("用餐者姓名:%s\n", order->name);
printf("菜品列表:\n");
printf("%-6s%-20s%-10s%-10s\n", "编号", "菜名", "数量", "价格");
for (int i = 0; i < order->num_items; i++) {
printf("%-6d%-20s%-10d%-10.2f\n",
order->items[i].id,
menu_items[order->items[i].id - 1].name,
order->items[i].quantity,
menu_items[order->items[i].id - 1].price * order->items[i].quantity);
}
printf("总金额:%.2f\n", order->total_price);
printf("堂食/打包:%s\n", order->dine_in ? "堂食" : "打包");
printf("--------------------------------\n");
}
// 添加菜品
void add_menu_item()
{
if (num_menu_items >= MAX_MENU_ITEMS) {
printf("菜单已满,无法添加菜品。\n");
return;
}
printf("请输入菜品信息:\n");
printf("菜品编号:");
scanf("%d", &menu_items[num_menu_items].id);
printf("菜品名称:");
scanf("%s", menu_items[num_menu_items].name);
printf("菜品描述:");
scanf("%s", menu_items[num_menu_items].desc);
printf("菜品类别:");
scanf("%s", menu_items[num_menu_items].category);
printf("菜品价格:");
scanf("%f", &menu_items[num_menu_items].price);
num_menu_items++;
printf("菜品添加成功!\n");
}
// 修改菜品
void modify_menu_item()
{
int id;
printf("请输入要修改的菜品编号:");
scanf("%d", &id);
int found = 0;
for (int i = 0; i < num_menu_items; i++) {
if (menu_items[i].id == id) {
printf("当前菜品信息为:\n");
printf("菜品编号:%d\n", menu_items[i].id);
printf("菜品名称:%s\n", menu_items[i].name);
printf("菜品描述:%s\n", menu_items[i].desc);
printf("菜品类别:%s\n", menu_items[i].category);
printf("菜品价格:%.2f\n", menu_items[i].price);
printf("请输入新的菜品信息:\n");
printf("菜品名称:");
scanf("%s", menu_items[i].name);
printf("菜品描述:");
scanf("%s", menu_items[i].desc);
printf("菜品类别:");
scanf("%s", menu_items[i].category);
printf("菜品价格:");
scanf("%f", &menu_items[i].price);
printf("菜品修改成功!\n");
found = 1;
break;
}
}
if (!found) {
printf("找不到指定的菜品。\n");
}
}
// 删除菜品
void delete_menu_item()
{
int id;
printf("请输入要删除的菜品编号:");
scanf("%d", &id);
int found = 0;
for (int i = 0; i < num_menu_items; i++) {
if (menu_items[i].id == id) {
for (int j = i; j < num_menu_items - 1; j++) {
menu_items[j] = menu_items[j + 1];
}
num_menu_items--;
printf("菜品删除成功!\n");
found = 1;
break;
}
}
if (!found) {
printf("找不到指定的菜品。\n");
}
}
// 查询菜品
void search_menu_item()
{
char keyword[MAX_NAME_LEN];
printf("请输入要查询的菜品名称或编号:");
scanf("%s", keyword);
int found = 0;
printf("--------------------------------\n");
printf("%-6s%-20s%-20s%-10s\n", "编号", "菜名", "类别", "价格");
printf("--------------------------------\n");
for (int i = 0; i < num_menu_items; i++) {
if (menu_items[i].id == atoi(keyword) ||
strstr(menu_items[i].name, keyword)) {
printf("%-6d%-20s%-20s%-10.2f\n",
menu_items[i].id, menu_items[i].name,
menu_items[i].category, menu_items[i].price);
found = 1;
}
}
printf("--------------------------------\n");
if (!found) {
printf("找不到指定的菜品。\n");
}
}
// 查询订单
void search_order()
{
char keyword[MAX_NAME_LEN];
printf("请输入要查询的用餐者姓名:");
scanf("%s", keyword);
int found = 0;
for (int i = 0; i < num_orders; i++) {
if (strcmp(orders[i].name, keyword) == 0) {
display_order(&orders[i]);
found = 1;
}
}
if (!found) {
printf("找不到指定的订单。\n");
}
}
// 统计信息
void show_statistics()
{
int num_orders_today = 0;
float total_sales_today = 0.0;
int num_orders_dine_in = 0;
float total_sales_dine_in = 0.0;
int num_orders_take_out = 0;
float total_sales_take_out = 0.0;
for (int i = 0; i < num_orders; i++) {
if (orders[i].dine_in) {
num_orders_dine_in++;
total_sales_dine_in += orders[i].total_price;
} else {
num_orders_take_out++;
total_sales_take_out += orders[i].total_price;
}
if (orders[i].id / 10000 == 20220101) {
num_orders_today++;
total_sales_today += orders[i].total_price;
}
}
printf("--------------------------------\n");
printf("当日点餐量:%d\n", num_orders_today);
printf("当日营业额:%.2f\n", total_sales_today);
printf("堂食订单量:%d\n", num_orders_dine_in);
printf("堂食营业额:%.2f\n", total_sales_dine_in);
printf("打包订单量:%d\n", num_orders_take_out);
printf("打包营业额:%.2f\n", total_sales_take_out);
printf("--------------------------------\n");
}
// 点餐
void place_order(char *name)
{
Order order;
order.id = num_orders + 1;
strcpy(order.name, name);
order.num_items = 0;
order.total_price = 0.0;
order.dine_in = 1;
printf("请点餐,输入菜品编号和数量,以0结束:\n");
while (1) {
int id, quantity;
scanf("%d", &id);
if (id == 0) {
break;
}
if (order.num_items >= MAX_ORDER_ITEMS) {
printf("订单中菜品数量已达上限,无法添加菜品。\n");
continue;
}
printf("请输入数量:");
scanf("%d", &quantity);
order.items[order.num_items].id = id;
order.items[order.num_items].quantity = quantity;
order.num_items++;
order.total_price += menu_items[id - 1].price * quantity;
}
printf("请选择用餐方式(1. 堂食,2. 打包):");
int option;
scanf("%d", &option);
if (option == 2) {
order.dine_in = 0;
}
orders[num_orders] = order;
num_orders++;
printf("点餐成功,订单编号为%d。\n", order.id);
}
// 付款
void make_payment(char *name)
{
printf("请输入要付款的订单编号:");
int id;
scanf("%d", &id);
int found = 0;
for (int i = 0; i < num_orders; i++) {
if (orders[i].id == id && strcmp(orders[i].name, name) == 0) {
display_order(&orders[i]);
printf("请支付:%.2f元\n", orders[i].total_price);
printf("付款成功!\n");
found = 1;
break;
}
}
if (!found) {
printf("找不到指定的订单。\n");
}
}
int main(int argc, char *argv[])
{
if (argc != 3) {
printf("用法:%s -a/-u name\n", argv[0]);
printf("选项:\n");
printf(" -a 以管理员身份登录\n");
printf(" -u 以用餐者身份登录\n");
return 1;
}
int is_admin = strcmp(argv[1], "-a") == 0;
char *name = argv[2];
if (is_admin) {
while (1) {
display_menu(admin_menu, sizeof(admin_menu) / sizeof(Menu));
printf("请选择操作:");
int option;
scanf("%d", &option);
switch (option) {
case 1:
add_menu_item();
break;
case 2:
modify_menu_item();
break;
case 3:
delete_menu_item();
break;
case 4:
search_menu_item();
break;
case 5:
search_order();
break;
case 6:
show_statistics();
break;
case 0:
printf("谢谢使用!\n");
return 0;
default:
printf("无效的选项,请重新选择。\n");
break;
}
}
} else {
while (1) {
display_menu(main_menu, sizeof(main_menu) / sizeof(Menu));
printf("请选择操作:");
int option;
scanf("%d", &option);
switch (option) {
case 1:
place_order(name);
break;
case 2:
make_payment(name);
break;
case 3:
search_order();
break;
case 0:
printf("谢谢光临!\n");
return 0;
default:
printf("无效的选项,请重新选择。\n");
break;
}
}
}
return 0;
}
```
阅读全文