#include<stdio.h>
#include<string.h>
#include<stdlib.h>

//-------登录设置

void login()
{
int number=0;
char username[20],password[20];//定义用户名和密码
 while(number<3)
  {
  system("cls");//清屏
  printf("\n\n");
  printf("\t\t*******************************\t\t\n");
  printf("\t\t             用户登录               \t\t\n");
  printf("\t\t*******************************\t\t\n");
  printf("\n\n");
  printf("\t\t\t用户名称:");
  scanf("%s",username);
  printf("\n\n");
  printf("\t\t\t用户密码:");
  scanf("%s",password);
   if(strcmp(username,"qt")==0&&strcmp(password,"qt")==0)
   {
     beforeMianMenu();//strcmp字符串比较函数
	 break;
   }
    else if(strcmp(username,"ht")==0&&strcmp(password,"ht")==0)
   {
     backMianMenu();//strcmp字符串比较函数
	 break;
   }
	else 
	{
	    system("cls");//清屏
		printf("请输入正确选项!按任意键返回:");
		getchar();
		getchar();//按任意键返回
	    number++;
	}
 }
}
//  ----后台页面设置

void backMianMeun()
{
     int choice;//选择功能项
     char con;//是否继续
     while(1)
	 {
//实现循环选择;程序主页面
     system("cls");
     printf("\n\n");
     printf("\t\t*******************************\t\t\n");
     printf("\t\t            后台主页面         \t\t\n");
     printf("\t\t*******************************\t\t\n");
     printf("\n\n");
     printf("\t\t\t1.商品维护:\n\n");
     printf("\t\t\t2.会员管理:\n\n");
     printf("\t\t\t3.库存提示:\n\n");
     printf("\t\t\t\t0.退出系统:\n\n");
     printf("\t\t请输入你的选择:\n\n");
     scanf("%d",&choice);      
        switch(choice)
		{
 	    case 1: goodsAdd();break;
    	case 2: memberManage();break;
    	case 3: stockWarn();break;
        case 0: printf("\t\t确认要退出吗(y/n)");
		        scanf("%c",&con);
		        if(con=='Y'||con=='N')
			    exit(0);//退出系统
		        break;
	    default: system("cls");//清屏
		printf("请输入正确选项!按任意键返回:");
		getchar();
		getchar();//按任意键返回
		}	 
	 }
}
//---------前台设置
void beforeMianMeun()
{
     int choice;//选择功能项
     char con;//是否继续
     while(1)
	 {
//实现循环选择;程序主页面
     system("cls");
     printf("\n\n");
     printf("\t\t*******************************\t\t\n");
     printf("\t\t            前台主页面         \t\t\n");
     printf("\t\t*******************************\t\t\n");
     printf("\n\n");
     printf("\t\t\t1.商品销售:\n\n");
     printf("\t\t\t2.库存预示:\n\n");
     printf("\t\t\t\t0.退出系统:\n\n");
     printf("\t\t请输入你的选择:\n\n");
     scanf("%d",&choice);      
        switch(choice)
		{
 	    case 1: goodsSale();break;
    	case 2: stockWarn();break;
        case 0: printf("\t\t确认要退出吗(y/n)");
		        scanf("%c",&con);
		        if(con=='Y'||con=='N')
			    exit(0);//退出系统
		        break;
	    default: system("cls");//清屏
		printf("请输入正确选项!按任意键返回:");
		getchar();
		getchar();//按任意键返回

		}
	 }
}


//  ---会员设置

