通過這個完整的超市管理系統,您將掌握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 50typedef 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++;// 自動生成IDlist->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;fread(&count, sizeof(int), 1, file);if (count <= 0) {fclose(file);return 0;}// 分配內存Product* new_data = malloc(count * sizeof(Product));if (!new_data) {printf("內存分配失敗!\n");fclose(file);exit(EXIT_FAILURE);}// 讀取商品數據for (int i = 0; i < count; i++) {fread(&new_data[i], sizeof(Product), 1, file);}fclose(file);// 更新列表if (list->data) {free(list->data);}list->data = new_data;list->count = count;printf("\n從 %s 加載了 %d 個商品\n", filename, count);return 1;
}
3. 文件操作實現 (fileio.c)
// fileio.c
#include "product.h"
#include <stdio.h>
#include <stdlib.h>// 清屏函數(跨平臺)
void clear_screen() {#ifdef _WIN32system("cls");#elsesystem("clear");#endif
}
4. 主程序與用戶界面 (main.c)
// main.c
#include <stdio.h>
#include <stdlib.h>
#include "product.h"#define FILENAME "supermarket.dat"void display_menu() {printf("\n===== 超市管理系統 =====\n");printf("1. 添加商品\n");printf("2. 顯示所有商品\n");printf("3. 修改商品信息\n");printf("4. 刪除商品\n");printf("5. 搜索商品\n");printf("6. 保存數據\n");printf("7. 退出系統\n");printf("請選擇操作: ");
}int main() {ProductList inventory;init_products(&inventory);// 加載已有數據load_from_file(FILENAME, &inventory);int choice;while (1) {display_menu();scanf("%d", &choice);switch (choice) {case 1:add_product(&inventory);break;case 2:display_products(&inventory);break;case 3:modify_product(&inventory);break;case 4:delete_product(&inventory);break;case 5:search_product(&inventory);break;case 6:save_to_file(FILENAME, &inventory);break;case 7:save_to_file(FILENAME, &inventory);free(inventory.data);printf("\n系統已退出,數據已保存\n");return 0;default:printf("無效選項,請重新輸入\n");}// 清除輸入緩沖區while (getchar() != '\n');printf("\n按回車鍵繼續...");getchar();clear_screen();}
}
系統功能詳解
1. 數據管理功能
- ?動態內存管理?:使用
realloc()
動態調整內存大小 - ?自動ID生成?:商品ID根據添加順序自動生成
- ?輸入驗證?:限制輸入長度,防止緩沖區溢出
- ?高效刪除?:通過移動最后一個元素實現O(1)復雜度刪除
2. 文件操作功能
- ?二進制存儲?:使用二進制格式保存數據,效率更高
- ?數據恢復?:啟動時自動加載上次保存的數據
- ?錯誤處理?:文件操作包含錯誤檢查,防止數據損壞
3. 用戶界面設計
- ?菜單驅動?:直觀的菜單導航系統
- ?跨平臺清屏?:兼容Windows/Linux/macOS系統
- ?操作確認?:關鍵操作后提供成功反饋
編譯與使用指南
編譯方法(GCC)
gcc -c product.c fileio.c
gcc main.c product.o fileio.o -o supermarket
./supermarket
使用說明
- 首次運行會自動創建數據文件
- 添加商品時只需輸入名稱、價格和庫存
- 可通過ID或名稱關鍵字搜索商品
- 修改庫存時直接輸入新數值即可
- 退出系統前務必選擇"保存數據"選項
擴展與優化建議
1.增加銷售功能?:
// 添加銷售函數
void sell_product(ProductList* list, int id, int quantity) {if (id < 1 || id > list->count) return;if (list->data[id-1].stock < quantity) {printf("庫存不足!\n");return;}list->data[id-1].stock -= quantity;printf("已售出 %d 件 %s\n", quantity, list->data[id-1].name);
}
2.?增加統計功能?:
// 庫存統計
void inventory_stats(ProductList* list) {float total_value = 0;printf("\n庫存統計:\n");for (int i = 0; i < list->count; i++) {float value = list->data[i].price * list->data[i].stock;printf("%s: 數量=%d, 總價值=%.2f\n", list->data[i].name, list->data[i].stock, value);total_value += value;}printf("\n庫存總價值: %.2f\n", total_value);
}
3.性能優化技巧?:
- 使用哈希表加速搜索
- 分批寫入文件減少I/O操作
- 添加內存緩存機制
這個超市管理系統實現了核心的商品管理功能,代碼結構清晰,注釋完整,適合學習和課程設計使用。通過動態內存管理和文件操作,系統可以高效地處理商品數據,為超市日常運營提供了便捷的管理工具
資源推薦:
C/C++學習交流君羊?<< 點擊加入
C/C++教程
C/C++學習路線,就業咨詢,技術提升