帮我C语言写一个商品销售系统
时间: 2023-08-16 22:09:18 浏览: 81
好的,这是一个简单的商品销售系统的C语言代码示例:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_PRODUCTS 100
struct product {
char name[20];
int stock;
float price;
};
struct product products[MAX_PRODUCTS];
int num_products = 0;
void add_product() {
if (num_products == MAX_PRODUCTS) {
printf("无法添加更多商品!\n");
return;
}
struct product p;
printf("请输入商品名称:");
scanf("%s", p.name);
printf("请输入商品库存:");
scanf("%d", &p.stock);
printf("请输入商品价格:");
scanf("%f", &p.price);
products[num_products++] = p;
printf("商品添加成功!\n");
}
void list_products() {
printf("%-20s%-10s%-10s\n", "名称", "库存", "价格");
for (int i = 0; i < num_products; i++) {
printf("%-20s%-10d%-10.2f\n", products[i].name, products[i].stock, products[i].price);
}
}
void sell_product() {
char name[20];
printf("请输入要出售的商品名称:");
scanf("%s", name);
for (int i = 0; i < num_products; i++) {
if (strcmp(products[i].name, name) == 0) {
if (products[i].stock == 0) {
printf("商品库存不足,无法出售!\n");
return;
}
products[i].stock--;
printf("商品出售成功!\n");
return;
}
}
printf("未找到此商品!\n");
}
int main() {
int choice;
do {
printf("请选择操作:\n");
printf("1. 添加商品\n");
printf("2. 查看商品\n");
printf("3. 出售商品\n");
printf("4. 退出\n");
printf("请输入选项编号:");
scanf("%d", &choice);
switch (choice) {
case 1:
add_product();
break;
case 2:
list_products();
break;
case 3:
sell_product();
break;
case 4:
printf("已退出!\n");
break;
default:
printf("无效的选项!\n");
break;
}
} while (choice != 4);
return 0;
}
```
这个销售系统支持添加商品、查看商品和出售商品等功能。你可以根据需要进行修改和扩展。
阅读全文