4.19 使用qsort对结构体数组进行排序,实现对结构的体一级排序和二级排序,进一步了解qsort的原理

qsort对结构体数组进行排序时,可以根据结构体元素中的任意某个成员进行比较之后,如果要交换则会连带结构体中其他成员的一起进行整体的结构体元素交换所以感觉真是万能排序接口

只进行对结构体的一级排序
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct _stu
{
    char name[10];
    float score;
}Stu;

int callBackCompare(const void * pa, const void * pb)//一级升序
{
#if 0//正确
    if(strcmp((*(Stu*)pa).name,(*(Stu*)pb).name)>0)
        return 1;
    else
        return 0;//或return -1;都行,毕竟qsort只对正数感兴趣
#endif

#if 1  //正确
    if(strcmp(((Stu*)pa)->name,((Stu*)pb)->name) > 0 )
        return 1;
    else
        return 0;//或return -1;都行,毕竟qsort只对正数感兴趣

#endif
}

int main(void)
{
    Stu stu[] = {{"aaa",23.5},
                 {"xxx",45.6},
                 {"bbb",89},
                 {"xxx",23.4},
                 {"yyy",100}};

    qsort(stu,sizeof(stu)/sizeof(*stu),sizeof(Stu),callBackCompare);

    int i;
    for(i = 0;i<5;i++)
    {
        printf("%s,%f\n",stu[i].name,stu[i].score);

    }

    return 0;
}

对结构体一级排序的同时进行二级排序
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct _stu
{
    char name[10];
    float score;
}Stu;

int callBackCompare(const void * pa, const void * pb)//一级升序二级降序
{
#if 0//正确
    if(strcmp((*(Stu*)pa).name,(*(Stu*)pb).name)>0)//对1级进行升序
        return 1;
    else
    {
        if( (*(Stu*)pa).score < (*(Stu*)pb).score )//对2级进行降序
        {
            return 1;
        }
        else
            return 0;
    }
#endif

#if 0  //正确
    if(strcmp(((Stu*)pa)->name,((Stu*)pb)->name) > 0 )
        return 1;
    else
    {
        if( ((Stu*)pa)->score < ((Stu*)pb)->score )//对2级进行降序
        {
            return 1;
        }
        else
            return 0;
    }

#endif
}

int main(void)
{
    Stu stu[] = {{"aaa",23.5},
                 {"xxx",45.6},
                 {"bbb",89},
                 {"xxx",23.4},
                 {"yyy",100}};

    qsort(stu,sizeof(stu)/sizeof(*stu),sizeof(Stu),callBackCompare);

    int i;
    for(i = 0;i<5;i++)
    {
        printf("%s,%f\n",stu[i].name,stu[i].score);

    }

    return 0;
}

原文地址:https://www.cnblogs.com/ZhuLuoJiGongYuan/p/9516950.html

时间: 2024-08-09 00:19:43

4.19 使用qsort对结构体数组进行排序,实现对结构的体一级排序和二级排序,进一步了解qsort的原理的相关文章

结构体数组与用malloc申请结构体空间的对比

结构体数组与用malloc申请结构体空间的对比 文章标题听起来很拗口,可能我描述的不太清楚,还是看例程吧: 我先写以前最早会用的malloc: 1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <string.h> 4 5 struct student 6 { 7 char *name; 8 int age; 9 }; 10 11 int main() 12 { 13 struct student *p_stud

NumPy-快速处理数据--ndarray对象--多维数组的存取、结构体数组存取、内存对齐、Numpy内存结构

本文摘自<用Python做科学计算>,版权归原作者所有. 上一篇讲到:NumPy-快速处理数据--ndarray对象--数组的创建和存取 接下来接着介绍多维数组的存取.结构体数组存取.内存对齐.Numpy内存结构 一.多维数组的存取 多维数组的存取和一维数组类似,因为多维数组有多个轴,因此它的下标需要用多个值来表示,NumPy采用组元(tuple)作为数组的下标.如二维数组需要(x, y)的元组标记一个数组元素:三维数组需要(x, y, z)的元组标记一个元素. 如下图所示,a为一个6x6的二

黑马程序员-----结构体数组

------<a href="http://www.itheima.com" target="blank">Java培训.Android培训.iOS培训..Net培训</a>.期待与您交流! ----- 第一讲  结构体数组 一.结构体数组的概念       数组的元素也可以是结构类型的.因此可以构成结构型数组.结构数组的每一个元素都是具有相同结构类型的下表结构变量.在实际应用中,经常用结构数组来表示具有相同数据结构的一个群体.如一个班的学生

C的日记-结构体变量和结构体数组

[结构体] 定义结构体的两方式    <1>    struct student{};        struct student a={10001,"云中",'M',"北京"};    <2>    struct student{        }a={10001,"云中",'M',"北京"};定义结构体数组a换成a[],struct student stu[3]={{..},{..},{..}};

编程题:指针变量指向结构体数组。

编程题:指针变量指向结构体数组. #include<stdio.h> void main() { struct person {char name[20]; char sex; int age; float height; }per[3]={{"Li Ping",'M',20,175},{"Wang Ling",'F',19,162.5}, {"Zhao Hui",'M',20,178}}; struct person *p; for

编程题:结构体数组的引用。功能:输出结构体数组各元素的成员值

编程题:结构体数组的引用.功能:输出结构体数组各元素的成员值 #include<stdio.h> void main() { struct person { char name[20]; char sex; int age; float height; }per[3]={{"Li Ping",'M',20,175},{"Wang Ling",'F',19,162.5}, {"Zhao Hui",'M',20,178}}; int i;

黑马程序员---C基础12【结构体数组】【结构体指针】【结构体嵌套】【作为函数参数】【枚举类型】

------Java培训.Android培训.iOS培训..Net培训.期待与您交流! ------- [结构体数组] 1.结构体数组: 结构体数组的每一个元素都是具有相同结构类型的下标结构变量:(可以表示一个群体,一个班学生档案) 2.结构数组定义: 定义格式: struct    结构名{ 成员列表: }数组名[数组长度]: 1 1)定义结构体的同时定义数组: 2 3 struct stu{ 4 5 int num; 6 7 char name[10]; 8 9 int age; 10 11

结构体数组的排序

按照结构体数组的某一项排序,那么一个结构体包含的元素仍保持不变 结果如下: 代码: 1 #include<algorithm> 2 #include<iostream> 3 using namespace std; 4 5 const int M = 3; 6 7 struct two { 8 double w; 9 double v; 10 }ss[M]; 11 bool cmp(two a,two b) 12 { 13 return a.v > b.v; //按照从大到小

结构体数组初始化三种方法,转载

C语言结构体初始化的三种方法:原文链接http://www.2cto.com/kf/201503/386575.html 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 #include <stdio.h> struct student_st {     char c;     int score;     const c