超市管理系统完整+源代码.docx
《超市管理系统完整+源代码.docx》由会员分享,可在线阅读,更多相关《超市管理系统完整+源代码.docx(20页珍藏版)》请在冰豆网上搜索。
超市管理系统完整+源代码
有一个小型超市,出售N(N>=10)种商品,设计并实现一个系统,完成下列
功能:
1.保存及输出。
超市中的各种商品信息保存在指定文件中,可以把它们输出显示。
2.计算并排序。
计算每类商品的总价值(sum,单精度)及平均价(aver,单精度,输出一位小数),将每类商品按平均价从大到小的顺序排序打印出来。
3.统计。
统计输出库存量低于100的货号及类别。
统计输出有两种以上(含两种)商品库存量低于100的商品类别。
1.2总体结构
本程序主要分为八个模块:
主模块、信息输出修改模块、新建信息模块、排序模块、计算模块、统计模块1、统计模块2、打印模块。
1)主模块:
通过调用各分模块实现功能;
2)信息输出修改模块:
输出显示文件中商品信息内容,添加商品信息,删除商品信息,修改商品信息;
3)新建商品信息模块:
建立一个新结构体,为链表存信息用,并且将信息保存在指定的文件中;
4)排序模块:
把文件中顺序零乱的商品信息按单价的大小从高到低进行排序,放到链表里存储;
5)计算模块:
将所有商品的价格与库存量进行累加求和;
6)打印模块:
将商品信息按每类平均价格排序(从高到低)按顺序打印出来;
7)统计模块1:
统计库存量低于100的货名及类别;
8)统计模块2:
统计商品库存量有2种以上(含2种)低于100的商品类别附录(程序清单)
#include"stdio.h"
/*输入,输出头文件*/
#include"stdlib.h"
/*申请空间头文件*/
#include"string.h"
/*对字符串加工头文件*/
#include"conio.h"
/*清屏头文件*/
FILE*fp;
intn=0;/*定义文件指针类型*/
inti,j,a[4],m;
/*定义整数类型*/
floataver[4],sum[4],g[4],h;/*定义浮点类型*/
charc[5]="elec";
/*定义字符数组类型*/
chard[5]="comm";
/*定义字符数组类型*/
chare[5]="food";
/*定义字符数组类型*/
charf[5]="offi";
/*定义字符数组类型*/
structgood
/*定义结构体*/
{
intnum;
/*商品编号*/
charkind[40];
/*商品类型*/
floatprice;
/*商品价格*/
charunit[10];
/*商品单位*/
intquantity;
/*商品数量*/
structgood*next;
/*定义结构体指针类型*/
}*head,*p1,*p2;
structgood*createlist()/*创建链表函数*/
{
structgood*head1,*p1,*p2;
/*定义结构体指针类型*/
if((fp=fopen("goodsmessage.txt","w"))==NULL)
{
printf("cannotopenthefile");
exit(0);/*结束程序*/
/*判断能否打开文件*/
head1=(structgood*)malloc(sizeof(structgood));
/*申请头结点空间*/
p1=head1;
p2=head1;
printf(
H*********************************************
\n");
printf("请输入信息:
编号,名称,类型,价格,单位,数目\n");
printf(
H*********************************************
\n");
printf("\n");
scanf("%d%s%s%f%s%d",&p1->num,p1->name,p1->kind,&p1->price,p1->unit,&p1->quantity);/*输入商品信息*/
printf("\n");
p1->next=NULL;
fprintf(fp,"%d%s%s%f%s%d",p1->num,p1->name,p1->kind,p1->price,p1->unit,p1->quantity);/*将商品信息写入文件*/
while
(1)
/*申请新空间*/
{
p1=(structgood*)malloc(sizeof(structgood));
printf("请输入信息:
编号,名称,类型,价格,单位,数目\n");
printf("\n");
scanf("%d",&p1->num);
{
printf("\n\n");
fprintf(fp,"%d",-1);
fclose(fp);
returnhead1;
/*返回头指针*/scanf("%s%s%f%s%d",p1->name,p1->kind,&p1->price,p1->unit,&p1->quantity);/*输入商品信息*/
printf("\n");
fprintf(fp,"%d%s%s%f%s%d",p1->num,p1->name,p1->kind,p1->price,p1->unit,p1->quantity);
/*将商品信息写入文件*/
p1->next=NULL;
p2->next=p1;
p2=p1;
}
}
structgood*paixu(structgood*head2)/*链表排序函数*/
{
structgood*p6,*p7,*r,*s;/*定义结构体指针类型*/
for(i=0;i<=3;i++)/*赋初值值*/
{
a[i]=0;
sum[i]=0;
aver[i]=0;
}
p6=(structgood*)malloc(sizeof(structgood));/*申请新空间*/
p6->next=head2;
head2=p6;
while(p6->next!
=NULL)/*判断循环结束条件*/
p7=p6->next;
r=p6;
while(p7->next!
=NULL)/*判断循环结束条件*/
{
if((p7->next->price)>(r->next->price))/*判断是否调换*/
r=p7;
p7=p7->next;
}
if(p6!
=r)/*判断循环结束条件*/
{
s=r->next;/*指针调换*/
r->next=s->next;
s->next=p6->next;
p6->next=s;
}
p6=p6->next;
}
p6=head2;
head2=head2->next;
free(p6);/*释放第一个无效空间*/
returnhead2;
}
voidjisuan()
{
p1=head;
do
{
sum[0]=sum[0]+(p1->price)*(p1->quantity);/*求电器总价*/
a[0]=a[0]+p1->quantity;/*求电器总件数*/
}
if(strcmp(p1->kind,d)==0)/*判断是否为日用品类型*/
{
sum[1]=sum[1]+(p1->price)*(p1->quantity);/*求日用品总价*/
a[1]=a[1]+p1->quantity;/*求日用品总件数*/
}
if(strcmp(p1->kind,e)==0)/*判断是否为办公用品类型*/
{
sum[2]=sum[2]+(p1->price)*(p1->quantity);/*求办公用品总价*/
a[2]=a[2]+p1->quantity;/*求办公用品总件数*/
}
if(strcmp(p1->kind,f)==0)/*判断是否为食品类型*/
{
sum[3]=sum[3]+(p1->price)*(p1->quantity);/*求食品总价*/
a[3]=a[3]+p1->quantity;/*求食品总件数*/
}
p1=p1->next;
/*遍历链表结束条件*/
/*求每类商品平均价*/
}while(p1!
=NULL);
for(i=0;i<4;i++)
aver[i]=sum[i]/a[i];
printf("商品类型\t平均价\t总库存量\n");
printf("\n");
printf("电器总价值:
%0.1f\t平均价:
%0.1f\t总库存量:
%d\n",sum[O],aver[O],a[O]);
printf("\n");
printf("日用品总价值:
%0.1f\t平均价:
%0.1f\t总库存量:
%d\n",sum[1],aver[1],a[1]);
printf("\n");
printf("食品总价值:
%0.1f\t平均价:
%0.1f\t总库存量:
%d\n",sum[2],aver[2],a[2]);
printf("\n");
printf("办公用品总价值:
%0.1f\t平均价:
%0.1f\t总库存
量:
%d\n",sum[3],aver[3],a[3]);
printf("\n");
}
voidshuchu()/*输出商品信息函数*/
do
intn=0,p=0,q=0,r=0;
printf("所有商品信息:
\n");
printf("编号,名称,类型,价格,单位,数目\n");
if((fp=fopen("goodsmessage.txt","rb+"))==NULL)/*判断能否打开文件*/
{
printf("cannotopenthefile");
exit(0);/*结束程序*/
}
head=(structgood*)malloc(sizeof(structgood));/*申请头结点空间*/
p3=head;
fscanf(fp,"%d%s%s%f%s%d",&p3->num,p3->name,p3->kind,&p3->price,p3->unit,&p3->quantity);
/*从文件中写到链表*/
while
(1)
{
p4=(structgood*)malloc(sizeof(structgood));/*申请头结点空间*/
fscanf(fp,"%d",&p4->num);
if(p4->num!
=-1)/*判断循环结束条件*/
fscanf(fp,"%s%s%f%s%d",p4->name,p4->kind,&p4->price,p4->unit,&p4->quantity);/*从文件中写到链表*/
p4->next=NULL;p3->next=p4;p3=p4;
}
else
{
p3->next=NULL;
break;
}
}
fclose(fp);/*关闭文件*/
p3=head;while(p3!
=NULL)
{
printf("%d%s%s%0.1f%s%d\n\n",p3->num,p3->name,p3->kind,p3->price,p3->unit,p3->quantity);
printf("\n");
p3=p3->next;
}
printf("//////////////////////////////////\n");
while(n!
=4)
p3=head;
printf("1添加商品信息\n");
printf("2删除某商品信息\n");
printf("3修改某商品信息\n");
\n");
printf("**********************************\n");
scanf("%d",&n);
单位数目\n");
/*申请空间*/
printf("请输入商品编号名称类型价格
printf("**********************************\n");
p4=(structgood*)malloc(sizeof(structgood));
scanf("%d%s%s%f%s%d",&p4->num,p4->name,p4->kind,&p4->price,p4->unit,&p4->quantity);/*输入商品信息*/
p4->next=NULL;
while(p3->next!
=NULL)/*判断循环结束条件*/
{
p3=p3->next;
}
p3->next=p4;
p3=head;
if((fp=fopen("goodsmessage.txt","w"))==NULL)/*判断能
否打开文件*/
printf("cannotopenthefile");
exit(0);/*结束程序*/
}
while(p3!
=NULL)
{fprintf(fp,"%d%s%s%f%s%d
",p3->num,p3->name,p3->kind,p3->price,p3->unit,p3->quantity)/*将商品信息写入文件*/
p3=p3->next;
}fprintf(fp,"%d",-1);
fclose(fp);/*关闭文件*/
printf(
H**********************************
\n");
printf("\n");
printf("请按4返回\n");
printf("\n");
if(n==2)/*删除商品*/
printf("请输入需要删除的商品编号\n");
scanf("%d",&p);
printf("1确认删除\n2取消删除\n");
scanf("%d",&r);
if(r==1)
if((head->num)==p)
{
head=head->next;
free(p3);/*释放空间*/
}
else
{
p4=head;p3=p4->next;
/*判断循环结束条件*/
while(p3!
=NULL)
{
if((p3->num)==p)
{p5=p3->next;
free(p3);/*释放空间*/p4->next=p5;
break;
}
p3=p3->next;
p4=p4->next;
}
开文件*/
{
printf("cannotopenthefile");
exit(0);/*结束程序*/
}p3=head;
while(p3!
=NULL)/*判断循环结束条件*/
{
fprintf(fp,"%d%s%s%f%s%d
",p3->num,p3->name,p3->kind,p3->price,p3->unit,p3->quantity);/*将商品信息写入文件*/
p3=p3->next;
}
fprintf(fp,"%d",-1);
fclose(fp);/*关闭文件*/
}
if(r==2)
continue;/*继续循环*/
printf(
H**********************************
\n");
printf("\n");
printf("请按4返回\n");
printf("\n");
{
printf("请输入需要修改的商品编号\n");
scanf("%d",&q);
while(p3!
=NULL)/*判断循环结束条件*/
{
if((p3->num)==q)/*判断是否为所需要修改的商品*/
{
printf("请输入商品单价与库存量(如果单价不变请输入原来的单价)\n");
scanf("%f%d",&p3->price,&p3->quantity);/*输入商品价
格与库存量*/
}
p3=p3->next;
}
if((fp=fopen("goodsmessage.txt","w"))==NULL)/*判断能
否打开文件*/
{
printf("cannotopenthefile");
exit(0);/*结束程序*/
}
p3=head;
while(p3!
=NULL)/*判断循环结束条件*/
fprintf(fp,"%d%s%s%f%s%d",p3->num,p3->name,p3->kind,p3->price,p3->unit,p3->quantity);/*将商品信息写入文件*/
p3=p3->next;
}fprintf(fp,"%d",-1);
fclose(fp);/*关闭文件*/
printf("\n");
printf("请按4返回\n");
printf("\n");
break;
scanf("%d",&p);
if(p==1)
continue;/*继续循环*/
}while(n!
=2);
voidprintf0(structgood*p)/*遍历链表并打印电器类商品函数*/
{
structgood*p3;/*定义结构体指针类型*/
p3=p;
while(p3!
=NULL)/*判断遍历链表循环结束条件*/
{
if(strcmp(p3->kind,c)==0)/*判断商品类型是否为电器类型*/
{
printf("%d\t%s\t%s\t%0.1f\t%s\t%d\n",p3->num,p3->name,p3->kind,p3->price,p3->unit,p3->quantity);/*输出电器类商品信息*/
printf("\n");
}p3=p3->next;
}
return;
}
voidprintf1(structgood*p)/*遍历链表并打印日用品类商品函数*/
{
structgood*p3;/*定义结构体指针类型*/
p3=p;
printf("%d\t%s\t%s\t%0.1f\t%s\t%d\n",p3->num,p3->name,p3->kind,p3->price,
p3->unit,p3->quantity);/*输出日用品类商品信息*/
printf("\n");
}p3=p3->next;
}
return;
}
voidprintf2(structgood*p)/*遍历链表并打印办公用品类商品函数*/
{
structgood*p3;/*定义结构体指针类型*/
p3=p;
while(p3!
=NULL)/*判断遍历链表循环结束条件*/
{
if(strcmp(p3->kind,e)==0)/*判断商品类型是否为办公用品类型*/
{
printf("%d\t%s\t%s\t%0.1f\t%s\t%d\n",p3->num,p3->name,p3->kind,p3->price,
p3->unit,p3->quantity);/*输出办公用品类商品信息*/
printf("\n");
}p3=p3->next;
}return;
}
voidprintf3(structgood*p)/*遍历链表并打印食品类商品函数*/
{
structgood*p3;/*定义结构体指针类型*/
p3=p;
while(p3!
=NULL)/*判断遍历链表循环结束条件*/
{
if(strcmp(p3->kind,f)==0)/*判断商品类型是否为食品类型*/
{
printf("%d\t%s\t%s\t%0.1f\t%s\t%d\n",p3->num,p3->name,p3->kind,p3->price,p3->unit,p3->quantity);/*输出食品类商品信息*/
printf("\n");
}
p3=p3->next;
}
return;
}
voidshunxudayin()
{
for(i=0;i<4;i++)
g[i]=aver[i];/*将平均价赋给新数组*/
for(i=j+1;i<4;i++)
{
if(g[j]{
h=g[j];
g[j]=g[i];
g[i]=h;
}
printf("商品平均价格排序表(从高到低)\n");
printf("\n");
printf("编号\t名称\t类别\t单价\t单位\t数量\n");
printf("\n");
for(j=0;j<4;j++)
for(i=0;i<4;i++)
{
if(aver[i]==g[j])/*判断每类商品平均价格的先后顺序*/
switch(i)
{
case0:
printf0(head);/*调用遍历链表并打印电器类商品函数*/
break;
case1:
printf1(head);/*调用遍历链表并打印日用品类商品函数*/
break;
case2:
printf2(head);/*调用遍历链表并打印办公用品类商品函数*/break;
case3:
printf3(head);/*调用遍历链表并打印食品类商品函数*/break;
}
}
}
voidtongji1()
{
p1=head;
printf("库存量低于100的货名及类别\n");
printf("\n");
printf("商品名称\t商品类型\n");
printf("\n");
while(p1!
=NULL)/*判断遍历链表循环结束条件*/
{
if(p1->quantity<100)/*判断库存量是否小于100*/
{
printf("%s\t%s\n",p1->name,p1->kind);/*输出商品名称及类别*/
printf("\n");
}
p1=p1->next;
}
}
voidtongji2()
printf("\n");
printf("食品\n");
printf("\n");
/*判断办公用品类
}if((a[3]<100)&&(a[3]>=2))
库存量是否为2种以上(含2种)低于100*/
{
printf("办公用