C语言用链表实现顾客根据菜品信息完成对订单的增删改查,并输出txt文件
时间: 2023-05-22 19:05:53 浏览: 222
这个问题可以实现。以下是代码示例:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct dish {
int dish_id;
char dish_name[100];
double dish_price;
} Dish;
typedef struct order {
int order_id;
char customer_name[100];
Dish dish_list[100];
int dish_count;
} Order;
typedef struct node {
Order data;
struct node* next;
} Node;
void print_dish(const Dish* dish) {
printf("%d,%s,%.2f\n", dish->dish_id, dish->dish_name, dish->dish_price);
}
void print_order(const Order* order) {
printf("%d,%s\n", order->order_id, order->customer_name);
for (int i = 0; i < order->dish_count; i++) {
print_dish(&order->dish_list[i]);
}
}
void print_order_list(const Node* head) {
while (head != NULL) {
print_order(&head->data);
head = head->next;
}
}
Order* create_order(const char* customer_name) {
Order* order = (Order*)malloc(sizeof(Order));
order->order_id = rand() % 100000;
strcpy(order->customer_name, customer_name);
order->dish_count = 0;
return order;
}
void add_dish_to_order(Order* order, const Dish* dish) {
order->dish_list[order->dish_count] = *dish;
order->dish_count++;
}
void remove_dish_from_order(Order* order, const Dish* dish) {
int i = 0;
while (i < order->dish_count) {
if (memcmp(&order->dish_list[i], dish, sizeof(Dish)) == 0) {
break;
}
i++;
}
if (i < order->dish_count) {
for (int j = i + 1; j < order->dish_count; j++) {
order->dish_list[j - 1] = order->dish_list[j];
}
order->dish_count--;
}
}
void modify_dish_in_order(Order* order, const Dish* old_dish, const Dish* new_dish) {
int i = 0;
while (i < order->dish_count) {
if (memcmp(&order->dish_list[i], old_dish, sizeof(Dish)) == 0) {
break;
}
i++;
}
if (i < order->dish_count) {
order->dish_list[i] = *new_dish;
}
}
void save_order_list(const Node* head, const char* filename) {
FILE* fp = fopen(filename, "w");
while (head != NULL) {
fprintf(fp, "%d,%s\n", head->data.order_id, head->data.customer_name);
for (int i = 0; i < head->data.dish_count; i++) {
fprintf(fp, "%d,%s,%.2f\n", head->data.dish_list[i].dish_id, head->data.dish_list[i].dish_name, head->data.dish_list[i].dish_price);
}
head = head->next;
}
fclose(fp);
}
void load_order_list(Node** head, const char* filename) {
FILE* fp = fopen(filename, "r");
while (1) {
Order order;
if (fscanf(fp, "%d,%[^,\n],", &order.order_id, order.customer_name) != 2) {
break;
}
order.dish_count = 0;
while (1) {
Dish dish;
if (fscanf(fp, "%d,%[^,],%lf\n", &dish.dish_id, dish.dish_name, &dish.dish_price) != 3) {
break;
}
add_dish_to_order(&order, &dish);
}
Node* node = (Node*)malloc(sizeof(Node));
node->data = order;
node->next = *head;
*head = node;
}
fclose(fp);
}
int main() {
Node* head = NULL;
// create orders
Order* order1 = create_order("Alice");
Dish dish11 = { 1, "Apple", 3.0 };
add_dish_to_order(order1, &dish11);
Dish dish12 = { 2, "Banana", 4.0 };
add_dish_to_order(order1, &dish12);
Node* node1 = (Node*)malloc(sizeof(Node));
node1->data = *order1;
node1->next = NULL;
head = node1;
Order* order2 = create_order("Bob");
Dish dish21 = { 1, "Coke", 2.5 };
add_dish_to_order(order2, &dish21);
Dish dish22 = { 3, "Orange", 4.0 };
add_dish_to_order(order2, &dish22);
Node* node2 = (Node*)malloc(sizeof(Node));
node2->data = *order2;
node2->next = head;
head = node2;
// print orders
print_order_list(head);
// remove a dish from Alice's order
remove_dish_from_order(order1, &dish11);
printf("After removing a dish:\n");
print_order(order1);
// modify a dish in Bob's order
Dish dish23 = { 4, "Peach", 3.5 };
modify_dish_in_order(order2, &dish21, &dish23);
printf("After modifying a dish:\n");
print_order(order2);
// save and load orders
save_order_list(head, "orders.txt");
Node* new_head = NULL;
load_order_list(&new_head, "orders.txt");
printf("After loading orders:\n");
print_order_list(new_head);
return 0;
}
这个程序可以用链表来实现对订单的增删改查,并将订单输出到txt文件。
阅读全文