超市库存管理系统,c语言,文件存储代码实现
时间: 2023-05-30 09:02:11 浏览: 331
C#仓库管理系统+完整源代码
5星 · 资源好评率100%
以下是一个简单的超市库存管理系统的C语言代码实现,使用了文件存储来保存数据。
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_PRODUCTS 100 // 最大商品数量
#define MAX_NAME_LEN 50 // 商品名称最大长度
// 商品结构体
typedef struct {
char name[MAX_NAME_LEN];
float price;
int quantity;
} Product;
// 从文件中读取商品数据
int read_products(Product *products, int *num_products) {
FILE *file = fopen("products.dat", "rb");
if (file == NULL) {
return 0;
}
fread(num_products, sizeof(int), 1, file);
fread(products, sizeof(Product), *num_products, file);
fclose(file);
return 1;
}
// 将商品数据写入文件
int write_products(Product *products, int num_products) {
FILE *file = fopen("products.dat", "wb");
if (file == NULL) {
return 0;
}
fwrite(&num_products, sizeof(int), 1, file);
fwrite(products, sizeof(Product), num_products, file);
fclose(file);
return 1;
}
// 显示商品列表
void show_products(Product *products, int num_products) {
printf("商品列表:\n");
printf("名称\t\t价格\t数量\n");
for (int i = 0; i < num_products; i++) {
printf("%-15s\t%.2f\t%d\n", products[i].name, products[i].price, products[i].quantity);
}
}
// 添加商品
void add_product(Product *products, int *num_products) {
if (*num_products == MAX_PRODUCTS) {
printf("商品数量已达到上限\n");
return;
}
char name[MAX_NAME_LEN];
float price;
int quantity;
printf("请输入商品名称: ");
scanf("%s", name);
printf("请输入商品价格: ");
scanf("%f", &price);
printf("请输入商品数量: ");
scanf("%d", &quantity);
strcpy(products[*num_products].name, name);
products[*num_products].price = price;
products[*num_products].quantity = quantity;
(*num_products)++;
printf("商品添加成功\n");
}
// 删除商品
void delete_product(Product *products, int *num_products) {
if (*num_products == 0) {
printf("没有商品可供删除\n");
return;
}
char name[MAX_NAME_LEN];
printf("请输入要删除的商品名称: ");
scanf("%s", name);
// 遍历商品列表,查找要删除的商品
int found = 0;
for (int i = 0; i < *num_products; i++) {
if (strcmp(products[i].name, name) == 0) {
found = 1;
// 将该商品后面的所有商品向前移动一个位置
for (int j = i; j < (*num_products - 1); j++) {
products[j] = products[j + 1];
}
(*num_products)--;
printf("商品删除成功\n");
break;
}
}
if (!found) {
printf("未找到要删除的商品\n");
}
}
// 修改商品
void modify_product(Product *products, int num_products) {
if (num_products == 0) {
printf("没有商品可供修改\n");
return;
}
char name[MAX_NAME_LEN];
printf("请输入要修改的商品名称: ");
scanf("%s", name);
// 遍历商品列表,查找要修改的商品
int found = 0;
for (int i = 0; i < num_products; i++) {
if (strcmp(products[i].name, name) == 0) {
found = 1;
float price;
int quantity;
printf("请输入新的价格: ");
scanf("%f", &price);
printf("请输入新的数量: ");
scanf("%d", &quantity);
products[i].price = price;
products[i].quantity = quantity;
printf("商品修改成功\n");
break;
}
}
if (!found) {
printf("未找到要修改的商品\n");
}
}
// 按名称查找商品
void search_product(Product *products, int num_products) {
if (num_products == 0) {
printf("没有商品可供查找\n");
return;
}
char name[MAX_NAME_LEN];
printf("请输入要查找的商品名称: ");
scanf("%s", name);
// 遍历商品列表,查找要查找的商品
int found = 0;
for (int i = 0; i < num_products; i++) {
if (strcmp(products[i].name, name) == 0) {
found = 1;
printf("名称\t\t价格\t数量\n");
printf("%-15s\t%.2f\t%d\n", products[i].name, products[i].price, products[i].quantity);
break;
}
}
if (!found) {
printf("未找到要查找的商品\n");
}
}
int main() {
Product products[MAX_PRODUCTS];
int num_products = 0;
if (!read_products(products, &num_products)) {
printf("无法读取商品数据\n");
return 1;
}
while (1) {
printf("\n请选择操作:\n");
printf("1. 显示商品列表\n");
printf("2. 添加商品\n");
printf("3. 删除商品\n");
printf("4. 修改商品\n");
printf("5. 按名称查找商品\n");
printf("6. 退出\n");
printf("请选择: ");
int choice;
scanf("%d", &choice);
switch (choice) {
case 1:
show_products(products, num_products);
break;
case 2:
add_product(products, &num_products);
write_products(products, num_products);
break;
case 3:
delete_product(products, &num_products);
write_products(products, num_products);
break;
case 4:
modify_product(products, num_products);
write_products(products, num_products);
break;
case 5:
search_product(products, num_products);
break;
case 6:
write_products(products, num_products);
return 0;
default:
printf("无效的选择\n");
break;
}
}
}
```
阅读全文