程序清单(2):admin.c
/*admin.c:管理员功能选择界面,根据客户选项调用相应函数实现系统功能*/
#include "shead.h"
#include "creat.c"
#include "add.c"
#include "delete.c"
#include "modnum.c"
#include "qname.c"
#include "qseatnum.c"
#include "smprice.c"
void Admin( )
{
int choice = 0;/*存放客户选项的变量*/
/*====功能及操作的界面提示====*/
while(1)
{
printf("--------------------------------------------------------------------------------------\n");
printf("| |\n");
printf("| Welcome to Product Management System |\n");
printf("| |\n");
printf("|-------------------------------------------------------------------------------------|\n");
printf ("| 1. Init Product Information File |\n");
printf("| 2. AddProduct( ) |\n");
printf("| 3. DelProdut( ) |\n");
printf("| 4. QueryByProductName( ) |\n");
printf("| 5. QueryByProductNum( ) |\n");
printf("| 6. ModifyByProductNumber( ) |\n");
printf("| 7. SortByMemberPrice( ) |\n");
printf("+++++++++++++++++++++++++++++++++++++++++++++++++++\n");
printf("| System User Management |\n");
printf("| 11. CreatNewUserFile( ) |\n");
printf("| 12. AddUser( ) |\n");
printf("| 13. DelUser( ) |\n");
printf("| 14. ModifyUser( ) |\n");
printf("| 15. SortByUserNum( ) |\n");
printf("+++++++++++++++++++++++++++++++++++++++++++++++++++\n");
printf("| 0. Exit System |\n");
printf("----------------------------------------------------------------------------------------\n");
printf("# Please Input Your Choose #\n");
printf("# number 1~7 to Mange the Product Information #\n");
printf("# number 11,12,13,14,15 to Mange the System User #\n");
printf("# number 0 to Exit the System #\n");
printf("-----------------------------------------------------------------------------------------\n");
scanf("%d",&choice);
getchar( );
/*根据客户选项调用相应函数*/
switch(choice)
{
case 1:
CreatFile( );
break;
case 2:
AddRecord( );
break;
case 3:
DelRecord( );
break;
case 4:
QueryByName( );
break;
case 5:
QueryBySeatNum( );
break;
case 6:
ModifyByNumber( );
break;
case 7:
SortByMemberPrice( );
break;
case 11:
CreatUser( );
break;
case 12:
AddUser( );
break;
case 13:
DelUser( );
break;
case 14:
ModifyByUserNumber( );
break;
case 15:
SortByUserNum( );
break;
case 0:
exit(0);
default:
break;
}
}
}
程序清单(3):creat.c
/*creat.c:用于创建商品信息文件*/
#include "stdio.h"
/*函数CreatFile*/
void CreatFile( )
{
FILE *fp = NULL; /*定义指向文件的指针*/
product Tmps; /*定义进行操作时存放结构体变量的*/
char DataFile[40] = ""; /*存储商品信息的文件名*/
int count = 1; /*计算可输入数据的最大范围*/
/*====输入存放商品信息的文件名====*/
printf("\n please input new file name of product information.");
printf("\n Notice:Name of file can't exceed 8 characters.suffix can't
exceed 3 characters,part of exceed will be discarded.\n");
gets(DataFile);
/*如顾客没有输入,则循环提示顾客输入*/
while(*DataFile == ('\0'))
{
printf("\n please input new file name to store data,end with enter.");
printf("\n Notice:Name of file can't exceed 8 characters,suffix can't
exceed 3 characters.part of exceed will be discarded.\n");
gets(DataFile);
}
/*用二进制写的方式打开文件,即创建文件*/
fp = fopen(DataFile,"wb+");
/*如果当前文件不存在,提示打开文件失败*/
if (fp == NULL)
{
printf("\n Open file %s fail!End with any key.\n",DataFile);
printf("Open file fail");
gatch( );
exit(1);
}
/*如果成功打开或创建文件,则提示输入商品序号、名称、价格要素等相关信息*/
printf("input product information record.number is 0 means input is end.\n");
printf("Number is not exceed 9 figures,Name is not exceed 20 characters,range of grade:0.00~1000.00\n");
/*循环从键盘上读取顾客输入的序号、名称、价格要素等相关信息*/
while(count <= SIZE)
{
/*输入序号,如为0则停止输入*/
printf("\n input 'number = 0' means end input .\n");
printf("number=");
scanf("%ld",&Tmps.Number);
if (Tmps.Number == 0 )
break;
printf("\n");
/*如遇无法写入文件的异常,则加以提示*/
if(fwrite(&TmpS,sizeof(product),1,fp)!=1
{
printf("\nwrite file %s fail!End with any key\n",DataFile);
perror("write file fail ");
getch( );
exit(1);
}
}
count++;
}
/*如果输入的数据量超过最大允许的范围,则提示数据不能录入*/
if (count>SIZE)
printf(*\nsorry,number of data can not exceed%d\n",SIZE);
fclose(fp);
/*====在屏幕上显示文件内容====*/
/* clrscr( );*/
printf(*The data you input is store successful %s in file.\n*,DataFile);
printf("Countent as follow:\n");
fp=fpen(DataFile,"rb");
if (fp==NULL)
{
printf("\nopen file%sfail!End with any key \n",DataFile);
perror("Open file fail");
getch( );
exit(1);
}
fciose(fp);
}
程序清单(4):add.c
/*add.c:添加商品信息记录*/
#include "stdio.h"
void AddRecord( )
{
FILE *fp = NULL; /*定义指向文件的指针*/
product TmpS; /*定义进行操作时的临时结构体变量*/
char DataFile[40] = ""; /*存储商品信息的文件名*/
int count = 1; /*计算可输入数据的最大范围*/
/*====输入要添加商品信息的文件名====*/
printf("\n please input the product information file name to add record:");
printf("\n Notice:Name of file can'texceed 8 characters.suffix can't
exceed 3 characters,part of exceed will be discarded.\n");
gets(DataFile);
/*如顾客没有输入,则循环提示顾客输入*/
while(*DataFile == ('\0'))
{
printf(*\n please input new file name to store data,end with enter.");
printf("\n Notice:Name of file can'texceed 8 characters,suffix can't
exceed 3 characters.part of exceed will be discarded.\n");
gets(DataFile);
}
fp = fopen (DataFile,"a+");/*a+:当文件存在时,追加,当文件不存在时,创建*/
/*如果当前文件不存在,提示打开文件失败*/
if (fp == NULL)
{
printf(*\n Open file %s fail!End with any key.\n*,DataFile);
perror("Open file fail");
getch( );
exit(1);
}
/*如果成功打开或创建文件,则提示输入商品序号、名称、价格要素等相关信息*/
printf("input number,name and salary.number is 0 means input is end.\n");
printf("Number is not exceed 9 figures,Name is not exceed 20 characters,range of grade:0.00~1000.00\n");
/*循环从键盘上读取顾客输入的序号、名称、价格要素等相关信息*/
while(count <=SIZE)
{
/*输入序号,如为0则停止输入*/
printf("\n input 'number =0' means end input.\n");
printf("number=");
scanf("%ld",&TmpS.Number);
if (TmpS.Number == 0 )
break;
/*提示输入商品名称*/
printf("name=");
scanf("%s",TmpS.price);
printf("\n");
/*如遇无法写入文件的异常,则加以提示*/
if(fwrite(&TmpS,sizeof(product),1,fp)!=1)
{
printf("\nwrite file %s fail!End with any key\n",DataFile);
perror("Write file fail ");
getch( );
exit(1);
}
count++;
}
/*如果输入的数据量超过最大允许的范围,则提示数据不能录入*/
if (count>SIZE)
printf("\nsory,number of data can not exceed%d\n",SIZE);
fclose(fp);
/*====在屏幕上显示文件内容====*/
/* clrscr( );*/
printf("The data you input is store successful %s in file.\n",DataFile);
printf("Countent as follow:\n");
fp=fopen(DataFile,"rb");
if (fp == NULL)
{
printf("\nOpen file%sfail!End with any key \n",DataFile);
perror("Open file fail");
getch( );
exit(1);
}
fclose(fp);
}
程序清单(5):delete.c
/*delete.c: 删除商品信息记录*/
#include "stdio.h"
void DelRecord( )
{
int i,j,k;
long delnum; /*存放顾客输入的要删除商品序号*/
product TmpS; /*定义进行操作时的临时结构体变量*/
product s[SIZE]; /*SIZE,在shead.h头文件中定义的常量,值为100*/
int recNumber; /*原文件中的记录数*/
char DataFile[40] = "",next; /*DataFile存储商品信息的文件名,next为是否进行下一次删除操作的选项*/
FILE *fp;/*====fp指针指向存储数据的文件名====*/
printf("\nplease input the name of production information file.\n");
gets(DataFile);
/*提示客户输入要进行删除记录的文件名*/
while(*DataFile == ('\0'))
{
printf("\nplease input the name of production information file.\n");
gets(DataFile);
}
begin:
/*以二进制读的方式打开文件*/
fp=fopen(DataFile,"rb");
if (fp == NULL)
{
printf("\nOpen file %s fail!End with any key\n",DataFile);
perror("Open file fail");
getch( );
exit(1);
}
/*输入要删除的商品序号*/
printf("please input the Product's seatnum which you will delete:");
scanf("%ld",&delnum);
printf("the product you will delete is:%ld\n",delnum);
/*将文件中信息存入结构体数组*/
/*与要删除的商品序号相匹配的项不写入数组,循环后数组中即为去掉了要删除记录后的剩余记录*/
recNumber=0;
while((fread(&TmpS,sizeof(product),1,fp)) != (int)NULL)
{
if(TmpS.Number!=delnum)
{
s[recNumber].Number = TmpS.Number;
strcpy(s[recNumber].Name,TmpS.Name);
s[recNumber].price = TmpS.price;
s[recNumber].discount = TmpS.discount;
s[recNumber].memberprice = TmpS.memberprice;
recNumber++;
}
}
fclose(fp);
/*====将删除后的剩余结构体记录写入文件====*/
fp=fopen(DataFile,"wb+");
if (fp == NULL)
{
printf("\nSet up file %sfail !end with angkey.\n",DataFile);
perror("Set up fail");
getch( );
exit(1);
}
for(i=0; i
{
if(fwrite(&s,sizeof(product),1,fp)!=1)
{
printf("\nWrite file %s fail!end with angkey.\n",DataFile);
perror("Write file fail!");
getch( );
exit(1);
}
}
fcolse(fp);
/*====显示删除后的文件====*/
fp=fopen(DataFile,"rb");
if (fp == NULL)
{
printf("\nOpen file%sfail!End with angkey \n",DataFile);
perror("Open file fail");
getch( );
exit(1);
}
printf("the file after delete is:\n");
printf("\nNumber \t\tName\tprice\tdiscount\tmemberprice\n");
while(fread(&TmpS,sizeof(product),1,fp) != (int)NULL)
{
if(TmpS.Number!=0)
printf("\n%ld\t%s\t%4.1f\t%4.1f\t%4.1f\n",TmpS.Number,
TmpS.Name,TmpS.price,TmpS.discount,TmpS.memberprice);
}
fclose(fp);
/*询问客户是否继续删除*/
printf("\nGo on ?(y/n)");
next=getche( );
putchar('\n');
if ( next =='y' || next == 'y') goto begin;
}
程序清单(6):modnum.c
/*********************************************************************/
/* monnum.c : 修改商品信息记录 */
/********************************************************************/
#include "stdio.h"
void ModifyByNumber( )
{
int i,j,k;
long modnum; /*存储客户输入的要修改的商品序号*/
/*输入各项修改后的信息*/
long Number;
char Name[20];
float price;
float discount;
float memberprice;
product TmpS; /*定义进行操作时的临时结构体变量*/
product s[SIZE];/*SIZE,在shead.h头文件中定义的常量,值为100 */
int recNumber;
char DataFile[40] = "",next;
/*DataFile存储商品信息的文件名,next为是否进行下一次删除操作的选项*/
FILE *fp;/*====fp指针指向存储数据的文件名====*/
/*提示客户输入要进行修改记录的文件名*/
printf("\nplease input the name of file where data is stored,
end with enter key.\n");
gets(DataFile);
/*提示客户输入要进行修改记录的文件名*/
while(*DataFile == ('\0'))
{
printf("\nplease input the name of file where data is stored,
end with enter key.\n");
gets(DataFile);
}
begin:
/*以读的方式打开文件,如文件不存在,提示错误*/
fp=fopen(DataFile,"rb");
if (fp == NULL)
{
printf("\nOpen file %s fail!End with any key\n",DataFile);
perror("Open file fail");
getch( );
exit(1);
}
printf("please input the Employee'seatnum which you will modify:");
scanf("%ld",&modnum);
printf("the product you will delete is:%ld\n,modnum");
/*输入要修改记录的各项内容值*/
Number=modnum;
printf("name=");
scanf("%s",Name);