zoukankan      html  css  js  c++  java
  • c qsort()的辅助函数的用法

    什么都不用说,通过一个样例看看吧.

    typedef struct
    {
     int LinkID;
     int classnum;
     char name[20];
    }map;

    int comp1(const void *a,const void *b)
    {
     map *c=(map *)a;
     map *d=(map *)b;
     if(c->LinkID!=d->LinkID)
         return c->LinkID-d->LinkID;
        else
            return d->LinkID-c->LinkID;
    }

    int comp2(const void *a,const void *b)
    {
     map *c=(map *)a;
     map *d=(map *)b;
     if(c->classnum!=d->classnum)
         return c->classnum-d->classnum;
        else
            return d->classnum-c->classnum;
    }

    void sortInfo(int const n)
    {
     FILE *file=fopen("map.txt","r+");
     map mymap[200];
     int i=0;
     if(file==NULL)
     {
      printf("打开文件失败!\n");
      exit(0); 
     }
     
     fread(mymap,sizeof(map),n,file);
     
     printf("输入排序方式(1 按LinkID; 2 按 classnum): ");
     scanf("%d",&i);
     if(i==1)
        qsort(mymap,n,sizeof(map),comp1);
      else
           qsort(mymap,n,sizeof(map),comp2);
          
        rewind(file);  //重置位置指针.
        fwrite(mymap,sizeof(map),n,file);
        fclose(file);
    }

  • 相关阅读:
    C#深入浅出 修饰符(二)
    HDU 5785 Interesting
    HDU 5783 Divide the Sequence
    HDU 5781 ATM Mechine
    UVA 714 Copying Books
    uva 1471 Defense Lines
    UVA 11134 Fabled Rooks
    UVA 11572 Unique Snowflakes
    UVA 11093 Just Finish it up
    UVA 10954 Add All
  • 原文地址:https://www.cnblogs.com/wmx3ng/p/2775961.html
Copyright © 2011-2022 走看看