⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 smprice.c

📁 商品管理系统 1)进货员对商品基本信息进行输入、删除、修改和查询 (2)销售员对商品的基本信息进行查询和统计 (3)并可以对库存信息进行查询和修改 (4)顾客购买商品
💻 C
字号:
#ifndef SORTBYMEMBERPRICE
  #define SORTBYMEMBERPRICE
  #include "stdio.h"
  #include "shead.h"
  /* 按会员价排序浏览 */
  void SortByMemberPrice()
  {
   int i,j,k;
   product TempS;/* 临时结构体变量 */
   product *s,*t;
   int total=PRODUCTSIZE;
   int recNumber=0;
   char DataFile[40]="product";
   FILE * fp=fopen(DataFile,"rb");
   if(fp==NULL)
   {
    printf("\nOpen file %s fail!End with any key.",DataFile);
    perror("Open file fail");
    fflush(stdin);
    getch();
    return;
   }
   /* 动态分配内存空间。 */
   s=(product*)malloc(total*sizeof(product));
   if(s==NULL)
   {
    printf("malloc memory fail!End with any key.\n");
    fflush(stdin);
    getch();
    return;
   }
   clrscr();
   while(fread(&s[recNumber],sizeof(product),1,fp)!=(int)NULL)
   {
        recNumber++;
        if(recNumber>=total)
        {
         total+=PRODUCTINCREASE;
         t=(product*)realloc(s,total*sizeof(product));
         if(t==NULL)
         {
          printf("allot memory fail!End with any key.");
          free(s);/* 释放内存空间 */
          fflush(stdin);
          exit(0);
         }
         s=t;
         t=NULL;
        }
   }
   fclose(fp);
   /* 如果文件中有记录则排序 */
   if(recNumber>1)
   {
    for(i=0;i<recNumber-1;i++)
    {
     k=i;
     for(j=i+1;j<recNumber;j++)
     {
      if(s[k].memberprice<s[j].memberprice) k=j;
     }
     if(k!=i)
     {
      TempS.Number=s[k].Number;
      strcpy(TempS.Name,s[k].Name);
      TempS.price=s[k].price;
      TempS.discount=s[k].discount;
      TempS.memberprice=s[k].memberprice;

      s[k].Number=s[i].Number;
      strcpy(s[k].Name,s[i].Name);
      s[k].price=s[i].price;
      s[k].discount=s[i].discount;
      s[k].memberprice=s[i].memberprice;

      s[i].Number=TempS.Number;
      strcpy(s[i].Name,TempS.Name);
      s[i].price=TempS.price;
      s[i].discount=TempS.discount;
      s[i].memberprice=TempS.memberprice;
     }
    }
    /* 将排序好的记录写入文件 */
    fp=fopen(DataFile,"wb+");
    if(fp==NULL)
    {
     printf("open file %s fial!End with any key.\n",DataFile);
     perror("Open file fail");
     fflush(stdin);
     getch();
     free(s);/* 释放内存空间 */
     return;
    }
    for(i=0;i<recNumber;i++)
    {
      if(fwrite(&s[i],sizeof(product),1,fp)!=1)
      {
       printf("write file %s fail!end with any key.\n",DataFile);
       perror("write file fail");
       fflush(stdin);
       getch();
       return;
      }
    }
    fclose(fp);
   }else if(recNumber==0)
   {
    printf("No record!!\npress any key to countinue...\n");
    fflush(stdin);
    getch();
    free(s);/* 释放内存空间 */
    return;
   }
   /* 显示排序后的文件 */
   printf("the product's information in file %s is as flow:\n",DataFile);
   fp=fopen(DataFile,"rb");
   if(fp==NULL)
   {
    printf("\nOpen file %s fail!End with any key\n",DataFile);
    perror("Open file fail");
    fflush(stdin);
    getch();
    free(s);/* 释放内存空间 */
    return;
   }
   printf("\nMumber\tName\tprice\t\tdiscount\t\tmemberPrice\n");
   while(fread(&TempS,sizeof(product),1,fp)!=(int)NULL)
   {
    printf("%ld\t%s\t%.2f\t\t%-8.2f\t\t%.3f\n",TempS.Number,TempS.Name,TempS.price,TempS.discount,TempS.memberprice);
   }
   fclose(fp);
   free(s);/* 释放内存空间 */
   printf("Press any key to continue...");
   fflush(stdin);
   getch();
  }
#endif

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -