?系統概述
這是一個功能完善的藥店藥品管理系統,使用C語言開發,基于鏈表數據結構實現。系統提供藥品信息的增刪改查、排序和持久化存儲功能,適用于藥店日常藥品管理工作。
數據結構設計
#define MAX_NAME_LEN 50
#define MAX_ID_LEN 20
#define FILENAME "medicine.dat"/* 藥品信息結構體 */
typedef struct Medicine {char id[MAX_ID_LEN]; // 藥品編號char name[MAX_NAME_LEN]; // 藥品名稱float price; // 單價int stock; // 庫存數量struct Medicine *next; // 鏈表指針
} Medicine;
系統核心函數
1. 鏈表初始化與持久化
/* 從文件加載數據 */
void loadFromFile() {FILE *file = fopen(FILENAME, "rb");if (!file) return;Medicine temp;while (fread(&temp, sizeof(Medicine), 1, file)) {Medicine *newMed = (Medicine*)malloc(sizeof(Medicine));*newMed = temp;newMed->next = head;head = newMed;medicineCount++;}fclose(file);
}/* 保存數據到文件 */
void saveToFile() {FILE *file = fopen(FILENAME, "wb");if (!file) {printf("無法打開文件進行保存!\n");return;}Medicine *current = head;while (current) {fwrite(current, sizeof(Medicine), 1, file);current = current->next;}fclose(file);printf("成功保存%d條藥品記錄!\n", medicineCount);
}
?
2. 藥品添加功能
void addMedicine() {Medicine *newMed = (Medicine*)malloc(sizeof(Medicine));printf("\n--- 添加新藥品 ---\n");// 輸入藥品編號并檢查重復printf("輸入藥品編號: ");scanf("%s", newMed->id);clearInputBuffer();Medicine *current = head;while (current) {if (strcmp(current->id, newMed->id) == 0) {printf("錯誤:藥品編號已存在!\n");free(newMed);return;}current = current->next;}// 輸入其他信息printf("輸入藥品名稱: ");fgets(newMed->name, MAX_NAME_LEN, stdin);newMed->name[strcspn(newMed->name, "\n")] = '\0';printf("輸入藥品單價: ");scanf("%f", &newMed->price);printf("輸入庫存數量: ");scanf("%d", &newMed->stock);clearInputBuffer();// 添加到鏈表頭部newMed->next = head;head = newMed;medicineCount++;printf("藥品添加成功!\n");
}
3. 藥品刪除功能
void deleteMedicine() {char id[MAX_ID_LEN];printf("\n--- 刪除藥品 ---\n");printf("輸入要刪除的藥品編號: ");scanf("%s", id);clearInputBuffer();Medicine *current = head;Medicine *prev = NULL;while (current) {if (strcmp(current->id, id) == 0) {if (prev) {prev->next = current->next;} else {head = current->next;}free(current);medicineCount--;printf("藥品刪除成功!\n");return;}prev = current;current = current->next;}printf("未找到該藥品!\n");
}
?
4. 藥品查詢功能
void searchMedicine() {char keyword[MAX_NAME_LEN];int found = 0;printf("\n--- 藥品查詢 ---\n");printf("輸入藥品編號或名稱: ");fgets(keyword, MAX_NAME_LEN, stdin);keyword[strcspn(keyword, "\n")] = '\0';Medicine *current = head;printf("\n%-15s %-20s %-10s %-10s\n", "編號", "名稱", "單價", "庫存");printf("------------------------------------------------\n");while (current) {if (strstr(current->id, keyword) || strstr(current->name, keyword)) {printf("%-15s %-20s %-10.2f %-10d\n", current->id, current->name, current->price, current->stock);found = 1;}current = current->next;}if (!found) {printf("未找到匹配的藥品!\n");}
}
?
5. 排序功能實現
/* 按價格排序(冒泡排序) */
void sortByPrice() {if (!head || !head->next) return;int swapped;Medicine *ptr1;Medicine *lptr = NULL;do {swapped = 0;ptr1 = head;while (ptr1->next != lptr) {if (ptr1->price < ptr1->next->price) {// 交換節點數據Medicine temp = *ptr1;strcpy(ptr1->id, ptr1->next->id);strcpy(ptr1->name, ptr1->next->name);ptr1->price = ptr1->next->price;ptr1->stock = ptr1->next->stock;strcpy(ptr1->next->id, temp.id);strcpy(ptr1->next->name, temp.name);ptr1->next->price = temp.price;ptr1->next->stock = temp.stock;swapped = 1;}ptr1 = ptr1->next;}lptr = ptr1;} while (swapped);printf("\n已按價格降序排序!\n");displayAll();
}
完整系統源碼
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>#define MAX_NAME_LEN 50
#define MAX_ID_LEN 20
#define FILENAME "medicine.dat"typedef struct Medicine {char id[MAX_ID_LEN];char name[MAX_NAME_LEN];float price;int stock;struct Medicine *next;
} Medicine;Medicine *head = NULL;
int medicineCount = 0;// 所有函數聲明
void initSystem();
void saveToFile();
void loadFromFile();
void addMedicine();
void deleteMedicine();
void modifyMedicine();
void searchMedicine();
void displayAll();
void sortByPrice();
void sortByStock();
void clearInputBuffer();
void freeList();int main() {int choice;initSystem();while (1) {system("cls || clear");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("8. 保存數據\n");printf("0. 退出系統\n");printf("============================\n");printf("請選擇操作: ");if (scanf("%d", &choice) != 1) {clearInputBuffer();printf("輸入無效,請重新輸入!\n");continue;}switch (choice) {case 1: addMedicine(); break;case 2: deleteMedicine(); break;case 3: modifyMedicine(); break;case 4: searchMedicine(); break;case 5: displayAll(); break;case 6: sortByPrice(); break;case 7: sortByStock(); break;case 8: saveToFile(); break;case 0: saveToFile();freeList();printf("系統已退出,數據已保存!\n");exit(0);default:printf("無效選擇,請重新輸入!\n");}printf("\n按回車鍵繼續...");clearInputBuffer();getchar();}return 0;
}// 此處為前文列出的所有函數實現...
?
數據文件
- 所有藥品數據自動保存到?
medicine.dat
?文件中 - 每次啟動程序時會自動加載之前保存的數據
- 退出程序時自動保存當前數據
功能操作
- ?添加藥品?:輸入藥品編號、名稱、單價和庫存量
- ?刪除藥品?:根據藥品編號刪除指定藥品
- ?修改藥品?:更新藥品的名稱、單價和庫存
- ?查詢藥品?:支持按編號或名稱進行模糊查詢
- ?排序功能?:按價格或庫存進行降序排列
- ?數據保存?:手動保存當前數據到文件
- ?退出系統?:安全退出并保存數據
關鍵實現要點
-
?數據結構選擇?:
- 使用單鏈表存儲藥品信息
- 動態內存分配管理藥品節點
- 全局頭指針和計數器簡化管理
-
?數據持久化?:
- 使用二進制文件格式提高存儲效率
fread/fwrite
實現結構體直接讀寫- 自動加載和保存機制確保數據安全
-
?用戶交互設計?:
- 清晰的菜單導航系統
- 表格化數據顯示
- 輸入錯誤處理和緩沖區清理
-
?排序算法?:
- 冒泡排序實現簡單高效
- 僅交換節點數據,保持鏈表結構
- 支持價格和庫存兩種排序方式
系統總結
這個藥店藥品管理系統展示了C語言在數據結構、文件操作和用戶界面設計方面的強大能力。通過本系統,您可以:
- 了解鏈表數據結構的實際應用
- 掌握C語言文件操作技巧
- 學習完整項目的基本架構設計
- 掌握用戶界面的基本設計原則
- 學習數據持久化存儲的實現方法
系統具有良好的擴展性,可以根據需要添加更多功能,如:
- 按銷售日期管理藥品
- 添加有效期管理
- 實現采購和銷售模塊
- 添加用戶登錄和權限控制
代碼已添加詳細注釋,便于理解和學習,是學習C語言編程和數據結構實現的優秀案例。
資源推薦:
C/C++學習交流君羊?<< 點擊加入
C/C++教程
C/C++學習路線,就業咨詢,技術提升