超市管理系统源程序.doc
超市管理系统源程序#include <stdio.h>#include <string.h>#define MAX 20#define N 3#define PAGE 2#define PRINT1 printf("-n")#define PRINT2 printf("商品号-商品名称-商品种类-销售价格-生产日期(年-月-日)-保质期n")#define PRINT3 printf("%4d%s%s%8d%15%-%2d-%2d%10dn",goodsi.num,goodsi.name,goodsi.kind,goodsi.out_price,goodsi.pro_time.year,goodsi.pro_time.month,goodsi.pro_time.date,goodsi.save_day)int sum;/*记录商品数目*/struct date /*日期结构体类型*/ int year; int month; int date;struct goods_type /*商品结构体类型*/ int sum; char name10; char kind10; int amount; int goods_up; int goods_down; int in_price; int out_price; int in_num; struct date in_time; struct date pro_time; char factory10; int save_day; int profit;goodsMAX; /*存放MAX种商品记录的结构体数据*/save_message(jin sum) /*保存信息*/ FILE *fp; int i; if(fp=fopen("supermarket.txt","wb")=NULL) printf("读文件错误!n"); return; for(i=0;i<sum;i+) if(fwrite(&goodsi,sizeof(struct goods_type),1,fp)!=1) printf("写文件错误!n"); fclose(fp):input message() /*输入模块*/ int i=0,s=MAX; clrscr(); printf("nn 录入商品信息 (最多%d种)n",s); printf(" -n"); do printf("n 第%d种商品",i+1); printf("n 商品号:"); scanf("%d",&goodsi.num); if(goodsi.num=0) break; printf("n 商品名称:"); scanf("%s",goodsi.name); printf("n 商品类别:"); scanf("%s",goodsi.kind); printf("n 商品总量:"); scanf("%s",&goodsi.amout); printf("n 商品上限:"); scanf("%s",&goodsi.goods_up); printf("n 商品下限:"); scanf("%s",&goodsi.goods_down); printf("n 进货价格:"); scanf("%s",&goodsi.in_price); printf("n 销售价格:"); scanf("%s",&goodsi.out_price); printf("n 进货数量:"); scanf("%s",&goodsi.in_num); printf("n 进货日期(yyyy-mm-dd):"); scanf("%d-%d-%d",&goodsi.in_time.year,&goodsi.in_time.month,&goodsi.in_time.date); printf("n 商品名称:"); printf("n 生产日期(yyyy-mm-dd):"); scanf("%d-%d-%d",&goodsi.pro_time.year,&goodsi.pro_time.month,&goodsi.pro_time.date); printf("n 生产厂家:"); scanf("%s",goodsi.factory); printf("n 保质期:"); scanf("%s",goodsi.save_day); i+; while(i<MAX); printf("n -%d种商品信息输入完毕!-n",i); sum=i; printf("n 按任意键返回主菜单!"); bioskey(0);int read_message() /*读取信息*/ FILE *fp; int i=0; if(fp=fopen("supermarket.txt","rb")=NULL) printf("nn*暂时还没有任何库存信息,按任意键进入主菜单选择基本信息的录入!*n"); return; while(feof(fp)!=1) fread(&goodsi,sizeof(struct goods_type),1,fp); if(goodsi.num=0) break; else i+; fclose(fp); return(i);output_message() /*输出模块*/ int i=0,j=0,count,page=1; clrscr(); printf("nn -商品信息表- 第%d页nn",page); PRINT2; PRINT1; do if(goodsi.num!=0) j+; if(j%PAGE!=0) PRINT3; PRINT1; else PRINT3; PRINT1; if(goodsi+1.num!=0) printf("按任意键继续!"); bioskey(0); clrscr(); printf("nn -商品信息表- 第%d页nn",+page); PRINT2; PRINT1; i+; while(goodsi.num!=0); printf("按任意键返回主菜单!"); bioskey(0);insert_message() /*添加商品信息*/ int i=sum,j,flag=0; char choice; clrscr(); printf("nn 添加一种新商品n"); printf(" -n"); do goodsi.num=+sum; printf("n 新商品号:%d",goodsi.num); printf("n 商品名称:"); scanf("%s",goodsi.name); printf("n 商品类别:"); scanf("%s",goodsi.kind); printf("n 商品总量:"); scanf("%s",&goodsi.amout); printf("n 商品上限:"); scanf("%s",&goodsi.goods_up); printf("n 商品下限:"); scanf("%s",&goodsi.goods_down); printf("n 进货价格:"); scanf("%s",&goodsi.in_price); printf("n 销售价格:"); scanf("%s",&goodsi.out_price); printf("n 进货数量:"); scanf("%s",&goodsi.in_num); printf("n 进货日期(yyyy-mm-dd):"); scanf("%d-%d-%d",&goodsi.in_time.year,&goodsi.in_time.month,&goodsi.in_time.date); printf("n 商品名称:"); printf("n 生产日期(yyyy-mm-dd):"); scanf("%d-%d-%d",&goodsi.pro_time.year,&goodsi.pro_time.month,&goodsi.pro_time.date); printf("n 生产厂家:"); scanf("%s",goodsi.factory); printf("n 保质期:"); scanf("%s",goodsi.save_day); printf("n 要继续吗?(Y/N)"); choice=getch(); if(choice='y'|choice='Y') printf("n 继续!n"); i+; flag=1; else flag=0; while(flag=1); printf("n 按任意键返回主菜单!"); bioskey(0);del_message() /*删除商品信息*/;revise_message() /*修改模块*/ int i=0,choice,revise_num,flag; do clrscr(); printf("n 输入要修改的商品号:"); scanf("%d",&revise_num); for(i=0;i<sum;i+) if(goodsi.num=revise_num) printf("n -商品信息-n"); PRINT1; PRINT2; PRINT3; printf("n 您要修改那一项?n"); printf("n 1.名称 n"); printf("n 2.种类 n"); printf("n 3.销售价格 n"); printf("n 4.生产日期 n"); printf("n 5.保质期 n"); printf("n 请选择(1-5) n"); scanf("%d",&choice); switch(choice) case 1;printf("n 输入修改后的名称:"); scanf("%s",&goodsi.name);break; case 2;printf("n 输入修改后的种类:"); scanf("%s",&goodsi.kind);break; case 3;printf("n 输入修改后的销售价格:"); scanf("%s",&goodsi.out_price);break; case 4;printf("n 输入修改后的生产日期:"); scanf("%d-%d-%d",&goodsi.pro_time.year,&goodsi.pro_time.month,&goodsi.pro_time.date);break; case 5;printf("n 输入修改后的保质期:"); scanf("%s",&goodsi.save_day);break; PRINT1; PRINT2; PRINT3; break; if(i=sum) printf("n 该商品不存在!"); bioskey(0); printf("nn 继续修改吗?(Y/N)"); choice=getch(); if(choice='y'|choice='Y') flag=1; printf("n 继续!n"); else flag=0; while(flag=1); printf("n 按任意键返回主菜单!"); bioskey(0);renew_message() /*更新模块*/ int choice; do clrscr(); printf("nnn *更新商品信息*nn"); printf(" 1.添加商品信息nn"); printf(" 2.删除商品信息nn"); printf(" 3.修改商品信息nn"); printf(" 0.返回主菜单nn"); printf(" 请选择(0-3):nn"); scanf("%d",&choice); switch(choice) case 1 : insert_message();break; case 2 : del_message();break; case 3 : revise_message();break; case 0 : break; while(choice!=0);inquire_name() int i,choixe,j=0,inquire_in_price; char inquire_name10; clrscr(); printf("n 输入要查询的商品名称:"); scanf("%s",inquire_name); for(i=0;i<sum;i+) if(strcmp(goodsi.name,inquire_name)=0&&goodsi.num!=0) printf("n -商品信息-n"); PRINT1; PRINT2; PRINT3; else j+; if(j=sum) printf("n 该商品不存在!"); bioskey(0);inquire_kind() /*按类别查询*/;inquire_count_time();inquire_message() /*查询模块*/ int choice; do clrscr(); printf("nnn *查询商品信息*nn"); printf(" 1.按商品名称查询 nn"); printf(" 2.按商品类别查询 nn"); printf(" 3.按进货量和进货价格查询 nn"); printf(" 0.返回主菜单 nn"); printf(" 请选择(0-3) "); scanf("%d",&choice); switch(choice) case 1 : inquire_name(); ;break; case 2 : inquire_kind(); ;break; case 3 : inquire_count_time(); ;break; case 0 : break; while(choice!=0);count_profit() /*统计利润*/ int i,choice,j=0,count_num,profit; char inquire_name10; clrscr(); printf("n 输入要统计的商品号:"); scanf("%d",&count_num); for(i=0;i<sum;i+) if(goodsi.num=count_num) profit=goodsi.out_price-goodsi.in_price; printf("n 商品利润 "); printf("n -n "); printf("n 商品号 利润 "); printf("n%25d%13dn",goodsi.num,profit); else j+; if(j=sum) printf("n 该商品不存在!"); bioskey(0);count_in_amount() /*统计进货量*/;count_full() /*统计库存量*/;count_message() /*统计模块*/ int choice; do clrscr(); printf("nnn *统计商品信息*nn"); printf(" 1.统计某商品的利润nn"); printf(" 2.统计某商品的进货量nn"); printf(" 3.统计同种名称不同货号商品库存量nn"); printf(" 0.返回主菜单nn"); printf(" 请选择(0-3):"); scanf("%d",&choice); switch(choice) case 1 : count_profit() ;break; case 2 : count_in_amount() ;break; case 3 : count_full() ;break; case 0 : break; while(choice!=0);sort_amount() /*库存排行*/ struct goods_type *p,*qMAX; int i,j,k; clrscr(); printf(" 库存排行n"); printf("-n"); printf("n 排名 商品号 商品名称 商品类别 库存量n"); p=goods; for(i=0;i<sum;i+) qi=p+; for(i=0;i<sum;i+) k=i; for(j=i;j<sum;j+) if(qk->amount<qj->amount) k=j; if(k!=i) p=qi;qi=qk;qk=p; for(i=0;i<sum;i+) printf("n%18d %6d %8d %11s %10dn",i+1,qi->name,qi->kind,qi->amount); bioskey(0);sort_profit() /*利润排行*/;sort_out_amount() /*销售量排行*/;sort_message() /*排行模块*/ int choice; do clrscr(); printf("nnn *商品排行信息*nn"); printf(" 1.库存量排行nn"); printf(" 2.利润排行nn"); printf(" 3.销售量排行nn"); printf(" 0.返回主菜单nn"); printf(" 请选择(0-3)nn"); scanf("%d",&choice); switch(choice) case 1 : sort_amount(); ;break; case 2 : sort_profit(); ;break; case 3 : sort_out_amount(); ;break; case 0 : break; while(choice!=0);main() /*主程序*/ int choice; sum=read_message(); bioskey(0); do clrscr(); printf("nnn *超市管理系统*nn"); printf(" 1.录入商品信息 nn"); printf(" 2.打印商品信息 nn"); printf(" 3.更新商品信息 nn"); printf(" 4.查询商品信息 nn"); printf(" 5.统计商品信息 nn"); printf(" 6.商品排行信息 nn"); printf(" 请选择(0-6)"); scanf("%d",&choice); switch(choice) case 1: input_message(); break; case 2: output_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-以上商品信息已保存!-(文件名:supermarket.txt)n");