C语言模拟用户从文本文件中得到点餐信息点餐,修改订单,删除订单,计算总价和余额并输出到文本
时间: 2023-05-31 17:01:29 浏览: 72
文件中。
#include <stdio.h>
#include <string.h>
#define MAX_ORDER 100 // 最大订单数
#define MAX_NAME 20 // 最大菜名长度
struct Order { // 订单结构体
char name[MAX_NAME]; // 菜名
int price; // 单价
int num; // 数量
};
void showMenu(); // 显示菜单
int getOrder(struct Order orders[]); // 获取订单
void modifyOrder(struct Order orders[], int count); // 修改订单
void deleteOrder(struct Order orders[], int count); // 删除订单
void calculateTotal(struct Order orders[], int count); // 计算总价和余额
int main() {
struct Order orders[MAX_ORDER]; // 订单数组
int count = 0; // 订单数量
while (1) {
showMenu(); // 显示菜单
int choice;
scanf("%d", &choice);
switch (choice) {
case 1:
count = getOrder(orders); // 获取订单
break;
case 2:
modifyOrder(orders, count); // 修改订单
break;
case 3:
deleteOrder(orders, count); // 删除订单
break;
case 4:
calculateTotal(orders, count); // 计算总价和余额
break;
case 5:
return 0; // 退出程序
default:
printf("无效的选择!\n");
}
}
return 0;
}
void showMenu() {
printf("请选择操作:\n");
printf("1. 点餐\n");
printf("2. 修改订单\n");
printf("3. 删除订单\n");
printf("4. 计算总价和余额\n");
printf("5. 退出程序\n");
}
int getOrder(struct Order orders[]) {
printf("请输入订单数量(最大%d):\n", MAX_ORDER);
int count;
scanf("%d", &count);
if (count <= 0 || count > MAX_ORDER) {
printf("订单数量无效!\n");
return 0;
}
for (int i = 0; i < count; i++) {
printf("请输入第%d个菜品名称:\n", i + 1);
scanf("%s", orders[i].name);
printf("请输入第%d个菜品单价:\n", i + 1);
scanf("%d", &orders[i].price);
printf("请输入第%d个菜品数量:\n", i + 1);
scanf("%d", &orders[i].num);
}
printf("订单已保存!\n");
return count;
}
void modifyOrder(struct Order orders[], int count) {
if (count <= 0) {
printf("当前没有订单!\n");
return;
}
printf("请输入要修改的订单的菜品名称:\n");
char name[MAX_NAME];
scanf("%s", name);
int index = -1;
for (int i = 0; i < count; i++) {
if (strcmp(orders[i].name, name) == 0) {
index = i;
break;
}
}
if (index == -1) {
printf("找不到指定的订单!\n");
return;
}
printf("请输入新的菜品名称(不修改请直接回车):\n");
char newName[MAX_NAME];
scanf("%s", newName);
if (strlen(newName) > 0) {
strcpy(orders[index].name, newName);
}
printf("请输入新的菜品单价(不修改请输-1):\n");
int newPrice;
scanf("%d", &newPrice);
if (newPrice != -1) {
orders[index].price = newPrice;
}
printf("请输入新的菜品数量(不修改请输-1):\n");
int newNum;
scanf("%d", &newNum);
if (newNum != -1) {
orders[index].num = newNum;
}
printf("订单已修改!\n");
}
void deleteOrder(struct Order orders[], int count) {
if (count <= 0) {
printf("当前没有订单!\n");
return;
}
printf("请输入要删除的订单的菜品名称:\n");
char name[MAX_NAME];
scanf("%s", name);
int index = -1;
for (int i = 0; i < count; i++) {
if (strcmp(orders[i].name, name) == 0) {
index = i;
break;
}
}
if (index == -1) {
printf("找不到指定的订单!\n");
return;
}
for (int i = index; i < count - 1; i++) {
orders[i] = orders[i + 1];
}
printf("订单已删除!\n");
}
void calculateTotal(struct Order orders[], int count) {
if (count <= 0) {
printf("当前没有订单!\n");
return;
}
int total = 0;
for (int i = 0; i < count; i++) {
total += orders[i].price * orders[i].num;
}
printf("总价:%d\n", total);
printf("请输入余额:\n");
int balance;
scanf("%d", &balance);
if (balance < total) {
printf("余额不足!\n");
return;
}
printf("找零:%d\n", balance - total);
FILE *fp;
fp = fopen("order.txt", "w");
if (fp == NULL) {
printf("无法打开文件!\n");
return;
}
fprintf(fp, "菜名\t单价\t数量\n");
for (int i = 0; i < count; i++) {
fprintf(fp, "%s\t%d\t%d\n", orders[i].name, orders[i].price, orders[i].num);
}
fprintf(fp, "总价:%d\n", total);
fprintf(fp, "余额:%d\n", balance - total);
fclose(fp);
printf("订单已保存到文件!\n");
}
阅读全文