超市管理系统完整代码
2018-01-02 本文已影响0人
crabor
#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;
}