倉庫信息管理系統:實現進庫出庫、展現倉庫信息、支持查詢功能、數據的長久保存linux
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; // 倉庫結構體,成員是物品類數組和目前已存進的數目
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[])
,查找該物品是否已經存在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)
store.count --
int decrease_count(char name[], int count, int pos)
void show_goods()
Goods find_goods(char name[])
int InStore(char name[])
int InStore(char name[])
遍歷函數
store
中是否有name
FALSE
void Write_tofile()
更新文件函數
data.txt
store.goods[]
寫入,並控制寫入格式#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; }