【C语言】超市管理系统丨完整源码与实现解析
通过这个完整的超市管理系统,您将掌握C语言核心数据结构与文件操作技术
设计思路与核心数据结构
本系统使用动态数组管理商品数据,支持商品增删改查、文件存储和数据统计功能。系统采用模块化设计,分为商品管理、文件操作和用户界面三大模块。
// 商品结构体
typedef struct {
int id; // 商品ID(自动生成)
char name[50]; // 商品名称
float price; // 商品单价
int stock; // 库存数量
} Product;
// 商品列表(动态数组)
typedef struct {
Product* data; // 指向商品数组的指针
int count; // 当前商品数量
} ProductList;

完整源代码实现
1. 头文件定义 (product.h)
// product.h
#pragma once
#define NAME_LEN 50
typedef struct {
int id;
char name[NAME_LEN];
float price;
int stock;
} Product;
typedef struct {
Product* data;
int count;
} ProductList;
// 函数声明
void init_products(ProductList* list);
void add_product(ProductList* list);
void display_products(ProductList* list);
void modify_product(ProductList* list);
void delete_product(ProductList* list);
void search_product(ProductList* list);
void save_to_file(const char* filename, ProductList* list);
int load_from_file(const char* filename, ProductList* list);

2. 核心功能实现 (product.c)
// product.c
#include "product.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// 初始化商品列表
void init_products(ProductList* list) {
list->data = NULL;
list->count = 0;
}
// 添加商品
void add_product(ProductList* list) {
// 动态扩展内存
Product* new_data = realloc(list->data, (list->count + 1) * sizeof(Product));
if (!new_data) {
printf("内存分配失败!\n");
exit(EXIT_FAILURE);
}
list->data = new_data;
list->count++;
// 自动生成ID
list->data[list->count - 1].id = list->count;
// 输入商品信息
printf("\n商品ID: %d\n", list->count);
printf("请输入商品名称: ");
scanf("%49s", list->data[list->count - 1].name);
printf("请输入单价: ");
scanf("%f", &list->data[list->count - 1].price);
printf("请输入库存数量: ");
scanf("%d", &list->data[list->count - 1].stock);
printf("商品添加成功!\n");
}
// 显示所有商品
void display_products(ProductList* list) {
if (list->count == 0) {
printf("\n库存为空\n");
return;
}
printf("\n%5s %-20s %10s %6s\n", "ID", "名称", "单价", "库存");
printf("------------------------------------------------\n");
for (int i = 0; i < list->count; i++) {
printf("%5d %-20s %10.2f %6d\n",
list->data[i].id,
list->data[i].name,
list->data[i].price,
list->data[i].stock);
}
}
// 修改商品信息
void modify_product(ProductList* list) {
if (list->count == 0) {
printf("\n库存为空,无法修改\n");
return;
}
int id;
printf("\n请输入要修改的商品ID: ");
scanf("%d", &id);
if (id < 1 || id > list->count) {
printf("无效的商品ID\n");
return;
}
Product* product = &list->data[id - 1];
printf("\n当前商品信息:\n");
printf("名称: %s, 单价: %.2f, 库存: %d\n",
product->name, product->price, product->stock);
printf("\n输入新名称: ");
scanf("%49s", product->name);
printf("输入新单价: ");
scanf("%f", &product->price);
printf("输入新库存: ");
scanf("%d", &product->stock);
printf("\n商品信息更新成功!\n");
}
// 删除商品
void delete_product(ProductList* list) {
if (list->count == 0) {
printf("\n库存为空,无法删除\n");
return;
}
int id;
printf("\n请输入要删除的商品ID: ");
scanf("%d", &id);
if (id < 1 || id > list->count) {
printf("无效的商品ID\n");
return;
}
// 将最后一个元素移到要删除的位置
if (id < list->count) {
list->data[id - 1] = list->data[list->count - 1];
}
// 减少内存空间
Product* new_data = realloc(list->data, (list->count - 1) * sizeof(Product));
if (list->count > 1 && !new_data) {
printf("内存重新分配失败!\n");
return;
}
list->data = new_data;
list->count--;
// 重新生成ID序列
for (int i = 0; i < list->count; i++) {
list->data[i].id = i + 1;
}
printf("\n商品删除成功!\n");
}
// 搜索商品
void search_product(ProductList* list) {
if (list->count == 0) {
printf("\n库存为空\n");
return;
}
char keyword[50];
printf("\n请输入商品名称或ID: ");
scanf("%49s", keyword);
int found = 0;
printf("\n搜索结果:\n");
printf("%5s %-20s %10s %6s\n", "ID", "名称", "单价", "库存");
printf("------------------------------------------------\n");
for (int i = 0; i < list->count; i++) {
if (strstr(list->data[i].name, keyword) ||
(atoi(keyword) > 0 && list->data[i].id == atoi(keyword))) {
printf("%5d %-20s %10.2f %6d\n",
list->data[i].id,
list->data[i].name,
list->data[i].price,
list->data[i].stock);
found = 1;
}
}
if (!found) {
printf("未找到匹配的商品\n");
}
}
// 保存到文件
void save_to_file(const char* filename, ProductList* list) {
FILE* file = fopen(filename, "wb");
if (!file) {
printf("无法打开文件进行保存!\n");
return;
}
// 写入商品数量
fwrite(&list->count, sizeof(int), 1, file);
// 写入所有商品数据
for (int i = 0; i < list->count; i++) {
fwrite(&list->data[i], sizeof(Product), 1, file);
}
fclose(file);
printf("\n数据已保存到 %s\n", filename);
}
// 从文件加载
int load_from_file(const char* filename, ProductList* list) {
FILE* file = fopen(filename, "rb");
if (!file) {
printf("文件不存在,将创建新文件\n");
return 0;
}
// 读取商品数量
int count;