结构体快排

转载自:https://blog.csdn.net/qingyuanluofeng/article/details/47159193

  1. /* 
  2. 题目: 
  3. 学生成绩排序:成绩从低到高,姓名字母从低到高(区分大小写),年龄从低到高排序 
  4. 输入: 
  5. 3 
  6. abc 20 99 
  7. bcd 19 97 
  8. bed 20 97 
  9.  
  10. 输出: 
  11. bcd 19 97 
  12. bed 20 97 
  13. abc 20 99 
  14.  
  15. 易错点: 
  16. 1对于字符指针,scanf("%s",ps[i].sName)不需要取地址符 
  17. 2对于名字,要指定大小,否则内存访问非法,char sName[128];而不是定义char* sName; 
  18. 3int strcmp(const char* string1,const char* string2); <0:前<后 
  19.  
  20.  
  21. 得分:0 
  22. */  
  23.   
  24. #include <stdio.h>  
  25. #include <malloc.h>  
  26. #include <string.h>  
  27.   
  28. typedef struct Student  
  29. {  
  30.     //Student(char* name,int age,int grade):sName(name),iAge(age),iGrade(grade){}  
  31.     //char* sName;//就是这里出错了,没有具体制定多大  
  32.     //char* sName;  
  33.   
  34.     //法二,用重载操作符operator<的方法做  
  35.     bool operator < (const Student& stu) const  
  36.     {  
  37.         if(iGrade != stu.iGrade)  
  38.         {  
  39.             return iGrade < stu.iGrade;  
  40.         }  
  41.         else  
  42.         {  
  43.             if(sName != stu.sName)  
  44.             {  
  45.                 return strcmp(sName,stu.sName) < 0 ? true:false;  
  46.             }  
  47.             else  
  48.             {  
  49.                 return iAge < stu.iAge;  
  50.             }  
  51.         }  
  52.     }  
  53.     char sName[128];  
  54.     int iAge;  
  55.     int iGrade;  
  56. }Student;  
  57.   
  58. bool compare(struct Student stuA,struct Student stuB)  
  59. {  
  60.     if(stuA.iGrade < stuB.iGrade)  
  61.     {  
  62.         return true;  
  63.     }  
  64.     else if(stuA.iGrade == stuB.iGrade )  
  65.     {  
  66.         if(stuA.sName!=stuB.sName)  
  67.         {  
  68.             return strcmp(stuA.sName,stuB.sName) < 0 ? true:false ;//这里中断了  
  69.         }  
  70.         else  
  71.         {  
  72.             return stuA.iAge < stuB.iAge;  
  73.         }  
  74.     }  
  75.     else  
  76.     {  
  77.         return false;  
  78.     }  
  79. }  
  80.   
  81.   
  82. int partition(Student* ps,int low,int high)  
  83. {  
  84.     Student stu = ps[low];  
  85.     while(low < high)  
  86.     {  
  87.         //while(low < high && compare(stu,ps[high]))//法1  
  88.         while(low < high && stu < ps[high])  
  89.         {  
  90.             high--;  
  91.         }  
  92.         ps[low] = ps[high];  
  93.         //while(low < high && compare(ps[low],stu))//法1  
  94.         while(low < high && ps[low] < stu)  
  95.         {  
  96.             low++;  
  97.         }  
  98.         ps[high] = ps[low];  
  99.     }  
  100.     ps[low] = stu;  
  101.     return low;  
  102. }  
  103.   
  104. void quickSort(Student* ps,int low,int high)  
  105. {  
  106.     if(low < high)  
  107.     {  
  108.         int iPos = partition(ps,low,high);  
  109.         quickSort(ps,low,iPos-1);  
  110.         quickSort(ps,iPos+1,high);  
  111.     }  
  112. }  
  113.   
  114. void print(Student* ps,int iNum)  
  115. {  
  116.     for(int i = 0;i < iNum ;i++)  
  117.     {  
  118.         printf("%s %d %d\n",ps[i].sName,ps[i].iAge,ps[i].iGrade);  
  119.     }  
  120. }  
  121.   
  122.   
  123. int main(int argc,char* argv[])  
  124. {  
  125.     int iNum;  
  126.     while(EOF!=scanf("%d",&iNum))  
  127.     {  
  128.         //ptrStu ps = (ptrStu)malloc(iNum*sizeof(Student));  
  129.         Student* ps = (Student*)malloc(iNum*sizeof(Student));  
  130.         //Student ps[100];  
  131.         for(int i = 0; i < iNum; i++)  
  132.         {  
  133.             //char* sName;  
  134.             //int iAge,iGrade;  
  135.             //scanf("%s %d %d",&ps[i].sName,&ps[i].iAge,&ps[i].iGrade); //关键字符串不需要取地址,因为它本身就是一个地址,否则就是取地址的地址了  
  136.             scanf("%s %d %d",ps[i].sName,&ps[i].iAge,&ps[i].iGrade);  
  137.             //Student *stu = (Student*)malloc(1*sizeof(Student))(sName,iAge,iGrade);  
  138.             //ps[i] = stu;//难道是因为出了这里就不存在了  
  139.         }  
  140.         //printf("%s\n",ps[1].sName);  
  141.         //print(ps,iNum);  
  142.         quickSort(ps,0,iNum-1);  
  143.         print(ps,iNum);  
  144.         free(ps);  
  145.     }  
  146.     //getchar();  
  147.     return 0;  
  148. }  
posted @ 2018-05-21 19:53  cglong  阅读(328)  评论(0)    收藏  举报