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