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