• 结构体快排


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

    1. /* 
    2. 题目: 
    3. 学生成绩排序:成绩从低到高,姓名字母从低到高(区分大小写),年龄从低到高排序 
    4. 输入: 
    5. abc 20 99 
    6. bcd 19 97 
    7. bed 20 97 
    8.  
    9. 输出: 
    10. bcd 19 97 
    11. bed 20 97 
    12. abc 20 99 
    13.  
    14. 易错点: 
    15. 1对于字符指针,scanf("%s",ps[i].sName)不需要取地址符 
    16. 2对于名字,要指定大小,否则内存访问非法,char sName[128];而不是定义char* sName; 
    17. 3int strcmp(const char* string1,const char* string2); <0:前<后 
    18.  
    19.  
    20. 得分:0 
    21. */  
    22.   
    23. #include <stdio.h>  
    24. #include <malloc.h>  
    25. #include <string.h>  
    26.   
    27. typedef struct Student  
    28. {  
    29.     //Student(char* name,int age,int grade):sName(name),iAge(age),iGrade(grade){}  
    30.     //char* sName;//就是这里出错了,没有具体制定多大  
    31.     //char* sName;  
    32.   
    33.     //法二,用重载操作符operator<的方法做  
    34.     bool operator < (const Student& stu) const  
    35.     {  
    36.         if(iGrade != stu.iGrade)  
    37.         {  
    38.             return iGrade < stu.iGrade;  
    39.         }  
    40.         else  
    41.         {  
    42.             if(sName != stu.sName)  
    43.             {  
    44.                 return strcmp(sName,stu.sName) < 0 ? true:false;  
    45.             }  
    46.             else  
    47.             {  
    48.                 return iAge < stu.iAge;  
    49.             }  
    50.         }  
    51.     }  
    52.     char sName[128];  
    53.     int iAge;  
    54.     int iGrade;  
    55. }Student;  
    56.   
    57. bool compare(struct Student stuA,struct Student stuB)  
    58. {  
    59.     if(stuA.iGrade < stuB.iGrade)  
    60.     {  
    61.         return true;  
    62.     }  
    63.     else if(stuA.iGrade == stuB.iGrade )  
    64.     {  
    65.         if(stuA.sName!=stuB.sName)  
    66.         {  
    67.             return strcmp(stuA.sName,stuB.sName) < 0 ? true:false ;//这里中断了  
    68.         }  
    69.         else  
    70.         {  
    71.             return stuA.iAge < stuB.iAge;  
    72.         }  
    73.     }  
    74.     else  
    75.     {  
    76.         return false;  
    77.     }  
    78. }  
    79.   
    80.   
    81. int partition(Student* ps,int low,int high)  
    82. {  
    83.     Student stu = ps[low];  
    84.     while(low < high)  
    85.     {  
    86.         //while(low < high && compare(stu,ps[high]))//法1  
    87.         while(low < high && stu < ps[high])  
    88.         {  
    89.             high--;  
    90.         }  
    91.         ps[low] = ps[high];  
    92.         //while(low < high && compare(ps[low],stu))//法1  
    93.         while(low < high && ps[low] < stu)  
    94.         {  
    95.             low++;  
    96.         }  
    97.         ps[high] = ps[low];  
    98.     }  
    99.     ps[low] = stu;  
    100.     return low;  
    101. }  
    102.   
    103. void quickSort(Student* ps,int low,int high)  
    104. {  
    105.     if(low < high)  
    106.     {  
    107.         int iPos = partition(ps,low,high);  
    108.         quickSort(ps,low,iPos-1);  
    109.         quickSort(ps,iPos+1,high);  
    110.     }  
    111. }  
    112.   
    113. void print(Student* ps,int iNum)  
    114. {  
    115.     for(int i = 0;i < iNum ;i++)  
    116.     {  
    117.         printf("%s %d %d ",ps[i].sName,ps[i].iAge,ps[i].iGrade);  
    118.     }  
    119. }  
    120.   
    121.   
    122. int main(int argc,char* argv[])  
    123. {  
    124.     int iNum;  
    125.     while(EOF!=scanf("%d",&iNum))  
    126.     {  
    127.         //ptrStu ps = (ptrStu)malloc(iNum*sizeof(Student));  
    128.         Student* ps = (Student*)malloc(iNum*sizeof(Student));  
    129.         //Student ps[100];  
    130.         for(int i = 0; i < iNum; i++)  
    131.         {  
    132.             //char* sName;  
    133.             //int iAge,iGrade;  
    134.             //scanf("%s %d %d",&ps[i].sName,&ps[i].iAge,&ps[i].iGrade); //关键字符串不需要取地址,因为它本身就是一个地址,否则就是取地址的地址了  
    135.             scanf("%s %d %d",ps[i].sName,&ps[i].iAge,&ps[i].iGrade);  
    136.             //Student *stu = (Student*)malloc(1*sizeof(Student))(sName,iAge,iGrade);  
    137.             //ps[i] = stu;//难道是因为出了这里就不存在了  
    138.         }  
    139.         //printf("%s ",ps[1].sName);  
    140.         //print(ps,iNum);  
    141.         quickSort(ps,0,iNum-1);  
    142.         print(ps,iNum);  
    143.         free(ps);  
    144.     }  
    145.     //getchar();  
    146.     return 0;  
    147. }  
  • 相关阅读:
    移动端屏幕旋转的事件和样式方案。
    active:移动端触摸按钮的效果。
    移动端字体单位该使用px还是rem?
    Cordova/Ionic Android 开发环境搭建
    JavaScript 深拷贝(deep copy)和浅拷贝(shallow copy)
    你不知道的JS之 this 和对象原型(一)this 是什么
    你不知道的JS之作用域和闭包 附录
    你不知道的JS之作用域和闭包(五)作用域闭包
    你不知道的JS之作用域和闭包(四)(声明)提升
    你不知道的JS之作用域和闭包(三)函数 vs. 块级作用域
  • 原文地址:https://www.cnblogs.com/cglongge/p/9069001.html
Copyright © 2020-2023  润新知