倉庫管理系統報告
一、項目背景
倉庫信息管理系統:實現進庫出庫、展示倉庫信息、支持查詢功能、數據的長久保存
二、實現環境
1. WSL
2. clang version 10.0.0
Target: x86_64-pc-linux-gnu
(c環境都能跑哦~)
三、報告正文
- 實現方法
- 數據使用文件保存
- 采用函數模塊化編程思想
- 使用了必要的結構體
- 重要的數據結構
typedef struct{
char name[100];
int count;
}Goods; // 物品結構體,其成員為物品名稱和數量。
typedef struct{
Goods goods[50];
int count;
}Store; // 倉庫結構體,成員是物品類數組和目前已存進的數目
- 實現過程
- main函數
- 調用
void Init_Store()
函數,讀取文件數據,初始化全局變量store
及其成員 - 使用
switch
分支結構,進行每次操作選擇
- 調用
- 初始化
- 主控函數
void Init_Store()
- 定義文件指針
FILE *fp
,只讀權限 - 通過
feof(fp)
保證文件完成讀取,同時更新倉庫store
內容 - 完成后關閉文件
- 定義文件指針
- 主控函數
- 入庫
- 主控函數
int add_to_list(char name[], int count)
- 調用函數
int InStore(char name[])
,查找該物品是否已經存在 - 若已存在(即返回值不是FALSE),調用函數
int increase_count(char name[], int count, int pos)
;更新store
變量和data.txt
數據存儲文件 - 若不存在(即返回值為具體下標),調用函數
int add_to_list(char name[], int count)
;更新store
變量和data.txt
數據存儲文件
- 調用函數
int increase_count(char name[], int count, int pos)
- 說明該物品在倉庫中已存在,通過下標修改物品數量
int add_to_list(char name[], int count)
- 判斷倉庫是否已滿(
store.count > 50 ?
) - 判斷存儲物品的名稱是否合法(
strlen(name) > 100 ?
) - 若合法,則將物品添加到
store.goods
數組末尾;若不合法,則添加失敗
- 判斷倉庫是否已滿(
- 主控函數
- 出庫
- 主控函數
int delete_goods(char namep[], int count)
- 調用函數
int InStore(char name[])
,查找倉庫中是否有該物品;若不存在則報錯 - 獲取到物品目前數量,與需取出相比較
- 物品數量不足,取出失敗
- 數量恰好,調用函數
int delete_from_list(char name[], int pos)
- 數量大於需求,調用函數
int decrease_count(char name[], int count, int pos)
- 取出完成,更新
store
變量和data.txt
數據存儲文件
- 調用函數
int delete_from_list(char name[], int pos)
- 判斷要刪除的位置是否在數組末尾
- 若不是,則刪除目前位置內容;並將該位置之后的內容依次向前挪一個單元
- 將數組末尾初始化置為0;
store.count --
int decrease_count(char name[], int count, int pos)
- 通過下標修改物品數量
- 主控函數
- 查找
- 遍歷倉庫
void show_goods()
- 查找物品
Goods find_goods(char name[])
- 調用函數
int InStore(char name[])
- 若未找到,則返回一個空goods;若找到,則返回變量內容
- 調用函數
- 遍歷倉庫
- 輔助函數
int InStore(char name[])
遍歷函數- 查找
store
中是否有name
- 若有,則返回對應下標;若無,則返回
FALSE
- 查找
void Write_tofile()
更新文件函數- 只寫方式打開
data.txt
- 將
store.goods[]
寫入,並控制寫入格式
- 只寫方式打開
- main函數
- 測試
- 數據正常讀入,正常寫入。
- 程序功能完整。
- 總結
就簡單的寫寫 ... 題目中也沒要求太多 ...
UI使用qt或者MFC都是不錯的選擇 ...
或者使用一些宏渲染一下顏色什么的都是可以的 ...
然后也沒有進行異常處理 ... 只進行了警告和函數控制 ...
對於程序消耗,struct Store是起控制作用的結構體,完全可以用指針來代替定長數組 ... and so on
就這樣吧 ... - 源碼
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define TRUE 1
#define FALSE -1
typedef struct{
char name[100];
int count;
}Goods;
typedef struct{
Goods goods[50];
int count;
}Store;
Store store;
void Init_Store()
{
printf("Init Start ...\n");
FILE *fp;
fp = fopen("data.txt","r");
if(fp == NULL) {
printf("No such file !\n");
exit(0);
}
while(1) {
if(feof(fp)) break;
fscanf(fp, "%s%d", store.goods[store.count].name, &store.goods[store.count].count);
store.count++;
}
fclose(fp);
printf("Init Finished !\n");
printf("There are %d kinds of items.\n", store.count);
}
int InStore(char name[])
{
int i = 0;
for(;i < store.count; ++ i){
if(strcmp(name, store.goods[i].name) == 0)
return i;
}
return FALSE;
}
void Write_tofile()
{
FILE *fp = NULL;
fp = fopen("data.txt","w");
int i = 0;
while(i < store.count) {
fprintf(fp, "%s %d", store.goods[i].name, store.goods[i].count);
if(i != store.count-1) {
fprintf(fp, "\n");
}
i++;
}
fclose(fp);
}
int increase_count(char name[], int count, int pos)
{
store.goods[pos].count += count;
printf("The items already exist and have increased %d.\nNow the count is : %d.\n", count, store.goods[pos].count);
return TRUE;
}
int add_to_list(char name[], int count)
{
if(store.count > 50) {
printf("No more space for this goods ! Can't be stocked !\n");
return FALSE;
}
if(strlen(name) > 100) {
printf("Name's length over 100! Can't be stocked !\n");
return FALSE;
}
strcpy(store.goods[store.count].name, name);
store.goods[store.count].count = count;
printf("Stock successfully !\nNow the count is : %d.\n", store.goods[store.count].count);
store.count ++;
return TRUE;
}
int add_goods(char name[], int count)
{
int instore = InStore(name);
if(instore != FALSE) {
increase_count(name, count, instore);
Write_tofile();
return TRUE;
}
add_to_list(name, count);
Write_tofile();
return 0;
}
int decrease_count(char name[], int count, int pos)
{
store.goods[pos].count -= count;
printf("%d out of stock.\nNow the count is : %d.\n", count, store.goods[pos].count);
return TRUE;
}
int delete_from_list(char name[], int pos)
{
if(pos != store.count-1) {
for(;pos < store.count-1;) {
strcpy(store.goods[pos].name, store.goods[pos+1].name);
store.goods[pos].count = store.goods[pos+1].count;
pos ++;
}
}
store.goods[pos].name[0] = '\0';
store.goods[pos].count = 0;
store.count --;
printf("Out of stock and delete from list.\n");
return TRUE;
}
int delete_goods(char name[], int count)
{
int instore = InStore(name);
if(instore == FALSE) {
printf("There is no such goods ! Can't out of the stock !\n");
return FALSE;
}
int goods_count = store.goods[instore].count;
if(goods_count < count) {
printf("The %s goods isn't enough !\nNow the count is : %d.\n", name, goods_count);
return FALSE;
} else if(goods_count == count) {
delete_from_list(name, instore);
} else if(goods_count > count) {
decrease_count(name, count, instore);
}
Write_tofile();
return TRUE;
}
void show_goods()
{
int i = 0;
printf("show goods : \n");
for(;i < store.count;i ++) {
printf("%s : %d\n",store.goods[i].name, store.goods[i].count);
}
}
Goods find_goods(char name[])
{
int instore = InStore(name);
if(instore == FALSE) {
printf("Can't find such goods!\n");
Goods goods;
goods.count = 0;
goods.name[0] = '\0';
return goods;
}
return store.goods[instore];
}
int main()
{
Init_Store();
printf(" ---- list ---- \n");
printf(" 1: add goods 2: delete goods\n");
printf(" 3: list goods 4: find goods\n");
printf(" -- 0: exit -- \n");
int choice = 1;
while(choice!=0) {
printf("Enter your choice : ");
scanf("%d",&choice);
switch(choice) {
case 1:{
char str[100];
int count;
printf("Input goods and count to add : ");
scanf("%s%d", str, &count);
add_goods(str, count);
break;
}
case 2:{
char str[100];
int count;
printf("Input goods and count to delete : ");
scanf("%s%d", str, &count);
delete_goods(str, count);
break;
}
case 3:{
show_goods();
break;
}
case 4:{
char str[100];
printf("Input goods name to find : ");
scanf("%s",str);
Goods temp = find_goods(str);
printf("The goods : %s %d\n", temp.name, temp.count);
break;
}
default:{
printf("Please enter correct choice!\n");
break;
}
}
}
return 0;
}