用C写一个仓库管理系统,并注释
时间: 2024-03-10 22:47:33 浏览: 16
好的,以下是一个简单的仓库管理系统的C代码,注释在代码中:
```c
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
//定义结构体,表示仓库存储的商品
struct goods {
int goods_id; //商品编号
char goods_name[20]; //商品名称
int goods_price; //商品价格
int goods_num; //商品数量
};
//定义函数,用于添加商品
void add_goods(struct goods *g, int n) {
printf("请输入商品编号、商品名称、商品价格和商品数量:\n");
for(int i = 0; i < n; i++) {
printf("第%d个商品:", i+1);
scanf("%d%s%d%d", &g[i].goods_id, g[i].goods_name, &g[i].goods_price, &g[i].goods_num);
}
}
//定义函数,用于查找商品
void find_goods(struct goods *g, int n) {
int id;
printf("请输入要查找的商品编号:\n");
scanf("%d", &id);
for(int i = 0; i < n; i++) {
if(g[i].goods_id == id) {
printf("商品编号:%d 商品名称:%s 商品价格:%d 商品数量:%d\n", g[i].goods_id, g[i].goods_name, g[i].goods_price, g[i].goods_num);
return;
}
}
printf("未找到商品!\n");
}
int main() {
int n;
printf("请输入仓库中商品的数量:\n");
scanf("%d", &n);
//动态分配内存空间
struct goods *g = malloc(n * sizeof(struct goods));
//添加商品
add_goods(g, n);
//查找商品
find_goods(g, n);
//释放内存空间
free(g);
return 0;
}
```
这个仓库管理系统可以实现添加商品和查找商品的功能,你可以根据自己的需求进行修改和完善。
相关推荐
![](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)
![7z](https://img-home.csdnimg.cn/images/20210720083312.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)