用c++编写一个进销存系统
时间: 2023-09-18 11:07:18 浏览: 47
一个完整的进销存系统需要包含很多功能,因此这里只提供一个简单的示例程序,以供参考。
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_ITEM_NUM 100 // 商品数量上限
#define MAX_NAME_LEN 20 // 商品名字长度上限
// 商品结构体
typedef struct {
char name[MAX_NAME_LEN];
int stock; // 库存量
float price; // 单价
} Item;
// 商品数组和当前商品数量
Item items[MAX_ITEM_NUM];
int itemNum = 0;
// 显示菜单
void showMenu() {
printf("\n=== 进销存系统 ===\n");
printf("1. 添加商品\n");
printf("2. 显示商品列表\n");
printf("3. 修改商品信息\n");
printf("4. 删除商品\n");
printf("5. 退出\n");
}
// 添加商品
void addItem() {
if (itemNum >= MAX_ITEM_NUM) {
printf("商品数量已达上限,无法添加!\n");
return;
}
Item item;
printf("请输入商品名称:");
scanf("%s", item.name);
printf("请输入商品库存量:");
scanf("%d", &item.stock);
printf("请输入商品单价:");
scanf("%f", &item.price);
items[itemNum++] = item;
printf("商品已添加成功!\n");
}
// 显示商品列表
void showItemList() {
printf("\n商品列表:\n");
printf("%-10s %-10s %-10s\n", "名称", "库存量", "单价");
for (int i = 0; i < itemNum; i++) {
printf("%-10s %-10d %-10.2f\n", items[i].name, items[i].stock, items[i].price);
}
}
// 修改商品信息
void modifyItem() {
char name[MAX_NAME_LEN];
printf("请输入要修改的商品名称:");
scanf("%s", name);
for (int i = 0; i < itemNum; i++) {
if (strcmp(items[i].name, name) == 0) {
printf("请输入新的库存量:");
scanf("%d", &items[i].stock);
printf("请输入新的单价:");
scanf("%f", &items[i].price);
printf("商品信息已修改成功!\n");
return;
}
}
printf("未找到该商品!\n");
}
// 删除商品
void deleteItem() {
char name[MAX_NAME_LEN];
printf("请输入要删除的商品名称:");
scanf("%s", name);
for (int i = 0; i < itemNum; i++) {
if (strcmp(items[i].name, name) == 0) {
for (int j = i; j < itemNum - 1; j++) {
items[j] = items[j + 1];
}
itemNum--;
printf("商品已删除!\n");
return;
}
}
printf("未找到该商品!\n");
}
int main() {
int choice;
while (1) {
showMenu();
printf("请输入选项:");
scanf("%d", &choice);
switch (choice) {
case 1:
addItem();
break;
case 2:
showItemList();
break;
case 3:
modifyItem();
break;
case 4:
deleteItem();
break;
case 5:
printf("谢谢使用!\n");
exit(0);
default:
printf("无效的选项!\n");
}
}
return 0;
}
```
这个简单的进销存系统可以添加、显示、修改、删除商品的信息。但是这个程序还有很多不足之处,比如没有对输入做错误检查,没有数据持久化等,需要根据实际需求进行改进。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)