void memberManage()
{
int choice;
char con;
     while(1)
	 {
	 system("cls");
     printf("\n\n");
     printf("\t\t*******************************\t\t\n");
     printf("\t\t             会员管理           \t\t\n");
     printf("\t\t*******************************\t\t\n");
     printf("\n\n");
     printf("\t\t\n1.会员添加\n\n");
	 printf("\t\t\n2.会员查询\n\n");
	 printf("\t\t\n3.会员统计\n\n");
	 printf("\t\t\n4.删除会员\n\n");
	 printf("\t\t\n0.返回主页面:");
	 printf("请输入你的选择:");
	 scanf("%d",&choice); 
	    switch(choice)
		{
	      case 1:memberdsAdd();break;//会员添加
	 	     case 2:memberQuery();break;//会员查询加
			    case 3:memberStat();break;//会员统计
					 case 4:memberDeleteMenu();break;//删除会员
						   case 0:
							     //返回主页面
							    printf("\t\t确认要返回主页面吗?(Y/N):");
							    scanf("%c",&con);
							    if(con=='Y'||con=='y')
								{
								  beforeMianMenu();
								}							   							  
								  break;							  
	   default:
		  system("cls");
          printf("请输入正确选择!(按任意键返回会员管理)");
		  getchar();getchar();//任意键返回
		}
	 }
}
void memberQurey()
{
   int choice;
   char con; 
   int number;
     while(1)
	 {
	 system("cls");
     printf("\n\n");
     printf("\t\t*******************************\t\t\n");
     printf("\t\t             会员查询          \t\t\n");
     printf("\t\t*******************************\t\t\n");
     printf("\n\n");
     printf("\t\t按会员编号查询:\t\n\n");
	 printf("\t\t按身份证号查询:\t\n\n");
	 printf("\t\t按会员名称查询:\t\n\n");
	 printf("\t\t\n0.返回主页面:");
	 printf("请输入你的选择:");
	 scanf("%d",&choice); 
	    switch(choice)
		{
		  case 1:
			printf("请输入会员编号:");
			scanf("%d",&number); 
			numberQuery(number,0);
            break;
          case 2: 
			identityQuery();
			break;
		  case 3: 
			nameQuery();
			break;
		  case 0: 
	       	printf("\t\t确认要返回主页面吗?(Y/N):");
		    scanf("%c",&con);
			if(con=='Y'||con=='y')
			 {
				beforeMianMenu();
			 }							   							  
			  break;
	   default:
		  system("cls");
          printf("请输入正确选择!(按任意键返回会员管理)");
		  getchar();getchar();//任意键返回
		}
	 }
}
//-----------会员统计


void memberStatMenu()
{
int choice;
char con;
   while(1)//循环选择
   {
     system("cls");
     printf("\n\n");
     printf("\t\t*******************************\t\t\n");
     printf("\t\t            会员统计           \t\t\n");
     printf("\t\t*******************************\t\t\n");
     printf("\n\n");
     printf("\t\t\n1.按会员编号降序排列\n\n");
	 printf("\t\t\n2.按会员编号升序排列\n\n");
	 printf("\t\t\n0.返回会员管理:");
	 scanf("%d",&choice); 
	     switch(choice)
		 {
		 case 1:
			 memberStat(1);//降序
			 break;
		 case 2:
			 memberStat(2);//升序
			 break;
	   	 case 0:
				 {//返回主页面
				   printf("\t\t确认要返回吗?(Y/N):");
				   scanf("%c",&con);
				   if(con=='Y'||con=='y')
					   return;
				  }
				  break;
		 default:
		  system("cls");
          printf("请输入正确选择!(按任意键返回)");
		  getchar();getchar();//任意键返回
		 }
   }
}



