商品代码

#include
#include
#include
#include
#include
#define SIZE 100
typedef struct
{
long Number;/*编号*/
char Name[20];/*名称*/
float price;/*价格*/
float discount;/*折扣*/
float memberprice;/*会员*/
}product;
extern void CreaFile();/*创建信息文件*/
extern void tianjia();/*添加商品信息*/
extern void bianhaochazhao();/*按编号查找信息*/
extern void namechazhao();/*按名称查找信息*/
extern void xiugai();/*修改信息*/
extern void shanchu();/*删除信息*/
extern void xianshi();/*显示所有信息*/
extern void sortbyMember();/*按合计查询*/
void main()
{
int menu=0;/*存放客户选项的变量*/
while(1)
{
printf("*******************************************************\n");
printf(" 欢迎使用商品管理系统 \n");
printf("-------------------------------------------------------\n");
printf(" 1、Init Product Information File \n");
printf(" 2、添加商品信息 \n");
printf(" 3、按编号查找信息 \n");
printf(" 4、按名称查找信息 \n");
printf(" 5、修改信息 \n");
printf(" 6、删除信息 \n");
printf(" 7、显示所有信息 \n");
printf(" 8、按合计查询 \n");
printf(" 0、 Exit System \n");
printf("-------------------------------------------------------\n");
printf(" 请您选择0^8: \n ");
scanf("%d",&menu);
getchar();
switch(menu)
{
case 1:{CreaFile();break;}
case 2:{tianjia();break;}
case 3:{bianhaochazhao();break;}
case 4:{namechazhao();break;}
case 5:{xiugai();break;}
case 6:{shanchu();break;}
case 7:{xianshi();break;}
case 8:{sortbyMember();break;}
case 0:exit(0);
default:break;
}
}
}
void CreaFile()/*创建信息文件*/
{
FILE *fp=NULL;/*指向文件的指针*/
product q;/*定义进行操作时存放结构体变量的*/
char s[20]="";/*存储商品信息的文件名*/
int count=1;/*计算输入数据的最大范围*/
gets(s);
fp=fopen(s,"wb+");
if(fp==NULL)
{
printf("\n Open file %s fail!End with any key.\n",s);
perror("Open file fail");
getch();
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,rang of grade:0.00~1000.00\n");
while(count<=SIZE)
{
printf("\n input 'number=0' means end input:\n");
printf("number=");
scanf("%ld",&q.Number);
if(q.Number==0)
break;
printf("name=");
scanf("%s",q.Nam

e);
printf("price=");
scanf("%f",&q.price);
printf("discount=");
scanf("%f",&q.discount);
q.memberprice=q.price*q.discount;
printf("\n");
if(fwrite(&q,sizeof(product),1,fp)!=1)/*如果无法写入文件的异常,则加以提示*/
{
printf("\nwrite file %s fail!End with any key\n",s);
perror("Write file fail ");
getch();
exit(1);
}
count++;
}
printf("The data you input is store successful %s in file.\n",s);
printf("Content as follow:\n");
fclose(fp);
fp=fopen(s,"rb+");
if(fp==NULL)
{
printf("\nOpen file %s fail!End with any key \n",s);
perror("Open file fail");
getch();
exit(1);
}
printf("\nNumber\tName\tprice\tdiscount\tmemberprice\n");
while(fread(&q,sizeof(product),1,fp)!=(int)NULL)
{
printf("\n%ld\t%s\t%4.2f\t%4.2f\t\t%4.2f\n",q.Number,https://www.360docs.net/doc/e37541049.html,,q.price,q.discount,q.memberprice);
}
fclose(fp);
}
void tianjia()/*添加商品信息*/
{
FILE *fp=NULL;
product q;
char s[40]="";
int count =1;
gets(s);
fp=fopen(s,"a+");
if(fp==NULL)
{
printf("\n Open file %s fail!End with any key.\n",s);
perror("Open file fil");
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,rang of grade:0.00~1000.00\n");
while(count<=SIZE)
{
printf("\n input 'number=0' means end input.\n");
printf("number=");
scanf("%ld",&q.Number);
if(q.Number==0)
break;
printf("name=");
scanf("%s",https://www.360docs.net/doc/e37541049.html,);
printf("price=");
scanf("%f",&q.price);
printf("discount=");
scanf("%f",&q.discount);
q.memberprice=q.price*q.discount;
printf("\n");
if(fwrite(&q,sizeof(product),1,fp)!=1)/*如果无法写入文件的异常,则加以提示*/
{
printf("\nwrite file %s fail!End with any key\n",s);
perror("Write file fail ");
getch();
exit(1);
}
count++;
}
fclose(fp);
printf("The data you input is store successful %s in file.\n",s);
printf("cotent as follow:\n");
fp=fopen(s,"rb");
if(fp==NULL)
{
printf("\nOpen file %s fail!End with any key \n",s);
perror("Open file fail");
getch();
exit(1);
}
printf("\nNumber\tName\tprice\tdiscount\tmemberprice\n");
while(fread(&q,sizeof(product),1,fp)!=(int)NULL)
{
printf("\n%ld\t%s\t%4.2f\t%4.2f\t%4.2f\n",q.Number,https://www.360docs.net/doc/e37541049.html,,q.price,q.discount,q.memberprice);
}
fclose(fp);
}
void bianhaochazhao()/*按编号查找信息*/
{
int result=0;/*"result=1"说明查询成功,反之查找失败*/
product q;
int Bianhao;/*用户输入要查询的商品编号*/
char s[40]="",next;/*s存储商品信息的文件名,next为是否进行下一次删除操作的选项*/
FILE *fp=NULL;
printf("\nplease input the number of product information file.\n");
gets(s);
while(*s==('\0'))
{
printf("\nplease input the number of product information file.\n");
gets(s);
}
/*提示用户输入要查询的商品编号*/
begin:

result=0;
printf("Please input the number of product which needs look up(under20 bit figure).\n");
scanf("%ld",&Bianhao);
getchar();
/*以读方式打开文件*/
fp=fopen(s,"r");
if(fp==NULL)
{
printf("\nOpen file %s fail! End with any key.\n",s);
perror("Open file fail");
getch();
exit(1);
}
while(feof(fp)==0)/*循环查找和输入编号相匹配的商品信息记录,如果查找到了,则输出结果*/
{
if(fread(&q,sizeof(product),1,fp)!=(int)NULL)
{
if(q.Number==Bianhao)
{
printf("\n Find:\n");
printf("\nNumber=%ld Name=%s price=%4.1f discount=%4.1f memberprice=%4.1f\n",q.Number,https://www.360docs.net/doc/e37541049.html,,q.price,q.discount,q.memberprice);
result=1;/*说明找到了该商品的对应信息*/
}
}
}
fclose(fp);
if(result==0)
{
printf("There is not data of this product in the file!");
}
printf("\n Go on ?(y/n)");
next=getchar();
putchar('\n');
if(next=='y'|| next=='Y') goto begin;
}
void namechazhao()/*按名称查找信息*/
{
int result=0;/*"result=1"说明查询成功,反之查找失败*/
product q;
char Xingming[20];/*用户输入要查询的商品名称*/
char s[40]="",next;/*s存储商品信息的文件名,next为是否进行下一次删除操作的选项*/
FILE *fp=NULL;
printf("\nplease input the number of product information file.\n");
gets(s);
while(*s==('\0'))
{
printf("\nplease input the number of product information file.\n");
gets(s);
}
/*提示用户输入要查询的商品编号*/
begin:
result=0;
printf("Please input the number of product which needs look up(under20 bit figure).\n");
scanf("%s",&Xingming);
getchar();
/*以读方式打开文件*/
fp=fopen(s,"r");
if(fp==NULL)
{
printf("\nOpen file %s fail! End with any key.\n",s);
perror("Open file fail");
getch();
exit(1);
}
while(feof(fp)==0)/*循环查找和输入编号相匹配的商品信息记录,如果查找到了,则输出结果*/
{
if(fread(&q,sizeof(product),1,fp)!=(int)NULL)
{
if(strcmp(https://www.360docs.net/doc/e37541049.html,,Xingming)==0)
{
printf("\n Find:\n");
printf("\nNumber=%ld Name=%s price=%4.1f discount=%4.1f memberprice=%4.1f\n",q.Number,https://www.360docs.net/doc/e37541049.html,,q.price,q.discount,q.memberprice);
result=1;/*说明找到了该商品的对应信息*/
}
}
}
fclose(fp);
if(result==0)
{
printf("There is not data of this product in the file!");
}
printf("\n Go on ?(y/n)");
next=getchar();
putchar('\n');
if(next=='y'|| next=='Y') goto begin;
}
void xiugai()/*修改信息*/
{
int i,j,k;
long modnum;/*输入要修改的商品编号*/
long Number;/*输入各项修改后的信息*/
char Name[20];
float price;
float discount;
float memberprice;
product q;
product p[SIZE];
int recNumber;
char s[40]="",next;
FILE *fp;
printf("\nplease input the name of file where data is stored,end with enter key.\n");
gets(s);
/*输入要进行修改记录的文件名*/
while(*s==

('\0'))
{
printf("\nplease input the name of file where data is stored,ed with enter key.\n");
gets(s);
}
begin:
/*以读的方式打开文件,如文件不存在,提示错误*/
fp=fopen(s,"rb");
if(fp==NULL)
{
printf("\nOpen file %s fail!End with any key\n",s);
perror("Open file fail");
getch();
exit(1);
}
printf("please input the Employee'seatnum which you will modify:");
scanf("%1d",&modnum);
printf("the product you will delete is :%ld\n",modnum);
/*输入修改记录的各项内容值*/
Number=modnum;
printf("name=");
scanf("%s",Name);
printf("price=");
scanf("%f",&price);
printf("discount=");
scanf("%f",&discount);
memberprice=q.price*q.discount;
/*将文件中要修改的信息存入结构体数组*/
recNumber=0;
while(fread(&q,sizeof(product),1,fp)!=(int)NULL)
{
if(q.Number!=modnum)
{
p[recNumber].Number=q.Number;
strcpy(p[recNumber].Name,https://www.360docs.net/doc/e37541049.html,);
p[recNumber].price=q.price;
p[recNumber].discount=q.discount;
p[recNumber].memberprice=q.memberprice;
recNumber++;
}
else
{
p[recNumber].Number=Number;
strcpy(p[recNumber].Name,Name);
p[recNumber].price=price;
p[recNumber].discount=discount;
p[recNumber].memberprice=memberprice;
recNumber++;
}
}
fclose(fp);
/*将修改后的结构体数组记录写入文件*/
fp=fopen(s,"wb+");
if(fp==NULL)
{
printf("\nSet up file %s fail !End with any key.\n",s);
perror("Set up fail");
getch();
exit(1);
}
for(i=0;i{
if(fwrite(&p[i],sizeof(product),1,fp)!=1)
{
printf("\nWrite file %s fail!End with any key.\n",s);
perror("Write file fail!");
getch();
exit(1);
}
}
fclose(fp);;
/*显示修改后的文件*/
fp=fopen(s,"rb");
if(fp==NULL)
{
printf("\nOpen file %s fail!End with any key.\n",s);
perror("Open file fail");
getch();
exit(1);
}
printf("the file after modify is :\n");
printf("\nNumber \t\tName\tprice\tdiscount\tmemberprice\n");
while(fread(&q,sizeof(product),1,fp)!=(int)NULL)
{
if(q.Number!=0)
printf("\n%ld\t%s\t%4.1f\t%4.1f%4.1f\n",q.Number,https://www.360docs.net/doc/e37541049.html,,q.price,q.discount,q.memberprice);
}
fclose(fp);
/*提示是否进行下一次修改*/
printf("\nGo on?(y\n)");
next=getchar();
putchar('\n');
if(next=='y'||next=='Y') goto begin;
}
void shanchu()/*删除信息*/
{
int i,j,k;
long delnum;/*输入要删除的商品编号*/
product q;
product p[SIZE];
int recNumber;
char s[40]="",next;
FILE *fp;
printf("\nplease input the name of production information file.\n");
gets(s);
/*输入要进行删除记录的文件名*/
while(*s==('\0'))
{
printf("\nplease input the name of production information file.\n");
gets(s);
}
begin:
fp=fopen(s,"rb");
if(fp==NULL)
{
printf("\nOpen file %s fail!End with any key.\n",s);
perror("Open file fail");
getch();
exit(1);
}
printf("please input the product's seatnum which y

ou will delete:");
scanf("%ld",&delnum);
printf("the product you will delete is:%1d\n",delnum);
recNumber=0;
while((fread(&q,sizeof(product),1,fp))!=(int)NULL)
{
if(q.Number!=delnum)
{
p[recNumber].Number=q.Number;
strcpy(p[recNumber].Name,https://www.360docs.net/doc/e37541049.html,);
p[recNumber].price=q.price;
p[recNumber].discount=q.discount;
p[recNumber].memberprice=q.memberprice;
recNumber++;
}
}
fclose(fp);
/*将删除后的剩余结构体记录写入文件*/
fp=fopen(s,"wb+");
if(fp==NULL)
{
printf("\nSet up file %s fail !End with anykey.\n",s);
perror("Set up fail");
getch();
exit(1);
}
for(i=0;i{
if(fwrite(&p[i],sizeof(product),1,fp)!=1)
{
printf("\nWrite file %s fail!End with any key.\n",s);
perror("Write file fail");
getch();
exit(1);
}
}
fclose(fp);
/*显示删除后的文件*/
fp=fopen(s,"rb");
if(fp==NULL)
{
printf("\nOpen file %s fail!End with any key.\n",s);
perror("Open file fail");
getch();
exit(1);
}
printf("the file after delete is :\n");
printf("\nNumber \t\tName\tprice\tdiscount\tmemberprice\n");
while(fread(&q,sizeof(product),1,fp)!=(int)NULL)
{
if(q.Number!=0)
printf("\n%ld\t%s\t%4.1f\t%4.1f\t%4.1f\n",q.Number,https://www.360docs.net/doc/e37541049.html,,q.price,q.discount,q.memberprice);
}
fclose(fp);
/*是否继续删除*/
printf("\nGo on?(y\n)");
next=getchar();
putchar('\n');
if(next=='y'||next=='Y') goto begin;
}
void xianshi()/*显示*/
{
FILE *fp;
int i,j,k;
product q;
product p[SIZE];
int recNumber;
char s[40]="";
printf("\nplease input the name of file where data is stored,end with enter key.\n");
gets(s);
/*输入要进行排序的文件名*/
while(*s==('\0'))
{
printf("\nplease input the name of file where data is stored,end with enter key.\n");
gets(s);
}
/*以读的方式打开文件,如文件不存在,提示错误*/
fp=fopen(s,"rb");
if(fp==NULL)
{
printf("\nOpen files %s fail!End with any key\n",s);
perror("Open file fail");;
getch();
exit(1);
}
/*将文件中要排序的信息存入结构体数组*/


/*显示排序后的文件*/



printf("\nNumber \tName\tprice\tdiscount\tmemberprice\n");
while(fread(&q,sizeof(product),1,fp)!=(int)NULL)
{
printf("\n%ld\t%s\t%4.1f\t%4.1f\t%4.1f\n",q.Number,https://www.360docs.net/doc/e37541049.html,,q.price,q.discount,q.memberprice);
}
fclose(fp);
}
void sortbyMember()/*按合计查询*/
{
int i,j,k;
product q;
product p[SIZE];
int recNumber=0;
char s[40]="";
FILE *fp;
printf("\nplease input the name of product information file.\n");
gets(s);
while(*s==('\0'))
{
printf("\nplease input the name of file where data is stored,end with enter key.\n");
gets(s);
}
/*以读的方式打开文件,如文件不存在,提示错误*/
fp=fopen(s,"rb");
if(fp==NULL)
{
printf("\nOpen files %s fail!End with any key\n",s);
perror("Open file fail");;
getch();
exit(1);
}
/*将文

件中要排序的信息存入结构体数组*/
while((fread(&q,sizeof(product),1,fp))!=(int)NULL)
{
p[recNumber].Number=q.Number;
strcpy(p[recNumber].Name,p[recNumber].Name);
p[recNumber].price=q.price;
p[recNumber].discount=q.discount;
p[recNumber].memberprice=q.memberprice;
recNumber++;
}
fclose(fp);
/*如果文件中有记录,则将各条记录按价格排序*/
if(recNumber>1)
{ /*用选择排序法进行按价格排序*/
for(i=0;i{
k=i;
for(j=i+1;j{
if(p[k].pricek=j;
}
q.Number=p[k].Number;
strcpy(https://www.360docs.net/doc/e37541049.html,,p[k].Name);
q.price=p[k].price;
q.discount=p[k].discount;
q.memberprice=p[k].memberprice;
p[k].Number=p[i].Number;
strcpy(p[k].Name,p[i].Name);
p[k].price=p[i].price;
p[k].discount=p[i].discount;
p[k].memberprice=p[i].memberprice;
p[i].Number=q.Number;
strcpy(p[i].Name,https://www.360docs.net/doc/e37541049.html,);
p[i].price=q.price;
p[i].discount=q.discount;
p[i].memberprice=q.memberprice;
}
/*将排序好的结构体记录写入文件*/
fp=fopen(s,"wb+");
if(fp==NULL)
{
printf("\nSet up file %s fail !End withh anykey.\n",s);
perror("Set up fail");
getch();
exit(1);
}
for(i=0;i{
if(fwrite(&p[i],sizeof(product),1,fp)!=1)
{
printf("\nWrite file %s fail!End with any key.\n",s);
perror("Write file fail!");
getch();
exit(1);
}
}
fclose(fp);
}
/*显示排序后的文件*/
printf("the Product's price in file %s is as flow:\n",s);
fp=fopen(s,"rb");
if(fp==NULL)
{
printf("\n Open file %s fail!End with any key.\n",s);
perror("Open fail file");
getch();
exit(1);
}
printf("\nNumber\t\tName\tprice\tdiscount\tmemberprice\n");
while(fread(&q,sizeof(product),1,fp)!=(int)NULL)
{
printf("\n%ld\t%s\t%4.1f\t%4.1f\t%4.1f\n",q.Number,https://www.360docs.net/doc/e37541049.html,,q.price,q.discount,q.memberprice);
}
fclose(fp);
}







相关文档
最新文档