c餐厅点餐系统程序设计
时间: 2023-10-30 14:08:12 浏览: 108
C语言编写的餐厅点菜系统
5星 · 资源好评率100%
以下是一个简单的餐厅点餐系统的C语言程序设计,具体实现细节可能会根据实际需求进行调整。
```c
#include <stdio.h>
#include <stdlib.h>
struct MenuItem {
int id;
char name[50];
float price;
};
struct OrderItem {
int id;
char name[50];
int quantity;
float price;
};
void displayMenu(struct MenuItem menu[], int size);
int getItemIndex(struct MenuItem menu[], int size, int id);
void addOrderItem(struct MenuItem menu[], int size, struct OrderItem orders[], int *ordersSize, int id, int quantity);
void displayOrder(struct OrderItem orders[], int size);
void removeOrderItem(struct OrderItem orders[], int *ordersSize, int index);
float calculateTotal(struct OrderItem orders[], int size);
int main() {
int choice;
int ordersSize = 0;
struct MenuItem menu[] = {
{1, "Hamburger", 3.50},
{2, "Cheeseburger", 4.00},
{3, "Fries", 2.00},
{4, "Soda", 1.50}
};
int menuSize = sizeof(menu) / sizeof(struct MenuItem);
struct OrderItem orders[50];
printf("Welcome to our restaurant!\n");
do {
printf("\nPlease select an option:\n");
printf("1. Display menu\n");
printf("2. Add item to order\n");
printf("3. Display order\n");
printf("4. Remove item from order\n");
printf("5. Calculate total\n");
printf("6. Quit\n");
printf("Enter your choice (1-6): ");
scanf("%d", &choice);
switch (choice) {
case 1:
displayMenu(menu, menuSize);
break;
case 2:
int id, quantity;
printf("Enter item ID: ");
scanf("%d", &id);
printf("Enter quantity: ");
scanf("%d", &quantity);
addOrderItem(menu, menuSize, orders, &ordersSize, id, quantity);
break;
case 3:
displayOrder(orders, ordersSize);
break;
case 4:
int index;
printf("Enter item index: ");
scanf("%d", &index);
removeOrderItem(orders, &ordersSize, index);
break;
case 5:
printf("Total: $%.2f\n", calculateTotal(orders, ordersSize));
break;
case 6:
printf("Thank you for visiting our restaurant!\n");
break;
default:
printf("Invalid choice. Please select again.\n");
break;
}
} while (choice != 6);
return 0;
}
void displayMenu(struct MenuItem menu[], int size) {
printf("\nMenu:\n");
for (int i = 0; i < size; i++) {
printf("%d. %s - $%.2f\n", menu[i].id, menu[i].name, menu[i].price);
}
}
int getItemIndex(struct MenuItem menu[], int size, int id) {
for (int i = 0; i < size; i++) {
if (menu[i].id == id) {
return i;
}
}
return -1;
}
void addOrderItem(struct MenuItem menu[], int size, struct OrderItem orders[], int *ordersSize, int id, int quantity) {
int index = getItemIndex(menu, size, id);
if (index == -1) {
printf("Invalid item ID. Please try again.\n");
return;
}
struct MenuItem item = menu[index];
struct OrderItem orderItem = {item.id, item.name, quantity, item.price * quantity};
orders[*ordersSize] = orderItem;
(*ordersSize)++;
printf("Item added to order.\n");
}
void displayOrder(struct OrderItem orders[], int size) {
printf("\nOrder:\n");
for (int i = 0; i < size; i++) {
printf("%d. %s x %d - $%.2f\n", orders[i].id, orders[i].name, orders[i].quantity, orders[i].price);
}
}
void removeOrderItem(struct OrderItem orders[], int *ordersSize, int index) {
if (index < 0 || index >= *ordersSize) {
printf("Invalid item index. Please try again.\n");
return;
}
for (int i = index; i < *ordersSize - 1; i++) {
orders[i] = orders[i + 1];
}
(*ordersSize)--;
printf("Item removed from order.\n");
}
float calculateTotal(struct OrderItem orders[], int size) {
float total = 0.0;
for (int i = 0; i < size; i++) {
total += orders[i].price;
}
return total;
}
```
这个程序实现了一个简单的餐厅点餐系统,包括显示菜单、添加订单、显示订单、删除订单、计算总价等功能。它使用了结构体来表示菜单和订单项,并使用了函数来处理不同的任务,使代码更易于理解和维护。
阅读全文