//----会员添加
void memberAdd()
{
struct member hy;
FILE *fp,*bhfp;
int bh=1;
char con;
  //打开一个二进制文件夹,添加数据
  if((fp=fopen("data\\member.dat","ab"))==NULL)
  {
  	
    fp=fopen("data\\member.dat","wb");
	return;
	
  }
  while(1)
  {
  if((bhfp=fopen("data\\memberNumber.dat","r"))==NULL)
   {
   printf("不能打开文件夹,可按任意键新建\n");
   bhfp=fopen("data\\memberNumber.dat","wb"); 
   
   putw(1,bhfp);
   fclose(bhfp);
   getchar();getchar();
   memberManage();
   }
   else
	{  
	  bh=getw(bhfp);
	}
   hy.memberNumber=bh;
   fclose(bhfp);


   system("cls");
   printf("\n\n");
   printf("\t\t*******************************\t\t\n");
   printf("\t\t            会员添加           \t\t\n");
   printf("\t\t*******************************\t\t\n");
   printf("\n\n");

   //会员信息录入
   printf("\t\t会员的编号:\t%d\n",bh);
   printf("\t\t请输入会员姓名:\n");
   scanf("%s",&hy.memberName);
   printf("\t\t请输入会员身份证号:\n");
   scanf("%s",&hy.identity);
   printf("\t\t请确认添加会员的信息:(Y/N)\n");
   scanf("%c",&con);
   if(con=='Y'||con=='y')
   {
      if((fwrite(&hy,sizeof(struct number),1,fp)!=1)
	  {
		printf("\n\n添加失败!\n");
	  }
	  else 
	  {
	   printf("\n\n添加成功!\n");
	   //保存会员
       if((bhfp=fopen("data\\memberNumber.dat","w"))==NULL)
	   {
	    printf("不能打开文件夹\n");
		putw(1,bhfp);
		return;	   
	   }
       //会员编号递增
	   putw(bh+1,bhfp);
       fclose(bhfp);
       getchar();getchar();
	  }
   }
   //返回处理
   printf("\t\t确认要返回吗?(Y/N):");
   scanf("%c",&con);
   if(con=='N'||con=='n')
   {
	   break;
   }
  }
  fclose(fp);
}



//---------会员编号查询
int numberQuery(int number)
{
  struct member hy;
  FILE *fp;
  if((fp=fopen("data\\member.dat","rb"))==NULL)
  {
    printf("不能打开文件夹!\n");
	return 0;
  }
  while(!feof(fp))
  {
	fread(&hy,sizeof(struct member),1,fp);
    if(hy.memberNumber==number)
	{

     system("cls");
     printf("\n\n");
     printf("\t\t*******************************\t\t\n");
     printf("\t\t         会员查询结果:        \t\t\n");
     printf("\t\t*******************************\t\t\n");
     printf("\n\n");
     printf("\t\t按会员编号查询:\t%d\n\n",hy.memberNumber);
	 printf("\t\t按会员身份证号查询\t%d\n\n",hy.identity);
	 printf("\t\t按会员名称查询:\t%s\n\n",hy.memberName);
     printf("\t\t按任意键返回会员查询页面");
	 getchar();getchar();
	 fclose(fp);
	 return 1;
	}
  }
    system("cls");
	printf("\n\n无此会员编号");
	printf("\t\t按任意键返回会员查询页面");
    getchar();getchar();
	fclose(fp);
	return 0;
}


//----按会员姓名查询

void nameQuery()
{
  struct member hy;
  int found=0;//0--未找到,1--找到。
  char memberName[20];
  FILE *fp;
  if((fp=fopen("data\\member.dat","rb"))==NULL)
  {
    printf("文件夹不能打开!\n");
	return;
  }
 pirntf("请输入会员姓名");
 scanf("%s",&memberName);
 while(!feof(fp))
 {
   fread(&hy,sizeof(struct member),1,fp);
   if(struct(hy.memberName,memberName)==0)
   {
     system("cls");
	 found=1;
     printf("\n\n");
     printf("\n\n");
     printf("\t\t*******************************\t\t\n");
     printf("\t\t         查询结果如下:        \t\t\n");
     printf("\t\t*******************************\t\t\n");
     printf("\n\n");
     printf("\t\t会员编号:\t%d\n\n",hy.memberNumber);
	 printf("\t\t会员身份证号\t%d\n\n",hy.identity);
	 printf("\t\t会员名称:\t%s\n\n",hy.memberName);
     printf("\t\t按任意键返回会员查询页面");
	 getchar();getchar();
	 break;
 }
   if(found==0)
   {
     system("cls");
     printf("\n\n无此会员");
	 printf("\t\t按任意键继续查询");
	 getchar();getchar();
   }
   fclose(fp);
}


//---按身份证号查询

void identityQuery()
{
  struct member hy;
  int found=0;//0--未找到,1--找到。
  char identity[19];
  FILE *fp;
  if((fp=fopen("data\\member.dat","rb"))==NULL)
  {
    printf("文件夹不能打开!\n");
	return;
  }
 pirntf("请输入会员身份证号");
 scanf("%s",&identity);
 while(!feof(fp))
 {
   fread(&hy,sizeof(struct member),1,fp);
   if(struct(hy.identity,identity)==0)
   {
     system("cls");
	 found=1;
     printf("\n\n");
     printf("\n\n");
     printf("\t\t*******************************\t\t\n");
     printf("\t\t         查询结果如下:        \t\t\n");
     printf("\t\t*******************************\t\t\n");
     printf("\n\n");
     printf("\t\t会员编号:\t%d\n\n",hy.memberNumber);
	 printf("\t\t会员身份证号\t%d\n\n",hy.identity);
	 printf("\t\t会员名称:\t%s\n\n",hy.memberName);
     printf("\t\t按任意键返回会员查询页面");
	 getchar();getchar();
	 break;
 }
   if(found==0)
   {
     system("cls");
     printf("\n\n无此会员");
	 printf("\t\t按任意键继续查询");
	 getchar();getchar();
   }
   fclose(fp);
}

 /* ---会员统计-----*/
int memberStat(int i)
{
  struct member hy;
  struct member memberinfo[1000];
  int countNum=0;
  int k,j;
  FILE *fp;
  if((fp=fopen("data\\member.dat","rb"))==NULL)
  {
    printf("不能打开文件夹!\n");
	return 0;
  }
 if(!feof(fp))
 {
   fread(&hy,sizeof(struct member),1,fp);
 }
 while(!feof(fp))
  {
   memberinfo[countNum]=hy;
   countNum++;
   fread(&hy,sizeof(struct member),1,fp);  
  }
 if(i==1)
	{
	 for(j=0;j<countNum;j++)
	  {
	  for(k=j+1;k<countNum;k++)
	  {
		  if(memberinfo[j].memberNumber<memberinfo[k].memberNumber)
		  {
		    struct member temp;
			temp=memberinfo[j];
			memberinfo[j]=memberinfo[k];
            memberinfo[k]=temp;		  
		  }
	  }
	 }
 }
else
	{
	 for(j=0;j<countNum;j++)
	  {
	  for(k=j+1;k<countNum;k++)
	  {
		  if(memberinfo[j].memberNumber>memberinfo[k].memberNumber)
		  {
		    struct member temp;
			temp=memberinfo[j];
			memberinfo[j]=memberinfo[k];
            memberinfo[k]=temp;		  
		  }
	  }
	 }
 }
   system("cls");
   printf("\n\n");
   printf("\t\t*******************************\t\t\n");
   printf("\t\t            会员列表           \t\t\n");
   printf("\t\t*******************************\t\t\n");
   printf("\n\n会员总数为:%d",countNum);
   printf("\n\n");
   printf("\t\t会员编号:");
   printf("\t\t会员名称:");
   printf("\t\t会员身份证号:");
   printf("\n\n");
   for(j=0;j<countNum;j++)
   {
   printf("\t\t%d",memberinfo[j].memberNumber);
   printf("\t\t%s",memberinfo[j].memberName);
   printf("\t\t%s",memberinfo[j].identity);
   printf("\t\n\n");
  }
   printf("\t\t按任意键返回");
   getchar();getchar();
   fclose(fp);
   return 1;
}


/*-----------按编号查询----------*/

int numberQuery(int number,int flag)
{
  struct member hy;
  FILE *fp;
  if((fp=fopen("data\\member.dat","rb"))==NULL)
  {
    printf("不能打开文件夹!\n");
	return 0;
  }
 while(!feof(fp))
 {
   fread(&hy,sizeof(struct member),1,fp);
   if(hy.memberNumber==number)
   {
      if(flag==0)
	  {
	   system("cls");
       printf("\n\n\n\t查询结果如下");
       printf("\t\t 会员编号: \t%d",hy.memberNumber);
       printf("\t\t 会员姓名: \t%s",hy.memberName);
       printf("\t\t 身份证号: \t%s",hy.identity);
       printf("\t\n\n");
    
       printf("\t\t按任意键返回");
       getchar();getchar();
       fclose(fp);
       return 1;	  
	  }
	  else 
	  {
	    fclose(fp);
		 return 1;	
		 break;
	  }
   }
 }    
      if(flag==0)
	  {
	   system("cls");
       printf("\n\n\n\t无此会员编号");
       printf("\t\n\n");
    
       printf("\t\t按任意键返回");
       getchar();getchar();
	  }
	  fclose(fp);
      return 0;	  
	  
}




/*-----------会员删除---------------*/

void deleteMemberMenu()
{
  char con;
  int choice;
  while(1)
  {
    system("cls");
     printf("\n\n");
     printf("\t\t*******************************\t\t\n");
     printf("\t\t             会员删除          \t\t\n");
     printf("\t\t*******************************\t\t\n");


     printf("\n\n");
     printf("\t\t\0、返回会员管理:\n\n");
	 printf("\t\t请输入删除会员的编号:\n");
	 scanf("%d",&choice);
	 if(choice==0)
	 {
	   break;	 
	 }
     else
	 {
	   if(numberQuery(choice,1)==1)
	   {
	     printf("\t\t确认要删除此会员吗?(Y/N)\n");
		 scanf("%s",&con);
		 if(con=='n'||con=='N')
		 {
			 break;
		 }
         
	   }
	   deleteMember(choice);
	 }
     else
	 {
	  system("cls");
      printf("\n\n无此会员");
	  printf("\t\t按任意键继续查询");
	  getchar();getchar();

	
      system("cls");
	  deleteMemberMenu();
	 }
 }

 
//-------会员删除
void deleteMember(int bianhao)
{
   
struct member hy[100];
FILE *fp;
int i,j;
char con;
int memberNumber=0;
 if((fp=fopen("data\\member.dat","rb"))==NULL)
 {
   printf("cannot open file\n");
   return;
 }
 while(!feof(fp))//feof检测流上的文件结束符
 {
    if(fread(&hy[memberNumber],sizeof(struct member),1,fp)==1)
	{
	 memberNumber++;
	}
	fclose(fp);
  for(i=0;i<memberNumber;i++)
	 {
	 if(hy[i].memberNumber==bianhao)
	 {
	  if(i==-1)//文件中没有数据
	  {
	     printf("无会员记录,按回车键返回会员管理界面");
		 memberManage();
	  }
	 forj=i;j<memberNumber;j++)
	 {
	  hy[j]=hy[j+1];
	 }
	}      
}
  
 if((fp=fopen("data\\member.dat","wb"))==NULL)
 {
   printf("cannot open file\n");
   return;
 }
  for(i=0;i<memberNumber-1;i++)
  {
   if((fp=fwrite(&hy[i],sizeof(struct member),1,fp)!=1)
	   printf("write error!");
  
  }
  fclose(fp);
}


/*   ---------商品维护------------  */


void goodsAdd()
{    
int i=1;
struct goods sp;
FILE *fp;
char con;
sp.stockPrice=0;
sp.salePrice=0;
  while(1)//实现循环添加商品
  {
	//程序主页面
     system("cls");
     printf("\n\n");
     printf("\t\t*******************************\t\t\n");
     printf("\t\t            商品维护           \t\t\n");
     printf("\t\t*******************************\t\t\n");
     printf("\n\n");
	 int i=1;
	 printf("请输入商品的编号:\n");
	 scanf("%d",&sp.goodsNumber);
	 printf("请输入商品的名称:\n");
	 scanf("%s",&sp.goodsName);
	 printf("请输入商品的生产日期:(YYYY-MM-DD)\n");
	 scanf("%d-%d-%d",&sp.produceDate.year,&sp.produceDate.month,&sp.produceDate.day);
	 printf("请输入商品的保质期:");
	 scanf("%d",&sp.assureDate);
	 printf("请输入商品的类型:");
	 scanf("%s",&sp.goodsType);
	 printf("请输入商品的进价:");
	 scanf("%f",&sp.stockPrice);
	 printf("请输入商品的数量:");
	 scanf("%d",&sp.amount);
	 printf("请输入商品的单价:");
	 scanf("%f",&sp.salePrice);
	 printf("\n确认添加商品吗?(Y/N):");
	 scanf("%c",&con);
	 if(con=='y'||con=='Y')
	 {
	  if((fp=fopen("data\\goods.dat","ab"))==NULL)
	  {
	   fp=fopen("data\\goods.dat","wb");
	   break;  
	  }
	  if(fwrite(&sp,sizeof(struct goods),1,fp)!=1)
	  {
		  printf("\n\n添加失败!\n\n");
	  }
	  else
	  {
	  printf("\n\n添加成功!\n");//保存商品
	  }
	  fclose(fp);
	 }
     //返回处理
     printf("\n\t确认添加商品吗?(Y/N):\n");
	 scanf("%c",&con);
	 if(con=='n'||con=='N')
	 {
	  break;
	 }
  }
}



//商品销售---购物清单
void goodsSale()
{
 struct goods sp;
 int chooseGoods[100][2];//保存用户选购商品数量和编号
 FILE *fp1;
 char con;//是否继续购买
 float discount;//商品打折
 int flag;
 int stock=0,buyKind=0,i;//buykind记录客户购买的种类
 float dueMoney=0,payMoney,change;//应付金额、实付金额,找零金额
    if((fp1=fopen("data\\goods.dat","rb"))==NULL)//提取商品信息
	{
    printf("cannot open file\n");
    return;
	}
    system("cls");
     printf("\n\n");
     printf("\t\t*******************************\t\t\n");
     printf("\t\t             商品销售          \t\t\n");
     printf("\t\t*******************************\t\t\n");
     printf("\n\n");
//选购商品
	 while(1)
	 {
	 printf("\n\t\t请输入商品的编号:\n");
	 scanf("%d",&chooseGoods[buyKind][0]);
     printf("\n\t\t请输入商品的数量:\n");
	 scanf("%d",&chooseGoods[buyKind][1]); 	
	 printf("\n 你购买的商品总价为:%.2f",dueMoney); 
	 printf("\n\t\t还要继续购买吗?(Y/N)\n");
	 scanf("%c",&con);
	 if(con=='n'||con=='N')
	   {
	     break;
	   }
	 buyKind++;
	 if(con=='Y'||con=='y')
	 {
	   printf("\n\t\t你是会员吗?(Y/N)\n");
	   scanf("%c",&con);
	   if(con=='Y'||con=='y')
	   {
	   //检查是否是会员
		 while(1)
		 {
		    printf("\n\t\t请输入你的会员编号:\n");
        	scanf("%d",&number);
			if(!numberQurey(number,1))
			{
			  flag=0;
			  printf("\t无此会员编号;\n");
			  continue;
			}
			else 
			{
			  flag=1;
			  break;
			}
		 
		 }
	   }
	 }
	 //完成商品总数量的处理
	 amountDeal(chooseGoods,buyKind);
	 //结算找零
	 printf("请你付钱:谢谢。");
	 scanf("%.2f",payMoney); 
	 if(flag==0)
	 {
	   //非会员情况
       if(payMoney==dueMoney)
	   {
	     printf("你付账的金额正合适,谢谢你的光临,再见!n\n");
		 getchar();getchar();
	   }
	   else
	   {
	     change=payMoney-dueMoney;
		 if(change>0)
		 {
		   printf("找你%.2f\n",change);
		   printf("谢谢你的光临,再见!\n");
		   getchar();getchar();
		 }
	      else
		  {
		    printf("金额不足,请重新操作!\n");
		    getchar();getchar();  
		  }
	   }
	 }
       else
	   {
	     //打折优惠处理
		  printf("\n\t\t你是否要打折(Y/N)\n");
	      scanf("%c",&con);
          if(con=='Y'||con=='y')
		  {
          printf("\n\t\t请你输入商品打折比例(0.00)\n");
		  scanf("%f",&discount);
		   {
		    if(payMoney==dueMoney*discount)	
			{
		       printf("你付账的金额正合适,谢谢你的光临,再见!n\n");
		       getchar();getchar();
			}
			else
			{
			  change=payMoney-dueMoney*discount;
			  if(change>0)
			  {
			    printf("找你%.2f\n",change);
		        printf("谢谢你的光临,再见!\n");
		        getchar();getchar();		  
			  }
			 else
			 {
			  printf("你付账的金额正合适,谢谢你的光临,再见!n\n");
		      getchar();getchar();			 
			 }
			}
		   }
		  }
	   }

     system("cls");
     printf("\n\n");
     printf("\t\t*******************************\t\t\n");
     printf("\t\t               购物清单        \t\t\n");
     printf("\t\t*******************************\t\t\n");
     printf("\t\t商品编号,\t商品名称,\t商品单价,\t商品数量\n\n");
	 for(i=0;i<=buyKind;i++)
	 {
	   while(!feof(fp1))
	   {
	    fread(&sp,sizeof(struct goods),1,fp1);
		if(chooseGoods[i][0]==sp.goodsNumber)
		{
		 int tempAmount=sp.amount;

         if(tempAmount<chooseGoods[i][1])
		 {
		 printf("\n\n\t%d\t\t%s\t\t%.2f\t\t%d",chooseGoods[i][0],sp.goodsName,sp.salePrice,sp.amount+1);
         printf("\t\t*******************************\t\t\n");
         printf("\t\t\n   库存不足,商品未计价,按回车键返回! ");
		 getchar();	 getchar();
		 beforeMianMeun();
		 }
		 else
		 {
		 dueMoney=dueMoney+chooseGoods[i][1]*sp.salePrice;
         printf("\n\n\t%d\t\t%s\t\t%.2f\t\t%d",chooseGoods[i][0],sp.goodsName,sp.salePrice,chooseGoods[i][1]);
		 printf("\t\t*******************************\t\t\n");
		 rewind(fp1);
		 }
		 break;
		}	 
	   }	 
	 }
	 fclose(fp1);
	 fclose(fp1);
  
}


//商品销售---动态处理商品
void amountDeal(int chooseGoods[][2],int buyKind)
{
struct goods sp[100];
FILE *fp;
int i,j;
int goodsNumber=0;
 if((fp=fopen("data\\goods.dat","rb"))==NULL)//提取商品信息
 {
   printf("cannot open file\n");
   return;
 }
 while(!feof(fp))//feof检测流上的文件结束符
 {

    if(fread(&sp[goodsNumber],sizeof(struct goods),1,fp)==1)
	{
		goodsNumber++;
	}
   fclose(fp);

   //将库存商品数量减去已销售数量
	 for(j=1;j<goodsNumber;j++)
	 {
	   for(i=1;i<buyKind;i++)
	   {	 
	     if(sp[j].goodsNumber==chooseGoods[i][0])
		 {
		  sp[j].amount=sp[j].amount-chooseGoods[i][1];
		  break;
		 }
	   }
	 }
	 if((fp=fopen("data\\goods.dat","wb"))==NULL)//数据写入
	 {
      printf("cannot open file\n");
      return;
	 }
	 for(i=1;i<goodsNumber;i++)
	 {
	  if(fwrite(&sp[i],sizeof(struct goods),1,fp)!=1)
    	printf("write error");
	 }
fclose(fp);
}

/*-----超市库存预警------*/
 
void stockWarn()
{
struct goods sp;
FILE *fp;
int stock=0;
 if((fp=fopen("data\\goods.dat","rb"))==NULL)//提取商品信息
 {
   printf("cannot open file\n");
   return;
 }
 system("cls");
     printf("\n\n");
     printf("\t\t*******************************\t\t\n");
     printf("\t\t               库存预警        \t\t\n");
     printf("\t\t*******************************\t\t\n");


	 while(!feof(fp1))//feof检测流上的文件结束符
	 {

       if(fread(&sp,sizeof(struct goods),1,fp)==1)
       {
	     if(sp.amount<100)
		 {
		   if(stock==0)
		   {
		     printf("\n\t\t剩余商品不足100件,请你尽快补充货源!\n");
		   }
		   stock==1;
		   printf("\t\t商品编号:");
           printf("\t\t商品名称:");
           printf("\t\t商品数量:");
           printf("\t\t*******************************\t\t\n");
		 }	   
	   }
	 }
    printf("\n\n按任意键返回");
    getchar();getchar();
	fclose(fp);
}

//----主函数
void main()
{
   //定义日期结构体 
   struct date
   {
     int year;
     int month;
     int day;
   };
   //定义商品结构体 
   struct goods
   {
   int goodsNumber;//商品编码
   char goodsName[20];//商品名称
   char goodsType[20];//商品类型
   struct date produceDate;//生产日期
   int assureDate;//保质期
   float stockPrice;//商品进价
   int amount;//商品数量
   float salePrice;//商品单价
   };
   //定义会员结构体 
  struct member
  {
    int memberNumber;//会员编号
    char identity[20];//身份编号证
    char memberName[20];//会员名称
  };
login();
getchar();
}