c語言之倉庫信息管理系統

倉庫管理系統報告

1、項目背景

倉庫信息管理系統:實現進庫出庫、展現倉庫信息、支持查詢功能、數據的長久保存linux

2、實現環境

1. WSL
2. clang version 10.0.0
   Target: x86_64-pc-linux-gnu

(c環境都能跑哦~)編程

3、報告正文

  1. 實現方法
    1. 數據使用文件保存
    2. 採用函數模塊化編程思想
    3. 使用了必要的結構體
  2. 重要的數據結構
typedef struct{
    char name[100]; 
    int count;
}Goods;     // 物品結構體,其成員爲物品名稱和數量。

typedef struct{
    Goods goods[50];
    int count;
}Store;     // 倉庫結構體,成員是物品類數組和目前已存進的數目
  1. 實現過程
    1. main函數
      1. 調用void Init_Store()函數,讀取文件數據,初始化全局變量store及其成員
      2. 使用switch分支結構,進行每次操做選擇
    2. 初始化
      1. 主控函數void Init_Store()
        1. 定義文件指針FILE *fp,只讀權限
        2. 經過feof(fp)保證文件完成讀取,同時更新倉庫store內容
        3. 完成後關閉文件
    3. 入庫
      1. 主控函數int add_to_list(char name[], int count)
        1. 調用函數int InStore(char name[]),查找該物品是否已經存在
        2. 若已存在(即返回值不是FALSE),調用函數int increase_count(char name[], int count, int pos);更新store變量和data.txt數據存儲文件
        3. 若不存在(即返回值爲具體下標),調用函數int add_to_list(char name[], int count);更新store變量和data.txt數據存儲文件
      2. int increase_count(char name[], int count, int pos)
        1. 說明該物品在倉庫中已存在,經過下標修改物品數量
      3. int add_to_list(char name[], int count)
        1. 判斷倉庫是否已滿(store.count > 50 ?)
        2. 判斷存儲物品的名稱是否合法(strlen(name) > 100 ?)
        3. 若合法,則將物品添加到store.goods數組末尾;若不合法,則添加失敗
    4. 出庫
      1. 主控函數int delete_goods(char namep[], int count)
        1. 調用函數int InStore(char name[]),查找倉庫中是否有該物品;若不存在則報錯
        2. 獲取到物品目前數量,與需取出相比較
          1. 物品數量不足,取出失敗
          2. 數量剛好,調用函數int delete_from_list(char name[], int pos)
          3. 數量大於需求,調用函數int decrease_count(char name[], int count, int pos)
        3. 取出完成,更新store變量和data.txt數據存儲文件
      2. int delete_from_list(char name[], int pos)
        1. 判斷要刪除的位置是否在數組末尾
        2. 若不是,則刪除目前位置內容;並將該位置以後的內容依次向前挪一個單元
        3. 將數組末尾初始化置爲0;store.count --
      3. int decrease_count(char name[], int count, int pos)
        1. 經過下標修改物品數量
    5. 查找
      1. 遍歷倉庫void show_goods()
      2. 查找物品Goods find_goods(char name[])
        1. 調用函數int InStore(char name[])
        2. 若未找到,則返回一個空goods;若找到,則返回變量內容
    6. 輔助函數
      1. int InStore(char name[])遍歷函數
        1. 查找store中是否有name
        2. 如有,則返回對應下標;若無,則返回FALSE
      2. void Write_tofile()更新文件函數
        1. 只寫方式打開data.txt
        2. store.goods[]寫入,並控制寫入格式
  2. 測試
    1. 數據正常讀入,正常寫入。
    2. 程序功能完整。

  3. 總結
    就簡單的寫寫 ... 題目中也沒要求太多 ...
    UI使用qt或者MFC都是不錯的選擇 ...
    或者使用一些宏渲染一下顏色什麼的都是能夠的 ...
    而後也沒有進行異常處理 ... 只進行了警告和函數控制 ...
    對於程序消耗,struct Store是起控制做用的結構體,徹底能夠用指針來代替定長數組 ... and so on
    就這樣吧 ...
  4. 源碼
#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;
}
相關文章
相關標籤/搜索