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