超市商品管理系统C语言源代码
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
#include
#include
#include
#include
#include
#define MAX 20
#define PRINT1 printf("---------------------------------------------------------\n")
#define PRINT2 printf("商品号--商品名称--商品种类--销售价格--生产日期(年-月-日)--保质期\n")
#define PRINT3 printf("%4d%8s%8s%8d%8d-%2d-%2d%4d\n",goods[i].num,goods[i].name,goods[i].kind,goods[i].out_price,goods[i].pro_time.year,goods[i].pro_time.month,goods[i].pro_time.day,goods[i].save_day)
int sum;
struct date
{
int year;
int month;
int day;
};
struct goods_type /*定义结构体*/
{
int num;
char name[10];
char kind[10];
int amount;
int in_price;
int out_price;
struct date pro_time;
int save_day;
int profit;
}goods[MAX];
Save_Message(int sum); /*函数声明*/
void Inquire_message(void);
void Inquire_goodskind(void);
void Inquire_goodsname(void);
int Read_Message(void);
void Renew_Messahe(void);
void Revise_Message(void);
void Delete_Message(void);
void Insert_Message();
void Show_Message(void);
int Input_Message(void);
void Count_Message();
long Count_Profit();
long Count_Amount();
void Sort_amount(void);
void Sort_Message(void);
void Sort_Message(void)
{
int choice;
do
{
clrscr();
printf("\n\n\n *******商品信息排行********\n\n");
printf(" 1:按库存量排行\n\n");
printf(" 0:返回主菜单\n\n");
printf(" 请选择(0-1): ");
scanf("%d",&choice);
switch(choice)
{
case 1:Sort_amount();break;
case 0:break;
}
}while(choice!=0);
}
void Sort_amount(void) /*库存量排序*/
{
struct goods_type *p,*q[MAX];
int i,j,k;
clrscr();
printf(" 库存排行");
PRINT1;
printf("\n\n排名 商品号 商品名称 商品类别 库存量\n\n");
p=goods;
for(i=0;i
for(i=0;i
k=i;
for(j=i+1;jamount)
k=j;
if(k!=i)
{
p=q[i];
q[i]=q[k];
q[k]=p;
}
}
for(i=0;i
printf(" %d ",i+1);
printf(" %d ",q[i]->num);
printf(" %s ",q[i]->name);
printf(" %s ",q[i]->kind);
printf(" %d \n ",q[i]->amount);
}
bioskey(0);
}
long Count_Amount() /*统计库存量*/
{
int i,j=0,choice,count_num,Amount;
char inquire_name[10];
clrscr();
printf("\n 请输入想要统计的商品号: ");
scanf("%d",&count_num);
for(i=0;i
{
Amount=goods[i].amount;
printf("-------------------------");
printf(" 商品号
库存量");
printf("\n%8d%8d",goods[i].num,Amount);
}
else
j++;
if(j==sum)
printf("该商品信息不存在!!!");
bioskey(0);
}
long Count_Profit() /*统计利润模块*/
{
int i,choice,j=0,count_num,profit;
clrscr();
printf("\n 请输入想要统计的商品号: ");
scanf("%d",&count_num);
for(i=0;i
{
profit=goods[i].out_price-goods[i].in_price;
printf("\n 商品利润");
printf("\n -------------------------------");
printf("\n 商品号 利润");
printf("\n %8d%8d\n",&goods[i].num,&profit);
}
else
j++;
if(j==sum)
printf("\n 该商品不存在!!!");
printf("\n 按任意键返回主菜单!!");
bioskey(0);
}
void Count_Message(void) /*统计模块*/
{
int choice;
do{
clrscr();
printf("**************统计商品信息*************\n\n\n");
printf("******* 1:统计库存量 ************\n\n\n");
printf("******* 2:统计利润 ************\n\n\n");
printf("******* 0:返回主菜单 ************\n\n\n");
printf("******* 请选择(0-2): ************\n\n\n");
scanf("%d",&choice);
switch(choice)
{
case 1:Count_Amount();break;
case 2:Count_Profit();break;
case 0:break;
}
}while(choice!=0);
}
int Input_Message(void) /*输入模块*/
{
int i=0,s=MAX;
char bd;
clrscr();
printf("开始输入商品信息.\n");
printf("\n\n 商品信息:(最多 %d) \n",s);
printf(" ===============================\n");
do
{
printf("\n 第 %d 商品",i+1);
printf("\n 商品号: ");
scanf("%d",&goods[i].num);
if(goods[i].num==0) break;
printf("\n 商品名称: ");
scanf ("%s",goods[i].name);
printf("\n 商品种类: ");
scanf ("%s",goods[i].kind);
printf("\n 商品库存量: ");
scanf ("%d",&goods[i].amount);
printf("\n 进货价格: ");
scanf ("%d",&goods[i].in_price);
printf("\n 销售价格: ");
scanf ("%d",&goods[i].out_price);
printf("\n 生产日期(yyyy-mm-dd): ");
scanf ("%d-%d-%d",&goods[i].pro_time.year,&goods[i].pro_time.month,&goods[i].pro_time.day);
printf("\n 保质期: ");
scanf ("%d",&goods[i].save_day);
printf("继续输入信息,y or n ??");
fflush(stdin);
scanf("%c",&bd);
if(bd=='y')
i++;
else
break;
}while(i
getch();
printf(" 按任意键返回主菜单.");
sum=i;
printf("输入回车返回主菜单!!!");
bioskey(0);
}
void Show_Message(void) /*显示输出模块*/
{
int i=0,j=0,count,page=7;
clrscr();
printf("\n\n --商品信息-- 在第 %d 页\n\n",page);
PRINT2;
PRINT1;
do
{
if(goods[i].num!=0)
{
j++;
if(j%page!=0)
{
PRINT3;
PRINT1;
}
else
{
PRINT3;
PRINT1;
if(goods[i+1].num!=0)
{
printf("按任意键继续!");
bioskey(0);
clrscr();
printf("\n\n --------商品信息--------在第 %d 页\n\n",++page);
PRINT2;
PRINT1;
}
}
}
i++;
}while(goods[i].num!=0);
printf("按任意键返回主菜单!!!!!");
bioskey(0);
}
void Insert_Message(void)/*添加商品信息模块*/
{
int i=sum,j,flag=0;
char choice;
clrscr();
printf("\n\n 添加一种新商品\n\n ");
printf("\n-------------------------------------------------\n");
do
{
goods[i].num=++sum;
printf("\n 新商品号:%d",goods[i].num);
printf("\n 新商品名: ");
scanf("%s",goods[i].name);
printf("\n 新商品种类: ");
scanf("%s",goods[i].kind);
printf("\n 新商品数量: ");
scanf("%d",&goods[i].amount);
printf("\n 进价: ");
scanf("%d",&goods[i].in_price);
printf("\n 售价: ");
scanf("%d",&goods[i].out_price);
printf("\n 生产日期(yyyy-mm-dd): ");
scanf("%d-%d-%d",&goods[i].pro_time.year,&goods[i].pro_time.month,&goods[i].pro_time.day);
printf("\n 保质期: ");
scanf("%d",&goods[i].save_day);
i++;
printf("\n\n 想继续添加商品吗?(Y/N)");
fflush(stdin);
choice=getch();
if(choice=='y'||choice=='Y')
{
printf("继续");
flag=1;
}
else
flag=0;
}while(flag==1);
printf("\n\n\n 按任意键返回主菜单.........");
bioskey(0);
}
void Delete_Message(void)/*删除商品信息模块*/
{
int i,j,numberkey,flag=0;
char choice;
printf("开始删除......\n\n");
do
{
clrscr();
printf("请输入你要删除的商品号: \n");
scanf("%d",&numberkey);
for(i=0;i
if(goods[i].num==numberkey)
{
for(j=i;j
sum--;
flag=1;
printf("删除成功!!\n\n\n");
break;
}
}
if(flag==0)
{
printf("不存在该商品信息!!!");
bioskey(0);
}
printf("\n\n是否继续输入(y/n)?\n");
getchar();
scanf("%c",&ch
oice);
}while(choice=='y'||choice=='Y');
printf("\n按任意键返回");
bioskey(0);
}
void Revise_Message(void)/*修改商品信息模块*/
{
int i,j,flag,choice,revise_num;
char ch;
do
{
clrscr();
printf("\n\n 开始修改...");
printf("\n\n 请输入你想输入的商品号:\n");
scanf("%d",&revise_num);
for(i=0;i
if(goods[i].num==revise_num)
{
printf("确定修改此商品信息吗?(y/n)");
PRINT1;
PRINT2;
PRINT3;
getchar();
ch=getchar();
if(ch='y')
{
printf("\n\n\n修改商品信息的哪一项?");
printf("\n\n 1:商品名称");
printf("\n\n 2:商品类型");
printf("\n\n 3:销售价格");
printf("\n\n 4:生产日期");
printf("\n\n 5:保质期");
printf("\n\n 请选择(0-5)\n");
scanf("\n %d",&choice);
switch(choice)
{
case 1:printf("请输入修改后的商品名\n");
scanf("%s",&goods[i].name);break;
case 2:printf("请输入修改后的商品类型\n");
scanf("%s",&goods[i].kind);break;
case 3:printf("请输入修改后的商品销售价格\n");
scanf("%d",&goods[i].out_price);break;
case 4:printf("请输入修改后的商品生产日期\n");
scanf("%d-%d-%d",&goods[i].pro_time.year,&goods[i].pro_time.month,&goods[i].pro_time.day);break;
case 5:printf("请输入修改后的商品保质期\n");
scanf("%d",&goods[i].save_day);break;
case 0:break;
}
printf("修改商品信息:\n");
PRINT2;
PRINT3;
}
else
break;
}
else
j++;
if(j==sum)
printf("不存在该商品信息..");
}
printf("\n\n 继续修改商品信息吗(y/n)?");
getchar();
ch=getchar();
if(ch=='y')
{
printf("继续.\n");
flag=1;
}
else
flag=0;
}while(flag==1);
printf("修改完成!!\n\n");
getch();
}
void Renew_Message(void) /*更新商品信息模块*/
{
int choice;
do
{
clrscr();
printf("\n\n*********更新商品信息*********");
printf("\n\n 1:添加商品信息.\n\n");
printf("\n\n 2:删除商品信息.\n\n");
printf("\n\n 3
:修改商品信息.\n\n");
printf("\n\n 0:返回上一级.\n\n");
printf("\n\n 请选择(0-3): ");
scanf("%d",&choice);
switch(choice)
{
case 1:Insert_Message();break;
case 2:Delete_Message();break;
case 3:Revise_Message();break;
case 0:break;
}
}while(choice!=0);
}
int Read_Message(void) /*读取信息模块*/
{
FILE *fp;
int i=0;
if((fp=fopen("999.txt","rb"))==NULL)
{
printf("\n\t 现在还没有保存全部信息,按回车键回到主菜单输入商品信息!!");
printf("\n\t 按任意键返回!!!!\n");
getch();
return;
}
while(feof(fp)!=1)
{
fread(&goods[i],sizeof(struct goods_type),1,fp);
if(goods[i].num==0)
break;
else
i++;
}
fclose(fp);
return(i);
}
void Inquire_goodsname(void)/*按商品名称查询*/
{
int i,j,choice;
char namekey[10];
j=0;
printf("开始查询...");
printf("请输入你要查询的商品号:\n");
scanf("%s",namekey);
for(i=0;i
{
printf(" ---商品信息----");
PRINT1;
PRINT2;
PRINT3;
}
else
j++;
if(j==sum)
printf("该商品不存在!");
printf("按任意键返回主菜单.");
getch();
}
void Inquire_goodskind(void)/*按商品类型查询*/
{
int i,j,choice;
char kindkey[10];
j=0;
printf("开始查询...");
printf("请输入你要查询的商品种类:\n");
scanf("%s",kindkey);
for(i=0;i
{
printf(" ---商品信息----");
PRINT1;
PRINT2;
PRINT3;
}
else
j++;
if(j==sum)
printf("该商品不存在!");
printf("按任意键返回主菜单.");
getch();
}
void Inquire_Message(void) /*查询模块*/
{
int choice;
do
{
printf("\n *****查询商品信息 ***** \n");
printf("\n *****1:按商品名称.***** \n");
printf("\n *****2:按商品种类.***** \n");
printf("\n *****0:返回上一级.***** \n");
printf("\n 请选择(0-2): ***** \n");
scanf("%d",&choice);
switch(choice)
{
case 1:Inquire_goodsname();break;
case 2:Inquire_goodskind();break;
case 0:break;
}
}while(choice!=0);
}
Save_Message(int sum) /*保存模块*/
{
FILE *fp;
int i;
if((fp=fopen("999.txt","wb"))==NULL)
{
printf("\n\n不能打开文件!\n\n");
exit();
}
for(i=0;i<=sum;i++)
if(fwrite(&goods[i],sizeof(struct goods_type),1
,fp)!=1)
printf("写文件出错!");
fclose(fp);
}
void main(void) /*主函数*/
{
int choice;
clrscr();
sum=Read_Message();
bioskey(0);
do
{
clrscr();
printf(" ***************************************************\n");
printf(" ************ 超市商品管理系统 **************\n");
printf(" ****** ******\n");
printf(" ****** ******\n");
printf(" ****** ******\n");
printf(" ****** 欢迎登陆 ******\n");
printf(" ****** ******\n");
printf(" ****** ******\n");
printf(" ****** ******\n");
printf(" ***************************************************\n");
printf(" ***************************************************\n");
getchar ();
clrscr();
printf(" ***************超市商品管理系统***********\n\n");
printf(" ***** 1:输入商品信息 *****\n\n");
printf(" ***** 2:输出商品信息 *****\n\n");
printf(" ***** 3:更新商品信息 *****\n\n");
printf(" ***** 4:查询商品信息 *****\n\n");
printf(" ***** 5:统计商品信息 *****\n\n");
printf(" ***** 6:商品信息排序 *****\n\n");
printf(" ***** 0:退出系统 *****\n\n");
printf(" ***** 请选择(0-6): *****\n");
scanf("%d",&choice);
switch(choice)
{
case 1:Input_Message();break;
case 2:Show_Message();break;
case 3:Renew_Message();break;
case 4:Inquire_Message();break;
case 5:Count_Message();break;
case 6:Sort_Message();break;
case 0:break;
}
}while(choice!=0);
Save_Message(sum);
printf("\n-信息已经保存!-(file name:999.txt)\n");