#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// 定义玩具结构体
typedef struct {
char name[50];
float price;
int stock;
int sales;
} Toy;
// 显示菜单
void showMenu() {
printf(“1. 添加玩具\n”);
printf(“2. 查看玩具信息\n”);
printf(“3. 修改玩具信息\n”);
printf(“4. 销售玩具\n”);
printf(“5. 退出\n”);
}
// 添加玩具
void addToy(Toy *toys, int *count) {
Toy newToy;
printf(“请输入玩具名称:”);
scanf("%s", newToy.name);
printf(“请输入玩具价格:”);
scanf("%f", &newToy.price);
printf(“请输入玩具库存数量:”);
scanf("%d", &newToy.stock);
newToy.sales = 0;
toys[*count] = newToy;
(*count)++;
}
// 查看玩具信息
void viewToys(Toy *toys, int count) {
printf(“玩具信息如下:\n”);
for (int i = 0; i < count; i++) {
printf(“名称:%s 价格:%.2f 库存:%d 销售量:%d\n”, toys[i].name, toys[i].price, toys[i].stock, toys[i].sales);
}
}
// 修改玩具信息
void modifyToy(Toy *toys, int count) {
int choice;
printf(“请输入要修改的玩具序号(从 1 开始):”);
scanf("%d", &choice);
if (choice >= 1 && choice <= count) {
printf("请输入新的玩具名称:");
scanf("%s", toys[choice - 1].name);
printf("请输入新的玩具价格:");
scanf("%f", &toys[choice - 1].price);
printf("请输入新的玩具库存数量:");
scanf("%d", &toys[choice - 1].stock);
} else {
printf("无效的序号!\n");
}
}
// 销售玩具
void sellToy(Toy *toys, int count) {
int choice;
int quantity;
printf(“请输入要销售的玩具序号(从 1 开始):”);
scanf("%d", &choice);
if (choice >= 1 && choice <= count) {
printf("请输入销售数量:");
scanf("%d", &quantity);
if (quantity <= toys[choice - 1].stock) {
toys[choice - 1].stock -= quantity;
toys[choice - 1].sales += quantity;
printf("销售成功!\n");
} else {
printf("库存不足!\n");
}
} else {
printf("无效的序号!\n");
}
}
int main() {
Toy toys[100];
int count = 0;
int choice;
do {
showMenu();
printf("请选择操作:");
scanf("%d", &choice);
switch (choice) {
case 1:
addToy(toys, &count);
break;
case 2:
viewToys(toys, count);
break;
case 3:
modifyToy(toys, count);
break;
case 4:
sellToy(toys, count);
break;
case 5:
printf("退出系统!\n");
break;
default:
printf("无效的选择,请重新输入!\n");
}
} while (choice!= 5);
return 0;
}