超市管理系统完整代码

#include <io.h>
#include <malloc.h>
#include <stdio.h>
#include <stdlib.h>

#define MAX_LEN 256
#define MAX_GOODS 100

typedef enum { OK = 1, ERROR = 0 } Status;
typedef enum { YES = 1, NO = 0 } Bool;

typedef struct {
  int number;
  char partName[MAX_LEN + 1];
  int onHand;
  float price;
} Part;

typedef struct {
  Part *pGoodsInfoBase[MAX_GOODS];
  int partNum;
} GoodsInfoBase;

void PrintInfoOfCopyright(void);
/*Function: Print the information of copyright.*/

void PrintInfoOfInstructions(void);
/*Function: Print the information of instructions.*/

int Search(int searchNumber, GoodsInfoBase *pGoodsInfoBase);
/*Function: To find that whether the specific part exists or not.
            If it existed, return the index location. If not, return -1.
Input: searchNumber,the specific part number,
       pGoodsInfoBase,a pointer point to the goodsInfoBase in the main fuction.
Output: i,the index location of the specific part,
        or -1 if the specific part doesn't exist.*/

Status ReadGoodsInfoFile(char *fileName, GoodsInfoBase *pGoodsInfoBase);
/*Function: To read all of the information about goods from a file.
 Input: The name of a file;
        pGoodsInfoBase,a pointer point to the goodsInfoBase in the main fuction.
Output: The status OK if read successfully or the status ERROR when failed to
read.*/

Status ExitSSOS(char *fileName, GoodsInfoBase *pGoodsInfoBase);
/*Function: End the SSOS program and store all the information of goods into a
file. Input: A name of a new file that will store all the information;
       pGoodsInfoBase,a pointer point to the goodsInfoBase in the main fuction.
Output: The status OK When all the information is successfully stored and end
the SSOS; the status ERROR when failed to do it.*/

Status Insert(Part *pNewPart, GoodsInfoBase *pGoodsInfoBase);
/*Function: To insert the information of a specific part to the goodsInfoBase in
the main fuction. Input: pGoodsInfoBase,a pointer point to the goodsInfoBase in
the main fuction; pNewPart,a pointer point to a new part. Output: The status OK
if the information of the speific part successfully insert to the goodsInfoBase,
        or the status ERROR if goodsInfoBase is full or mallocing failed.
Note: It is required to #include<malloc.h> and #include<stdlib.h>.*/

Status UpdataOnHand(int changeOnHand, GoodsInfoBase *pGoodsInfoBase, int i);
/*Fuction: To change the quantity on hand of a specific part .
Input: changeOnHand, the change on hand of a part;
       pGoodsInfoBase,a pointer point to the goodsInfoBase in the main fuction;
       i,the index location of a part.*/

Status UpdataPrice(float newPrice, GoodsInfoBase *pGoodsInfoBase, int i);
/*Fuction: To change the price of a specific part .
Input: newPrice, the new price of a part;
       pGoodsInfoBase,a pointer point to the goodsInfoBase in the main fuction;
       i,the index location of a part.*/

Status Print(GoodsInfoBase *pGoodsInfoBase);
/*Fuction: To show all the information of the whole goods.
Input: pGoodsInfoBase,a pointer point to the goodsInfoBase in the main fuction.
Output: All the information of the whole goods.*/

Status Delete(int i, GoodsInfoBase *pGoodsInfoBase);
/*Function: To delete a part's information if it exist.
Input: pGoodsInfoBase,a pointer point to the goodsInfoBase in the main fuction.
       i,the index location of a part.*/

Bool IsFull(GoodsInfoBase *pGoodsInfoBase);
/*Function: To see whether the goods is full or not.
Input: pGoodsInfoBase,a pointer point to the goodsInfoBase in the main fuction.
Output: NO when it is not full or YES when it is full.*/

Status PrintInfoOfAPart(int i, GoodsInfoBase *pGoodsInfoBase);
/*Function: To show the information of a part.
Input: pGoodsInfoBase,a pointer point to the goodsInfoBase in the main fuction.
       i,the index location of a part.*/

Bool IsIllegalInput(int scanfReturnValueOfPartNumber,
                    char firstCharacterOfPartName, int scanfReturnValueOfOnHand,
                    int scanfReturnValueOfPrice);
/*Function: To see whether the input is correct or not.
Input: the return value of scanfs and the first character of the part name.
Output: YES or NO.*/

Bool IsIllegalInput(int scanfReturnValueOfPartNumber,
                    char firstCharacterOfPartName, int scanfReturnValueOfOnHand,
                    int scanfReturnValueOfPrice) {
  Bool ret = NO;
  char ch = firstCharacterOfPartName;
  int a = scanfReturnValueOfPartNumber;
  int b = scanfReturnValueOfOnHand;
  int c = scanfReturnValueOfPrice;
  if (!((ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z'))) {
    ret = YES;
  }
  if (a == 0) {
    ret = YES;
  }
  if (b == 0) {
    ret = YES;
  }
  if (c == 0) {
    ret = YES;
  }
  return ret;
}

Status PrintInfoOfAPart(int i, GoodsInfoBase *pGoodsInfoBase) {
  printf("   %d\t\t%s\t\t%d\t\t\t%g\n",
         pGoodsInfoBase->pGoodsInfoBase[i]->number,
         pGoodsInfoBase->pGoodsInfoBase[i]->partName,
         pGoodsInfoBase->pGoodsInfoBase[i]->onHand,
         pGoodsInfoBase->pGoodsInfoBase[i]->price);
  return OK;
}

Bool IsFull(GoodsInfoBase *pGoodsInfoBase) {
  int i;
  i = pGoodsInfoBase->partNum;
  if (i >= MAX_GOODS) {
    return YES;
  }
  return NO;
}

void PrintInfoOfCopyright(void) {
  puts("Welcome to use the Super Supermarket OS(SSOS)!\n");
  puts("Super Supermarket OS Command V1.0, 2017-12-9");
  puts("developed by Rong Yang.");
  puts("All rights reserved.\n");
  return;
}

void PrintInfoOfInstructions(void) {
  puts("Instructions:");
  puts("'i': Insert a part to the base.");
  puts("'d': Delete a part from the base.");
  puts("'s': Search a part and show its information.");
  puts("'u': Update information of a part.");
  puts("'p': Print all the information of the base.");
  puts("'q': Exit the market system and save all the information.\n");
  return;
}

int Search(int searchNumber, GoodsInfoBase *pGoodsInfoBase) {
  int i, ret = -1;
  for (i = 0; i < pGoodsInfoBase->partNum; i++) {
    if (pGoodsInfoBase->pGoodsInfoBase[i]->number == searchNumber) {
      ret = i;
      break;
    }
  }
  return ret;
}

Status ReadGoodsInfoFile(char *fileName, GoodsInfoBase *pGoodsInfoBase) {

  FILE *pGoodsInfoFile;
  FILE *pCleanGoodsInfoFile;
  int i = pGoodsInfoBase->partNum;
  if ((pGoodsInfoFile = fopen(fileName, "r")) == NULL) {
    return ERROR;
  }
  while (!feof(pGoodsInfoFile)) {
    pGoodsInfoBase->pGoodsInfoBase[i] = (Part *)malloc(sizeof(Part));
    if (pGoodsInfoBase->pGoodsInfoBase[i] == NULL) {
      return ERROR;
    }
    fscanf(pGoodsInfoFile, "%d\t\t%[ a-zA-Z]\t\t\t%d\t\t%f",
           &pGoodsInfoBase->pGoodsInfoBase[i]->number,
           pGoodsInfoBase->pGoodsInfoBase[i]->partName,
           &pGoodsInfoBase->pGoodsInfoBase[i]->onHand,
           &pGoodsInfoBase->pGoodsInfoBase[i]->price);
    fgetc(pGoodsInfoFile);
    i++;
    pGoodsInfoBase->partNum++;
  }
  fclose(pGoodsInfoFile);
  pGoodsInfoBase->partNum--;
  if ((pCleanGoodsInfoFile = fopen(fileName, "w")) == NULL) {
    return ERROR;
  }
  fprintf(pCleanGoodsInfoFile, "");
  fclose(pCleanGoodsInfoFile);
  return OK;
}

Status ExitSSOS(char *fileName, GoodsInfoBase *pGoodsInfoBase) {
  FILE *pPrintFile;
  int i, n;
  if ((pPrintFile = fopen(fileName, "a+")) == NULL) {
    return ERROR;
  }
  n = pGoodsInfoBase->partNum;
  for (i = 0; i < n; i++) {
    fprintf(pPrintFile, "%d\t\t%s\t\t%d\t\t%g\n",
            pGoodsInfoBase->pGoodsInfoBase[i]->number,
            pGoodsInfoBase->pGoodsInfoBase[i]->partName,
            pGoodsInfoBase->pGoodsInfoBase[i]->onHand,
            pGoodsInfoBase->pGoodsInfoBase[i]->price);
  }
  fclose(pPrintFile);
  //    for (i = 0; i < n;i++){
  //        free(pGoodsInfoBase->pGoodsInfoBase[i]);
  //        pGoodsInfoBase->pGoodsInfoBase[i] = NULL;
  //    }
  return OK;
}

Status Print(GoodsInfoBase *pGoodsInfoBase) {
  int i;
  printf("Part Number\tPart Name\tQuantity on hand\tPrice\n");
  for (i = 0; i < pGoodsInfoBase->partNum; i++) {
    PrintInfoOfAPart(i, pGoodsInfoBase);
  }
  return OK;
}

Status UpdataOnHand(int changeOnHand, GoodsInfoBase *pGoodsInfoBase, int i) {
  pGoodsInfoBase->pGoodsInfoBase[i]->onHand += changeOnHand;
  return OK;
}

Status UpdataPrice(float newPrice, GoodsInfoBase *pGoodsInfoBase, int i) {
  pGoodsInfoBase->pGoodsInfoBase[i]->price = newPrice;
  return OK;
}

Status Insert(Part *pNewPart, GoodsInfoBase *pGoodsInfoBase) {
  int i;
  i = pGoodsInfoBase->partNum;

  pGoodsInfoBase->pGoodsInfoBase[i] = (Part *)malloc(sizeof(Part));
  if (pGoodsInfoBase->pGoodsInfoBase[i] == NULL) {
    return ERROR;
  }
  *pGoodsInfoBase->pGoodsInfoBase[i] = *pNewPart;
  pGoodsInfoBase->partNum++;
  return OK;
}

Status Delete(int i, GoodsInfoBase *pGoodsInfoBase) {
  int n;
  n = pGoodsInfoBase->partNum;
  free(pGoodsInfoBase->pGoodsInfoBase[i]);
  for (; i < n - 1; i++) {
    pGoodsInfoBase->pGoodsInfoBase[i] = pGoodsInfoBase->pGoodsInfoBase[i + 1];
  }
  pGoodsInfoBase->pGoodsInfoBase[i] = NULL;
  pGoodsInfoBase->partNum--;
  return OK;
}

int main(void) {
  GoodsInfoBase goodsInfoBase;
  goodsInfoBase.partNum = 0;
  char operationCode;
  char fileName[MAX_LEN + 1] = {"C:\\Users\\HP\\Desktop\\GoodsInfoBase.txt"};
  Part NewPart;
  Bool isFull;
  Bool isIllegalInput;
  int searchNumber;
  int i;
  int updataCode;
  int changeOnHand;
  float newPrice;
  int scanfReturnValue[3];

  PrintInfoOfCopyright();
  PrintInfoOfInstructions();
  ReadGoodsInfoFile(fileName, &goodsInfoBase);

  while (1) {

    printf("Enter operation code: ");
    scanf(" %c", &operationCode);

    switch (operationCode) {

    case 'i':
      isFull = IsFull(&goodsInfoBase);
      if (isFull == YES) {
        printf("Goods is full.");
        break;
      }
      while (1) {
        printf("Enter part number: ");
        scanfReturnValue[0] = scanf("%d", &NewPart.number);
        fflush(stdin);
        printf("Enter part name: ");
        gets(NewPart.partName);
        fflush(stdin);
        printf("Enter quantity on hand: ");
        scanfReturnValue[1] = scanf("%d", &NewPart.onHand);
        fflush(stdin);
        printf("Enter price: ");
        scanfReturnValue[2] = scanf("%f", &NewPart.price);
        isIllegalInput =
            IsIllegalInput(scanfReturnValue[0], NewPart.partName[0],
                           scanfReturnValue[1], scanfReturnValue[2]);
        if (!isIllegalInput) {
          break;
        }
        printf("Illegal input!\n");
        fflush(stdin);
      }
      Insert(&NewPart, &goodsInfoBase);
      break;

    case 'd':
      printf("Enter part number: ");
      scanf("%d", &searchNumber);
      i = Search(searchNumber, &goodsInfoBase);
      if (i >= 0) {
        Delete(i, &goodsInfoBase);
      } else {
        printf("Part not found.\n");
      }
      break;

    case 's':
      printf("Enter part number: ");
      scanf("%d", &searchNumber);
      i = Search(searchNumber, &goodsInfoBase);
      if (i >= 0) {
        PrintInfoOfAPart(i, &goodsInfoBase);
      } else {
        printf("Part not found.\n");
      }
      break;

    case 'u':
      printf("Enter part number: ");
      scanf("%d", &searchNumber);
      i = Search(searchNumber, &goodsInfoBase);

      if (i >= 0) {
        printf("Enter what information you want to updata(0: onHand, 1: "
               "price): ");
        scanf("%d", &updataCode);

        if (updataCode == 0) {
          printf("Enter change in quantity on hand: ");
          scanf("%d", &changeOnHand);
          UpdataOnHand(changeOnHand, &goodsInfoBase, i);
        } else if (updataCode == 1) {
          printf("Enter new price: ");
          scanf("%f", &newPrice);
          UpdataPrice(newPrice, &goodsInfoBase, i);
        } else {
          printf("Wrong updata code!");
        }
      } else {
        printf("Part not found.\n");
      }
      break;

    case 'p':
      Print(&goodsInfoBase);
      break;

    case 'q':
      ExitSSOS(fileName, &goodsInfoBase);
      exit(1);

    default:
      PrintInfoOfInstructions();
    }

    fflush(stdin);
    printf("\n");
  }
  return 0;
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 213,928评论 6 493
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 91,192评论 3 387
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 159,468评论 0 349
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 57,186评论 1 286
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 66,295评论 6 386
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 50,374评论 1 292
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 39,403评论 3 412
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,186评论 0 269
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,610评论 1 306
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 36,906评论 2 328
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,075评论 1 341
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 34,755评论 4 337
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 40,393评论 3 320
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,079评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,313评论 1 267
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 46,934评论 2 365
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 43,963评论 2 351