c语言从文本中读出菜品信息,链表实现通过查询点餐,修改订单,删除订单,计算总价
时间: 2023-05-29 08:01:56 浏览: 102
这里提供一个简单的代码示例,实现从文本文件中读取菜品信息,并使用链表实现点餐、修改订单、删除订单、计算总价等功能。
菜品信息包括名称、价格、数量,存储在一个文本文件dish.txt中,每行包括名称、价格、数量,用空格分隔。如下所示:
红烧肉 20 10
麻辣肥牛 30 5
鱼香肉丝 25 8
宫保鸡丁 22 12
...
代码实现如下:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_LEN 50
// 菜品信息结构体
typedef struct dish_info
{
char name[MAX_LEN]; // 名称
float price; // 价格
int num; // 数量
struct dish_info* next; // 下一个节点指针
}DishInfo;
// 读取菜品信息
DishInfo* read_dish_info()
{
DishInfo* head = NULL;
DishInfo* tail = NULL;
FILE* fp = fopen("dish.txt", "r");
if(fp == NULL)
{
printf("Failed to open file\n");
exit(-1);
}
char buffer[MAX_LEN];
while(fgets(buffer, MAX_LEN, fp) != NULL)
{
// 解析每行的信息
char* name = strtok(buffer, " ");
float price = atof(strtok(NULL, " "));
int num = atoi(strtok(NULL, " "));
// 创建节点并加入链表中
DishInfo* temp = (DishInfo*)malloc(sizeof(DishInfo));
strcpy(temp->name, name);
temp->price = price;
temp->num = num;
temp->next = NULL;
if(head == NULL)
{
head = temp;
tail = temp;
}
else
{
tail->next = temp;
tail = temp;
}
}
fclose(fp);
return head;
}
// 打印菜单
void print_menu(DishInfo* head)
{
DishInfo* node = head;
printf("================= Menu =================\n");
while(node != NULL)
{
printf("%s\t%.2f\t%d\n", node->name, node->price, node->num);
node = node->next;
}
printf("=========================================\n");
}
// 查找菜品
DishInfo* find_dish(DishInfo* head, char* name)
{
DishInfo* node = head;
while(node != NULL)
{
if(strcmp(node->name, name) == 0)
{
return node;
}
node = node->next;
}
return NULL;
}
// 点餐
void order(DishInfo* head)
{
print_menu(head);
char name[MAX_LEN];
printf("Enter the dish name: ");
scanf("%s", name);
DishInfo* dish = find_dish(head, name);
if(dish == NULL)
{
printf("Dish not found!\n");
return;
}
int num = 0;
printf("Enter the quantity: ");
scanf("%d", &num);
if(num > dish->num)
{
printf("Not enough dishes, please choose another one or reduce the quantity!\n");
return;
}
dish->num -= num;
printf("Order success!\n");
}
// 修改订单
void modify_order(DishInfo* head)
{
char name[MAX_LEN];
printf("Enter the dish name to be modified: ");
scanf("%s", name);
DishInfo* dish = find_dish(head, name);
if(dish == NULL)
{
printf("Dish not found!\n");
return;
}
int num = 0;
printf("Enter the new quantity: ");
scanf("%d", &num);
if(num > dish->num)
{
printf("Not enough dishes, please choose another one or reduce the quantity!\n");
return;
}
dish->num -= num;
printf("Modify success!\n");
}
// 删除订单
void delete_order(DishInfo* head)
{
char name[MAX_LEN];
printf("Enter the dish name to be deleted: ");
scanf("%s", name);
DishInfo* node = head;
DishInfo* prev = NULL;
while(node != NULL)
{
if(strcmp(node->name, name) == 0)
{
if(prev == NULL)
{
head = node->next;
}
else
{
prev->next = node->next;
}
free(node);
printf("Delete success!\n");
return;
}
prev = node;
node = node->next;
}
printf("Dish not found!\n");
}
// 计算总价
void calculate_total_price(DishInfo* head)
{
float total_price = 0.0f;
DishInfo* node = head;
while(node != NULL)
{
total_price += node->price * node->num;
node = node->next;
}
printf("Total price: %.2f\n", total_price);
}
// 主函数
int main()
{
DishInfo* head = read_dish_info();
// 测试读取菜品信息
/*
DishInfo* node = head;
while(node != NULL)
{
printf("%s\t%.2f\t%d\n", node->name, node->price, node->num);
node = node->next;
}
*/
// 测试点餐
/*
order(head);
print_menu(head);
*/
// 测试修改订单
/*
modify_order(head);
print_menu(head);
*/
// 测试删除订单
/*
delete_order(head);
print_menu(head);
*/
// 测试计算总价
/*
calculate_total_price(head);
*/
int choice = 0;
while(1)
{
printf("=========== Ordering System ============\n");
printf("1. Print menu\n");
printf("2. Order\n");
printf("3. Modify order\n");
printf("4. Delete order\n");
printf("5. Calculate total price\n");
printf("6. Exit\n");
printf("=======================================\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch(choice)
{
case 1:
print_menu(head);
break;
case 2:
order(head);
break;
case 3:
modify_order(head);
break;
case 4:
delete_order(head);
break;
case 5:
calculate_total_price(head);
break;
case 6:
exit(0);
default:
printf("Invalid choice!\n");
break;
}
}
return 0;
}