#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// 定义服装结构体
typedef struct {
char name[50];
int size; // 假设尺码为整数,如 1: S, 2: M, 3: L 等
float price;
int quantity;
} Clothing;
// 显示菜单
void showMenu() {
printf(“1. 添加服装\n”);
printf(“2. 销售服装\n”);
printf(“3. 查看库存\n”);
printf(“4. 修改服装信息\n”);
printf(“5. 退出\n”);
}
// 添加服装
void addClothing(Clothing *clothes, int *numClothes) {
Clothing newClothing;
printf(“请输入服装名称: “);
scanf(”%s”, newClothing.name);
printf(“请输入服装尺码(1: S, 2: M, 3: L 等): “);
scanf(”%d”, &newClothing.size);
printf(“请输入服装价格: “);
scanf(”%f”, &newClothing.price);
printf(“请输入服装数量: “);
scanf(”%d”, &newClothing.quantity);
clothes[*numClothes] = newClothing;
(*numClothes)++;
}
// 销售服装
void sellClothing(Clothing *clothes, int numClothes) {
int choice;
printf(“请选择要销售的服装编号: “);
scanf(”%d”, &choice);
if (choice >= 1 && choice <= numClothes) {
int quantityToSell;
printf("请输入要销售的数量: ");
scanf("%d", &quantityToSell);
if (quantityToSell <= clothes[choice - 1].quantity) {
clothes[choice - 1].quantity -= quantityToSell;
printf("销售成功!\n");
} else {
printf("库存不足,销售失败!\n");
}
} else {
printf("无效的选择!\n");
}
}
// 查看库存
void viewInventory(Clothing *clothes, int numClothes) {
printf(“库存列表:\n”);
for (int i = 0; i < numClothes; i++) {
printf("%d. %s - 尺码: %d - 价格: %.2f - 数量: %d\n", i + 1, clothes[i].name, clothes[i].size, clothes[i].price, clothes[i].quantity);
}
}
// 修改服装信息
void modifyClothing(Clothing *clothes, int numClothes) {
int choice;
printf(“请选择要修改的服装编号: “);
scanf(”%d”, &choice);
if (choice >= 1 && choice <= numClothes) {
printf("请输入新的服装名称(按 Enter 键保持不变): ");
char newName[50];
scanf("%s", newName);
if (strlen(newName) > 0) {
strcpy(clothes[choice - 1].name, newName);
}
printf("请输入新的服装尺码(按 Enter 键保持不变): ");
int newSize;
scanf("%d", &newSize);
if (newSize!= 0) {
clothes[choice - 1].size = newSize;
}
printf("请输入新的服装价格(按 Enter 键保持不变): ");
float newPrice;
scanf("%f", &newPrice);
if (newPrice!= 0) {
clothes[choice - 1].price = newPrice;
}
printf("请输入新的服装数量(按 Enter 键保持不变): ");
int newQuantity;
scanf("%d", &newQuantity);
if (newQuantity!= 0) {
clothes[choice - 1].quantity = newQuantity;
}
printf("修改成功!\n");
} else {
printf("无效的选择!\n");
}
}
int main() {
Clothing clothes[100]; // 假设最多存储 100 种服装
int numClothes = 0;
int choice;
do {
showMenu();
printf("请选择操作: ");
scanf("%d", &choice);
switch (choice) {
case 1:
addClothing(clothes, &numClothes);
break;
case 2:
sellClothing(clothes, numClothes);
break;
case 3:
viewInventory(clothes, numClothes);
break;
case 4:
modifyClothing(clothes, numClothes);
break;
case 5:
printf("退出程序\n");
break;
default:
printf("无效的选择,请重新输入!\n");
}
} while (choice!= 5);
return 0;
}