#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// 定义花卉结构体
typedef struct {
char name[50];
float price;
int quantity;
} Flower;
// 显示菜单
void showMenu() {
printf(“1. 添加花卉\n”);
printf(“2. 销售花卉\n”);
printf(“3. 查看库存\n”);
printf(“4. 退出\n”);
}
// 添加花卉
void addFlower(Flower *flowers, int *numFlowers) {
Flower newFlower;
printf(“请输入花卉名称: “);
scanf(”%s”, newFlower.name);
printf(“请输入花卉价格: “);
scanf(”%f”, &newFlower.price);
printf(“请输入花卉数量: “);
scanf(”%d”, &newFlower.quantity);
flowers[*numFlowers] = newFlower;
(*numFlowers)++;
}
// 销售花卉
void sellFlower(Flower *flowers, int numFlowers) {
int choice;
printf(“请选择要销售的花卉编号: “);
scanf(”%d”, &choice);
if (choice >= 1 && choice <= numFlowers) {
int quantityToSell;
printf("请输入要销售的数量: ");
scanf("%d", &quantityToSell);
if (quantityToSell <= flowers[choice - 1].quantity) {
flowers[choice - 1].quantity -= quantityToSell;
printf("销售成功!\n");
} else {
printf("库存不足,销售失败!\n");
}
} else {
printf("无效的选择!\n");
}
}
// 查看库存
void viewInventory(Flower *flowers, int numFlowers) {
printf(“库存列表:\n”);
for (int i = 0; i < numFlowers; i++) {
printf("%d. %s - 价格: %.2f - 数量: %d\n", i + 1, flowers[i].name, flowers[i].price, flowers[i].quantity);
}
}
int main() {
Flower flowers[100]; // 假设最多存储 100 种花卉
int numFlowers = 0;
int choice;
do {
showMenu();
printf("请选择操作: ");
scanf("%d", &choice);
switch (choice) {
case 1:
addFlower(flowers, &numFlowers);
break;
case 2:
sellFlower(flowers, numFlowers);
break;
case 3:
viewInventory(flowers, numFlowers);
break;
case 4:
printf("退出程序\n");
break;
default:
printf("无效的选择,请重新输入!\n");
}
} while (choice!= 4);
return 0;
}