zoukankan      html  css  js  c++  java
  • C语言 · 运用结构体的排序方法

    C语言 · 运用结构体的排序方法

      之前遇到排序只想着最原始的方法,诸如冒泡,选择,快速排序等等,刚刚跟大牛学会了结构体的方法来排序,这样的话以后再也不用怕成绩统计、名次排序之类的题目了。

    首先头文件(基于大牛的方法,本人之后做题喜欢引入题目中常用的五个头文件)

    1
    2
    #include<stdlib.h>
    #include<string.h>

    定义结构体:

    1
    2
    3
    4
    5
    6
    /*定义一个结构体*/
    typedef struct Stu{
    char name[10];
    int id;
    int score;
    }stu;

    注释:最后一行stu是别名。

    定义排序(回调)函数:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    /*定义排序函数*/
    int cmp(const void *a,const void *b){
        stu c = *(stu*)a;
        stu d = *(stu*)b;
        //printf("%d ",strcmp(c.name,d.name));
        if(strcmp(c.name,d.name)>0){/*返回值是0、1*/<br>      return strcmp(c.name,d.name);
        }
        else{
            if(strcmp(c.name,d.name)==0){
                return c.id-d.id;
            }
        }
    }

    或者:

    1
    2
    3
    int cmp(const void *c,const void *d){
        return *(int *)c - *(int *)d;
    }

      

    使用qsort函数:

    1
    qsort(st,n,sizeof(st[0]),cmp);
    头文件:stdlib.h
    用 法: void qsort(void *base,int nelem,int width,int (*fcmp)(const void *,const void *));
    参数:
    1 :待排序数组首地址;
    2 :数组中待排序元素数量;
    3 :单个元素的大小,推荐使用sizeof(st[0])这样的表达式;
    4 :指向函数的指针,用于确定排序的顺序.

    下面给出一个成绩排序程序的完整代码:

    代码一:原始方法:

    复制代码
     1 #include<stdio.h>
     2 #include<string.h>
     3 int main()
     4 {
     5     int n;
     6     char name[20];
     7     char sex[20];
     8     char year[20];
     9     int score[200];
    10     
    11     int max = -1;
    12     int mix = 200;
    13     /*最高分者信息*/
    14     char maxname[20];
    15     char maxsex[20];
    16     char maxyear[20];
    17     /*最低分者信息*/ 
    18     char mixname[20];
    19     char mixsex[20];
    20     char mixyear[20];
    21     
    22     scanf("%d",&n);
    23     for(int i=0;i<n;i++){
    24         scanf("%s",name);
    25         scanf("%s",sex);
    26         scanf("%s",year);
    27         scanf("%d",&score[i]);
    28         /*若当前输入的分数比mix小,则将此条信息记录为最低分者*/
    29         if(score[i]<mix){
    30             strcpy(mixname,name);
    31             strcpy(mixsex,sex);
    32             strcpy(mixyear,year);
    33             mix = score[i];
    34         }
    35         /*若当前输入的分数比max大,则将此条信息记录为最高分者*/
    36         if(score[i]>max){
    37             strcpy(maxname,name);
    38             strcpy(maxsex,sex);
    39             strcpy(maxyear,year);
    40             max = score[i];
    41         }
    42     }
    43     printf("
    最高者:%s	性别:%s	年龄:%s
    ",maxname,maxsex,maxyear);
    44     printf("最低者:%s	性别:%s	年龄:%s
    ",mixname,mixsex,mixyear);
    45 } 
    复制代码

    代码二:结构体排序:

    复制代码
     1 #include<stdio.h>
     2 #include<string.h> 
     3 #include<stdlib.h>
     4 #include<math.h>
     5 #include<ctype.h>
     6 /*定义一个结构体*/
     7 typedef struct Stu{
     8     char name[100];
     9     char sex[10];
    10     int age;
    11     int score;
    12 }stu;
    13 /*    定义排序(回调)函数cmp: 
    14         返回类型必须是int;
    15         两个参数的类型必须都是const void *;
    16         如果是升序,那么就是如果a比b大返回一个正值,小则负值,相等返回0;
    17 */ 
    18 int cmp(const void *a,const void *b){
    19     /* *(stu*)a是因为:a是个void *类型,要先
    20     用(stu*)将它转成stu*类型,然后再用*取值,
    21     变成stu类型,才能比较大小。*/
    22     stu c=*(stu*)a;
    23     stu d=*(stu*)b;
    24     //按成绩升序排序 
    25     return c.score-d.score;
    26 }
    27 main(){
    28     int n;
    29     stu sz[100];
    30     scanf("%d",&n);
    31     for(int i=0;i<n;i++){
    32         scanf("%s %s %d %d",&sz[i].name,&sz[i].sex,&sz[i].age,&sz[i].score);
    33     }
    34     /*
    35     qsort函数参数: 
    36         1 待排序数组首地址;
    37         2 数组中待排序元素数量;
    38         3 各元素的占用空间大小,推荐使用sizeof(s[0])这样,特别是对结构体 ; 
    39         4 指向函数的指针,用于确定排序的顺序.
    40     注意:如果要对数组进行部分排序,比如对一个s[n]的数组排列其从s[i]开始的m个元素,只需要
    41 在第一个和第二个参数上进行一些修改:qsort(&s[i],m,sizeof(s[i]),cmp);
    42     */
    43     qsort(sz,n,sizeof(sz[0]),cmp);
    44     printf("
    按成绩升序为:
    
    ");
    45     for(int i=0;i<n;i++){
    46         printf("%s %s %d %d
    ",sz[i].name,sz[i].sex,sz[i].age,sz[i].score);
    47     }
    48 }
  • 相关阅读:
    搭建非域AlwaysOn win2016+SQL2016
    从0开始搭建SQL Server AlwaysOn 第四篇(配置异地机房节点)
    从0开始搭建SQL Server AlwaysOn 第二篇(配置故障转移集群)
    从0开始搭建SQL Server AlwaysOn 第三篇(配置AlwaysOn)
    从0开始搭建SQL Server AlwaysOn 第一篇(配置域控)
    四、基于Windows 2012配置SQL Server 2014 AlwaysOn
    三、安装SQLserver 2014(For AlwaysOn)
    二、 Windows 2012配置故障转移(For SQLServer 2014 AlwaysOn)
    Mybatis-SQL语句构建器类及日志
    Mybatis-JavaAPI
  • 原文地址:https://www.cnblogs.com/Firesun/p/10218596.html
Copyright © 2011-2022 走看看