希望有高手来讲解一下这段有关商品库存管理的源代码,新手都不大懂
刚学习C语言,很多东西都是似懂非懂的,这个源代码看的云里雾里的,希望高手讲解下,或者给个结构图帮助我理解下。。谢谢了#include<CONIO.H>
#include<STDIO.H>
#include<PROCESS.H>
#include<STRING.H>
#include<DOS.H>
#define Brand struct brand
#define Item struct item
Brand /*商品品牌类 */
{
char szBrndName[20]; /*名称*/
int nBrndCode; /*代码*/
int nBrndQuan; /*数量*/
float flBrandPrice; /* 价格*/
};
Item /*商品种类类*/
{
char szItemName[10]; /*名称*/
int nItemCode; /*代码*/
char szColour[10]; /*颜色*/
int nItemQuan; /* 数量*/
Brand strBrnd[3]; /*品牌*/
};
void GetBrandData_f(Brand *); /*输入商品品牌*/
void ShowBrandData_f(Brand *); /*显示商品品牌*/
void AddItem_f(); /*增加商品种类*/
void RemoveItem_f(); /* 去掉商品种类*/
void SearchItem_f(); /* 查询商品种类*/
void ShowAllBr_f(); /*显示种类清单*/
void GetItemData_f( Item *); /* 输入商品种类*/
void ShowItemData_f(Item *); /*显示商品种类*/
/* GET Brand DATA */
/* gets data about the Brandt it temperary storage */
void GetBrandData_f(Brand *strpBr)
{
getchar();
sin(10);
printf("\n\nEnter Brand Name ");
gets(strpBr->szBrndName);
printf("\nEnter Brand Code ");
scanf("%d", &strpBr->nBrndCode);
printf("\nEnter Quantity ");
scanf("%d", &strpBr->nBrndQuan);
printf("\nEnter Price");
scanf("%f", &strpBr->flBrandPrice);
}
/* SHOW Brand DATA */
/* shows the data of the Brand */
void ShowBrandData_f(Brand *strpBr)
{
printf("\n\nName : ");
puts(strpBr->szBrndName);
printf("\n\nCode : ");
printf("%d", strpBr->nBrndCode);
printf("\n\nQuantity : ");
printf("%d", strpBr->nBrndQuan);
printf("\nPrice : ");
printf("%f", strpBr->flBrandPrice);
}
/* GET Item DATA */
/* gets data about the Item it is temporary storage */
void GetItemData_f(Item *strpIm)
{
getchar();
printf("\nEnter Item Name ");
gets(strpIm->szItemName);
printf("\nEnter Item Code ");
scanf("%d", &strpIm->nItemCode);
getchar();
printf("\nEnter Colour ");
gets(strpIm->szColour);
printf("\nEnter Quantity ");
scanf("%d", &strpIm->nItemQuan);
printf("\n\nEnter Brand 1 ");
GetBrandData_f(&strpIm->strBrnd[0]);
printf("\n\nEnter Brand 2 ");
GetBrandData_f(&strpIm->strBrnd[1]);
printf("\n\nEnter Brand 3 ");
GetBrandData_f(&strpIm->strBrnd[2]);
}
/* SHOW Item DATA */
/* shows data about the Item */
void ShowItemData_f(Item *strpIm)
{
printf("\n\n\t\tItem Data");
printf("\n\nItem Name : ");
puts(strpIm->szItemName);
/* 写一个字符串到标准的输出流(或缓冲区)-Write a string to stdout */
printf("\n\nItem Code : ");
printf("%d", strpIm->nItemCode);
printf("\n\nColour : ");
puts(strpIm->szColour);
printf("\n\nQuantity : ");
printf("%d\n",strpIm->nItemQuan);
getch();
/* clrscr(); */
printf("\n\nBrand 1\n");
ShowBrandData_f(&strpIm-> strBrnd[0]);
getch();
/* clrscr(); */
printf("\n\nBrand 2\n");
ShowBrandData_f(&strpIm-> strBrnd[1]);
/* clrscr(); */
printf("\n\nBrand 3\n");
ShowBrandData_f(&strpIm-> strBrnd[2]);
}
/* Add Item */
/* this function first gets data into temp storage then saves it on the hard disk */
void AddItem_f()/*将新增商品写到数据文件中*/
{
/* clrscr(); */
Item strSamp;
FILE *fp;
if((fp=fopen( "TIS.txt", "ab"))==NULL)
{ /* if file could not be opened */
printf("Error Could Not Open File" );
getch();
exit(1);
}
GetItemData_f(&strSamp); /* Get Data From User For Temp. Storage */
/* write data to hard disk */
fwrite((char *)&strSamp, sizeof(Item), 1, fp);
fclose(fp);
}
/* TRAVERSE */
/* this function shows the list of all the Brands */
void ShowAllBr_f() /*显示数据文件中的所有商品种类及品牌*/
{
/* clrscr(); */
Item strSamp;
FILE *fp;
if((fp=fopen( "TIS.txt", "rb"))==NULL)
{ /* if file could not be opened */
printf("Error Could Not Open File" );
getch();
exit(1);
}
fread((char *)&strSamp, sizeof(Item), 1, fp);
/* Store Data In Object */
while( !feof(fp) ) /* Untill Contacts Present */
{
ShowItemData_f(&strSamp) ; /* Display on Screen */
getch(); /* Wait For Keyboard Input */
fread((char *)&strSamp, sizeof(Item), 1, fp); /* Read Next Contact */
}
fclose(fp);
}
/* SEARCH */
void SearchItem_f()/*按商品的名称或代码查询商品*/
{
char szImName[10];
FILE *fp;
int nImCode;
int nChoice;
int nFlag=0; /* nFlag To Check If Found */
Item strSamp;
printf("\n(1)Search By Name\n(2)Search By Code");
scanf("%d", &nChoice);
if(nChoice==1)
{
getchar();
printf("\nEnter Item name ");
gets(szImName);
nImCode=0;
}
else
{
printf("\nEnter Code of Item To Search ");
scanf("%d", &nImCode);
strcpy(szImName,"null");
}
if((fp=fopen( "TIS.txt", "rb"))==NULL)
{ /* if file could not be opened */
printf("Error Could Not Open File" );
getch();
exit(1);
}
fread((char *)&strSamp, sizeof(Item), 1, fp);
/* Read Data To Object */
while( !feof(fp) ) /* Untill There Are Contacts */
{
if((strcmp(strSamp.szItemName, szImName)==0) || (strSamp.nItemCode==nImCode))
/*Compare */
{
ShowItemData_f(&strSamp);
getch();
nFlag=1; /* Set Flag */
break; /* Break Loop */
}
fread((char *)&strSamp, sizeof(Item), 1, fp); /* Read Next Contact */
}
if(nFlag !=1) /* If Not Found */
{
printf("Item Not Found");
getch();
}
fclose(fp);
}
/* REMOVE Item */
/* 从数据文件中去掉某一品种。将商品数据文件拷贝到一临时文件中(要去掉的商品不拷贝),然后再将临时文件的所有内容写回到原数据文件*/
void RemoveItem_f()
{
FILE *fp, *ftemp;
char szImName[10];
int nImCode;
int nChoice;
Item strSamp;
printf("\n(1)Delete By Name\n(2)Delete By Code");
scanf("%d",&nChoice);
if(nChoice==1)
{
getchar();
printf("\nEnter Item name ");
gets(szImName);
nImCode=0;
}
else
{
printf("\nEnter Code of Item To Search ");
scanf("%d", &nImCode);
strcpy(szImName,"null");
}
if((fp=fopen( "TIS.txt", "rb"))==NULL)
{ /* if file could not be opened */
printf("Error Could Not Open File" );
getch();
exit(1);
}
if((ftemp=fopen("delete.txt", "wb"))==NULL)
{ /* if file could not be opened */
printf("Error Could Not Open File" );
getch();
exit(1);
}
fseek(fp,0,0);
fseek(ftemp,0,0);
fread((char *)&strSamp, sizeof(Item), 1, fp);
while( !feof(fp) )
{
if( (strcmp(szImName, strSamp.szItemName)==0) || (nImCode==strSamp.nItemCode))
{
fread((char *)&strSamp, sizeof(Item), 1, fp);
}
else
{
fwrite((char *)&strSamp, sizeof(Item), 1, ftemp);
fread((char *)&strSamp, sizeof(Item), 1, fp);
}
}
fclose(fp);
fclose(ftemp);
if((fp==fopen( "TIS.txt", "wb"))==NULL)
{ /* if file could not be opened */
printf("Error Could Not Open File" );
getch();
exit(1);
}
if((ftemp==fopen("delete.txt", "rb"))==NULL)
{ /* if file could not be opened */
printf("Error Could Not Open File" );
getch();
exit(1);
}
fseek(fp,0,0);
fseek(ftemp,0,0);
fread((char *)&strSamp, sizeof(Item), 1, ftemp);
while( !feof(ftemp) )
{
fwrite((char *)&strSamp, sizeof(Item), 1, fp);
fread((char *)&strSamp, sizeof(Item), 1, ftemp);
}
fclose(fp);
fclose(ftemp);
}
/* MAIN FUNCTION */
void main()
{
Item strSamp;
int nChoice;
while(1)
{
printf("\n\tThe Inventory System (TIS)");
printf("\n\n(1)Add Item\n\n(2)Show All Data");
printf("\n\n(3)Search Item\n\n(4)Remove Item\n\n(5)Exit TIS");
printf("\n\nEnter Choice ");
scanf("%d", &nChoice);
switch(nChoice)
{
case 1:
AddItem_f();
break;
case 2:
ShowAllBr_f();
break;
case 3:
SearchItem_f();
break;
case 4:
RemoveItem_f();
break;
case 5:
{
exit(1);
}
}
}
}