#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// 定义珠宝结构体
typedef struct {
char name[50];
float price;
int quantity;
char material[20]; // 珠宝材料,如金、银、钻石等
} Jewel;
// 显示菜单
void showMenu() {
printf(“1. 添加珠宝\n”);
printf(“2. 销售珠宝\n”);
printf(“3. 查看库存\n”);
printf(“4. 修改珠宝信息\n”);
printf(“5. 退出\n”);
}
// 添加珠宝
void addJewel(Jewel *jewels, int *numJewels) {
Jewel newJewel;
printf(“请输入珠宝名称: “);
scanf(”%s”, newJewel.name);
printf(“请输入珠宝价格: “);
scanf(”%f”, &newJewel.price);
printf(“请输入珠宝数量: “);
scanf(”%d”, &newJewel.quantity);
printf(“请输入珠宝材料: “);
scanf(”%s”, newJewel.material);
jewels[*numJewels] = newJewel;
(*numJewels)++;
}
// 销售珠宝
void sellJewel(Jewel *jewels, int numJewels) {
int choice;
printf(“请选择要销售的珠宝编号: “);
scanf(”%d”, &choice);
if (choice >= 1 && choice <= numJewels) {
int quantityToSell;
printf("请输入要销售的数量: ");
scanf("%d", &quantityToSell);
if (quantityToSell <= jewels[choice - 1].quantity) {
jewels[choice - 1].quantity -= quantityToSell;
printf("销售成功!\n");
} else {
printf("库存不足,销售失败!\n");
}
} else {
printf("无效的选择!\n");
}
}
// 查看库存
void viewInventory(Jewel *jewels, int numJewels) {
printf(“库存列表:\n”);
for (int i = 0; i < numJewels; i++) {
printf("%d. %s - 价格: %.2f - 数量: %d - 材料: %s\n", i + 1, jewels[i].name, jewels[i].price, jewels[i].quantity, jewels[i].material);
}
}
// 修改珠宝信息
void modifyJewel(Jewel *jewels, int numJewels) {
int choice;
printf(“请选择要修改的珠宝编号: “);
scanf(”%d”, &choice);
if (choice >= 1 && choice <= numJewels) {
printf("请输入新的珠宝名称(按 Enter 键保持不变): ");
char newName[50];
scanf("%s", newName);
if (strlen(newName) > 0) {
strcpy(jewels[choice - 1].name, newName);
}
printf("请输入新的珠宝价格(按 Enter 键保持不变): ");
float newPrice;
scanf("%f", &newPrice);
if (newPrice!= 0) {
jewels[choice - 1].price = newPrice;
}
printf("请输入新的珠宝数量(按 Enter 键保持不变): ");
int newQuantity;
scanf("%d", &newQuantity);
if (newQuantity!= 0) {
jewels[choice - 1].quantity = newQuantity;
}
printf("请输入新的珠宝材料(按 Enter 键保持不变): ");
char newMaterial[20];
scanf("%s", newMaterial);
if (strlen(newMaterial) > 0) {
strcpy(jewels[choice - 1].material, newMaterial);
}
printf("修改成功!\n");
} else {
printf("无效的选择!\n");
}
}
int main() {
Jewel jewels[100]; // 假设最多存储 100 种珠宝
int numJewels = 0;
int choice;
do {
showMenu();
printf("请选择操作: ");
scanf("%d", &choice);
switch (choice) {
case 1:
addJewel(jewels, &numJewels);
break;
case 2:
sellJewel(jewels, numJewels);
break;
case 3:
viewInventory(jewels, numJewels);
break;
case 4:
modifyJewel(jewels, numJewels);
break;
case 5:
printf("退出程序\n");
break;
default:
printf("无效的选择,请重新输入!\n");
}
} while (choice!= 5);
return 0;
}