1. 程式人生 > >C語言 · 運用結構體的排序方法 C語言 · 運用結構體的排序方法

C語言 · 運用結構體的排序方法 C語言 · 運用結構體的排序方法

AllSight  

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\n",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("\n最高者:%s\t性別:%s\t年齡:%s\n",maxname,maxsex,maxyear);
44     printf("最低者:%s\t性別:%s\t年齡:%s\n",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("\n按成績升序為:\n\n");
45     for(int i=0;i<n;i++){
46         printf("%s %s %d %d\n",sz[i].name,sz[i].sex,sz[i].age,sz[i].score);
47     }
48 }

  之前遇到排序只想著最原始的方法,諸如冒泡,選擇,快速排序等等,剛剛跟大牛學會了結構體的方法來排序,這樣的話以後再也不用怕成績統計、名次排序之類的題目了。

首先標頭檔案(基於大牛的方法,本人之後做題喜歡引入題目中常用的五個標頭檔案)

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\n",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("\n最高者:%s\t性別:%s\t年齡:%s\n",maxname,maxsex,maxyear);
44     printf("最低者:%s\t性別:%s\t年齡:%s\n",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("\n按成績升序為:\n\n");
45     for(int i=0;i<n;i++){
46         printf("%s %s %d %d\n",sz[i].name,sz[i].sex,sz[i].age,sz[i].score);
47     }
48 }