超市管理系统

#include
#include
#define MAX 3
#define PAGE 3
typedef struct
{
	int year;
	int month;
	int day;
}DATE;
typedef struct
{
	int num;//商品号 
	char name[10];//商品名称 
	char kind[10];//商品类别 
	int pro_time;//生产日期 
	int save_day;//保质期 
}GOODS;
int read_file(GOODS goods[]);//读信息模块 
void save_file(GOODS goods[],int sum);//存储信息模块 
int input(GOODS goods[]);//输入信息模块 
void output(GOODS goods[],int sum);//输出模块
int append(GOODS goods[],int sum);//添加信息模块 
void modify(GOODS goods[],int sum);//修改信息模块 
int del(GOODS goods[],int sum);//删除信息模块 
void inquire(GOODS goods[],int sum);//信息查询模块 
void count_();//信息统计模块 
void sort(GOODS goods[],int sum);//商品排行信息 
void main()
{
	GOODS goods[MAX];
	int choice,sum;
	sum=read_file(goods);
	if(sum==0)
	{
		printf("并录入基本库存信息!*****\n");
		getch();
		sum=input(goods);
	}
	do
	{
		printf("\n\n\n           *******超市管理系统*******\n\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("                     7.商品排行信息\n\n");
		printf("                     0.退出系统\n\n");
		printf("                     请选择(0-6)\n\n");
		scanf("%d",&choice);
		switch(choice)
		{
			case 1:sum=append(goods,sum);  break;
			case 2:modify(goods,sum);      break;
			case 3:sum=del(goods,sum);     break;
			case 4:output(goods,sum);      break;
			case 5:inquire(goods,sum);     break;
			case 6:count_();               break;
			case 7:sort(goods,sum);        break;
			case 0:                        break;
		}
	}while(choice!=0);
	save_file(goods,sum);
 } 
 int read_file(GOODS goods[])
 {
 	FILE *fp;
 	int i=0;
 	if((fp=fopen("supermarket.txt","rt"))==NULL)
 	{
 		printf("\n\n*****库存文件不存在,请创建");
 		return 0;
	 }
	 while(feof(fp)!=1)
	 {
	 	fread(&goods[i],sizeof(GOODS),1,fp);
	 	if(goods[i].num ==0)
	 	break;
	 	else 
	 	i++;
	 }
	 fclose(fp);
	 return i;
 }
 void save_file(GOODS goods[],int sum)
 {
 	FILE *fp;
 	int i;
 	if((fp=fopen("supermarket.txt","wb"))==NULL)
 	{
 		printf("读文件错误!\n");
 		return;
	 }
	 for(i=0;i<sum;i++)
	 {
	 	if(fwrite(&goods[i],sizeof(GOODS),1,fp)!=1)
	 	{
	 		printf("写文件错误!\n");
		 }
	 }
	 fclose(fp);
 }
 int input(GOODS goods[])
 {
 	int i=0;
 	printf("\n\n                             录入商品信息  (最多%d种)\n",MAX);
	 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           生产日期(yyyy-mm-dd):");
	 	scanf("%d",&goods[i].pro_time);
	 	printf("\n           保质期:");
		scanf("%d",&goods[i].save_day );
		i++;	
	 }while(i<MAX);
	 printf("\n              --%d种商品信息输入完毕--\n",i);
	 printf("\n         按任意键返回主菜单");
	 getch();
	 return i; 
 }
 void output(GOODS goods[],int sum)
 {
 	int i=0,j=0,page=1;
 	printf("\n\n\n\n    --商品信息表--       第%d页\n\n",page);
 	printf("商品号--商品名称--商品种类--生产日期(年-月-日)--保质期\n");
 	printf("--------------------------------------------------------\n");
 	do
 	{
 		if(goods[i].num !=0)
 		{
 			j++;
 			if(j%PAGE!=0)
 			{
 				printf("%4d %8s %8s %15d %10d\n",goods[i].num,goods[i].name,goods[i].kind,goods[i].pro_time,goods[i].save_day);
 				printf("----------------------------------------------------------\n");
			 }
			 else
			 {
			 	printf("按任意键继续");
			 	getch();
			 	printf("\n\n\n\n    --商品信息表--     第%d页\n\n",++page);
			 	printf("商品号--商品名称--商品种类--生产日期(年-月-日)--保质期\n");
 	            printf("--------------------------------------------------------\n");
 	            printf("%4d %8s %8s %15d %10d\n",goods[i].num,goods[i].name,goods[i].kind,goods[i].pro_time,goods[i].save_day);
 				printf("----------------------------------------------------------\n");
			 }
		 }
		 i++;
	 }while(goods[i].num!=0);
	 printf("按任意键返回主菜单");
	 getch();
 }
 int append(GOODS goods[],int sum)
 {
 	int i;
 	i=sum;
    printf("\n即将添加数据,按任意键继续\n");
    getch();
    if(sum>=30)
    {
    	printf("空间已满,添加失败\n");
		getch(); 
    	return;
	}
	else
	{
		printf("\n\n\n                请输入你要添加的商品信息(最多%d种):\n",MAX-i-1);
		printf("\n                   -----------------------------\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           生产日期(yyyy-mm-dd):");
	    	scanf("%d",&goods[i].pro_time);
	    	printf("\n           保质期:");
	    	scanf("%d",&goods[i].save_day );
		    i++;
		}while(i<MAX); 
	}
	printf("\n\n             --%d种商品信息添加完毕--\n",i-sum);
	printf("按任意键返回主菜单");
	getch();
	return i;
 }
 void modify(GOODS goods[],int sum)
 {
 	printf("\n\n%d\n\n",goods[1].num);
 	int i=0,choice,modify_num,flag;
 	do
 	{
 		printf("\n                   输入要修改的商品号:");
 		scanf("%d",&modify_num);
 		printf("\n\n%d \n",modify_num);
 		for(i=0;i<sum;i++)
 		if(goods[i].num==modify_num);
 		{
 			printf("\n             --商品信息--%d\n",goods[i].num);
 			printf("商品号--商品名称--商品种类--生产日期(年-月-日)--保质期\n");
 			printf("--------------------------------------------------------\n");
 			printf("%4d %8s %8s %15d %10d\n",goods[i].num,goods[i].name,goods[i].kind,goods[i].pro_time,goods[i].save_day);
 			printf("\n                             你要修改哪一项?\n");
 			printf("\n                                1.名称\n");
 			printf("\n                                2.种类\n");
 			printf("\n                                3.生产日期\n");
 			printf("\n                                4.保质期\n");
 			printf("\n                                请选择(1-4):\n");
 			scanf("%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].pro_time);  break;
 				case 4:printf("\n               请输入修改后的保质期:");
				scanf("%d",&goods[i].save_day);    break; 
			 }
			printf("商品号--商品名称--商品种类--生产日期(年-月-日)--保质期\n");
 			printf("--------------------------------------------------------\n");
 			printf("%4d %8s %8s %15d %10d\n",goods[i].num,goods[i].name,goods[i].kind,goods[i].pro_time,goods[i].save_day);
 			break;
		 }
		 if(i==sum)
		 {
		 	printf("\n              这个商品不存在\n");
		 	getch();
		 }
		 printf("\n\n    还要继续修改吗?(y/n)");
		 choice=getch();
		 if(choice=='Y'||choice=='y')
		 {
		 	flag=1;
		 	printf("\n           继续!\n");
		 }
		 else   flag=0;
	 }while(flag==1);
	 printf("\n        按任意键返回主菜单\n");
	 getch();
 }
 int del(GOODS goods[],int sum)
 {
 	int i,j,del_num,choice,flag;
 	int count=0;
 	do
 	{
 		printf("\n ---------你要删哪个?\n");
 		scanf("%d",&del_num);
 		for(i=0;i<sum;i++)
 		if(goods[i].num ==del_num)
 		{
 			printf("\n             --商品信息--\n");
 			printf("商品号--商品名称--商品种类--生产日期(年-月-日)--保质期\n");
 			printf("--------------------------------------------------------\n");
 			printf("%4d %8s %8s %15d %10d\n",goods[i].num,goods[i].name,goods[i].kind,goods[i].pro_time,goods[i].save_day);
 			printf("\n             确定要删它吗?那么可爱。后悔的话输入(n)");
 			if(getch()=='n')     break; 
 			for(j=i;j<sum;j++)
 			{
 				goods[j]=goods[j+1];
 				goods[sum-1].num=0;
			 }
			 count++;
			 break;
		 }
		 if(i==sum)
		 {
		 	printf("\n    你想删除的商品不存在,按任意键继续\n");
		 	getch();
		 }
		 printf("\n\n         还要继续删除吗?(Y/N)");
		 choice=getch();
		 if(choice=='Y'||choice=='y')
		 {
		 	flag=1;
		 	printf("\n           继续!\n");
		 }
		 else   flag=0; 
	 }while(flag==1);
	 printf("\n        按任意键返回主菜单");
	 getch();
	 return (sum-count);
 }
 void inquire(GOODS goods[],int sum)
 {
 	int look,i,flag,choice;
 	do
 	{
 		printf("\n      请输入你要查询的货号:");
 		scanf("%d",&look);
 		for(i=0;i<sum;i++)
 		if(goods[i].num ==look)
 		{
 			printf("\n          --商品信息--\n");
 			printf("商品号--商品名称--商品种类--生产日期(年-月-日)--保质期\n");
 			printf("--------------------------------------------------------\n");
 			printf("%4d %8s %8s %15d %10d\n",goods[i].num,goods[i].name,goods[i].kind,goods[i].pro_time,goods[i].save_day);
 			break;
		 }
		 if(i==sum)
		 {
		 	printf("\n   你要查询的商品不存在,按任意键继续\n");
		 	getch();
		 }
		 printf("\n\n         还要继续查询吗?(Y/N)");
		 choice=getch();
		 if(choice=='Y'||choice=='y')
		 {
		 	flag=1;
		 	printf("\n           继续!\n");
		 }
		 else   flag=0;
	 }while(flag==1);
	 printf("\n        按任意键返回主菜单");
	 getch();
	 return;
 }
 void count_()
 {
 	return;
 }
 void sort(GOODS goods[],int sum)
 {
 	GOODS t;
 	int i,j,k;
 	printf("\n\n\n\n\n            库存排行\n");
 	printf("------------------------------------------------");
 	printf("\n    排名   商品号  商品名称  商品类别  库存量\n");
 	for(i=0;i<sum;i++)
 	{
 		k=i;
 		for(j=i+1;j<sum;j++)
 		{
 			if(goods[k].save_day>goods[j].save_day)
 			k=j;
		 }
		 if(k!=i)
		 {
		 	t=goods[i];
		 	goods[i]=goods[k];
		 	goods[k]=t;
		 }
	 }
	 output(goods,sum);
	 getch();
 }

你可能感兴趣的:(超市管理系统)