herochenling 2015-08-25 01:34 采纳率: 33.3%
浏览 5055
已采纳

在c中怎样用qsort对结构体数组进行多级排序?

比如说 我有个结构体数组里面的每个元素是 struct a{ int cat; string train; double plane; float tree;};
然后首先按cat从小到大排,然后train从大到小,接着plane大到小,最后tree 小到大.我已经写了个comparator了,但是输出结果跟预期不一样. 然后我的问题是我这样写多级排序的comparator对吗?不对的话该怎么写? 还有folat double这种类型的camparator怎么写?如果像我那样直接相减,是不是会发生溢出之类的情况?图片说明

  • 写回答

2条回答

  • qq1223386926 2015-08-26 14:44
    关注
    #include <stdlib.h>
    #include <string.h>
    
    struct st_test
    {
            int cat;
            char caTrain[ 100 ];
            double plane;
            float  tree;
    };
    
    int cmp( const void *arg1, const void *arg2 );
    
    int
    main( void )
    {
            int i = 0;
            struct st_test stArr[] = {
                    { 2, "defghi", 5.2, 6.7 },
                    { 2, "aefghi", 5.0, 6.9 },
                    { 1, "aefghi", 5.1, 6.0 },
                    { 1, "aefghi", 5.9, 5.0 },
                    { 3, "elflfg", 9.0, 2.0 },
                    { 3, "elflfg", 9.0, 1.0 }
            };
    
            qsort( stArr, 6, sizeof( struct st_test ), cmp );
    
            for( i = 0; i < 6; i++ )
            {
                    printf( "%2d %8s %3.1lf %3.1f\n", stArr[ i ].cat, stArr[ i ].caTrain,
                            stArr[ i ].plane, stArr[ i ].tree );       
            }
            return 0;
    }
    
    int 
    cmp( const void *arg1, const void *arg2 )
    {
            static const double DSTDZERO = 0.0000001;
            static const float  FSTDZERO = 0.0000001;
            struct st_test *stArg1 = ( struct st_test * )arg1;
            struct st_test *stArg2 = ( struct st_test * )arg2;
    
            if( stArg1->cat != stArg2->cat )
                    return  stArg1->cat > stArg2->cat ? 1 : -1;
            else if( strcmp( stArg1->caTrain, stArg2->caTrain ) != 0 )
                    return -strcmp( stArg1->caTrain, stArg2->caTrain );
            else if( !( ( stArg1->plane - stArg2->plane >= -DSTDZERO ) && ( stArg1->plane - stArg2->plane <= DSTDZERO ) ) )
            {
                    if( stArg1->plane - stArg2->plane < -DSTDZERO )
                            return 1;
    
                    if( stArg1->plane - stArg2->plane > DSTDZERO )
                            return -1;
            } 
            else if( !( ( stArg1->tree - stArg2->tree >= -FSTDZERO ) && ( stArg1->tree - stArg2->tree <= FSTDZERO ) ) )
            {
                    if( stArg1->tree - stArg2->tree < -FSTDZERO )
                            return -1;
    
                    if( stArg1->tree - stArg2->tree > FSTDZERO )
                            return 1;
            } 
    
            return 0;
    }
    ![图片说明](https://img-ask.csdn.net/upload/201508/26/1440600253_53796.png)
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥50 易语言把MYSQL数据库中的数据添加至组合框
  • ¥20 求数据集和代码#有偿答复
  • ¥15 关于下拉菜单选项关联的问题
  • ¥20 java-OJ-健康体检
  • ¥15 rs485的上拉下拉,不会对a-b<-200mv有影响吗,就是接受时,对判断逻辑0有影响吗
  • ¥15 使用phpstudy在云服务器上搭建个人网站
  • ¥15 应该如何判断含间隙的曲柄摇杆机构,轴与轴承是否发生了碰撞?
  • ¥15 vue3+express部署到nginx
  • ¥20 搭建pt1000三线制高精度测温电路
  • ¥15 使用Jdk8自带的算法,和Jdk11自带的加密结果会一样吗,不一样的话有什么解决方案,Jdk不能升级的情况