1. 程式人生 > 其它 >C語言 - 指標 05:利用函式指標實現動態排序

C語言 - 指標 05:利用函式指標實現動態排序

簡介

1 - 利用好 typedef 關鍵字,往往會使程式碼更為簡潔、易懂

2 - typedef 重新定義函式指標

(1)程式碼示例

 1 BOOL maxValue(int a,int b){
 2     
 3     return a>b?a:b;
 4 }
 5 
 6 // 定義函式指標:和 maxValue 函式型別保持一致
 7 typedef BOOL (*PFUN)(int a,int b);
 8 
 9 // 定義一個函式:返回型別的是 PFUN
10 PFUN test(){
11     
12     return maxValue;
13 }

main 函式中呼叫

1 PFUN p = test();
2 printf("最大值:%d\n",p(10,20)); // 列印 20

3 - 

4 - 程式碼示例:實現動態排序

  1 #include <stdio.h>
  2 
  3 // 使用列舉,定義一個 BOOL 變數
  4 typedef enum{
  5     false,
  6     true
  7 }BOOL;
  8 
  9 // 結構體:學生資訊
 10 typedef struct stu{
 11     
 12     char name[50];
 13     int age;
 14     float score;
15 16 }Student; 17 18 // 列印學生原有資訊 19 void printStudent(Student *s,int count){ 20 21 printf("=======================\n原學生資訊\n\n"); 22 for (int i = 0; i < count; i++) { 23 24 printf("%s %d %.2f\n",s[i].name,s[i].age,s[i].score); 25 }; 26 printf("
\n=======================\n"); 27 } 28 29 // 按年齡排序(方法已寫死,只能實現年齡排序這一功能) 30 // 造成的問題:如果需求改變,要求按照性別或分數排序呢?很明顯,這種寫法很不靈活 31 void sortByAge(Student *stu,int count){ 32 33 for (int i = 0; i < count -1; i++) { 34 for (int j = 0; j < count - 1 -i; j++) { 35 36 if (stu[j].age > stu[j+1].age) { 37 Student temStu = stu[j]; 38 stu[j] = stu[j+1]; 39 stu[j+1] = temStu; 40 } 41 } 42 } 43 44 printf("----------------\n按年齡升序\n\n"); 45 for (int i = 0; i < count; i++) { 46 47 printf("%s %d %.2f\n",stu[i].name,stu[i].age,stu[i].score); 48 }; 49 printf("\n----------------\n"); 50 51 } 52 53 //--------------------動態排序--------------------- 54 // 按照年齡排序 55 BOOL sortWithAge(Student s1,Student s2){ 56 return s1.age<s2.age; 57 } 58 59 // 按照分數排序 60 BOOL sortWithScore(Student s1,Student s2){ 61 return s1.score>s2.score; 62 } 63 64 // 按照姓名排序 65 BOOL sortWithName(Student s1,Student s2){ 66 67 return strcmp(s1.name,s2.name)>0 ? 1 : 0; 68 } 69 70 // 方式 1:不使用 typedef 關鍵字 71 // 此時函式指標作為引數,苦澀難懂,極為不便 72 void sortStudent01(Student *stu,int count,BOOL(*sortDemo)(Student s1,Student s2)){ 73 74 for (int i = 0; i < count -1; i++) { 75 for (int j = 0; j < count - 1 -i; j++) { 76 77 if (sortDemo(stu[j],stu[j+1])) { 78 Student temStu = stu[j]; 79 stu[j] = stu[j+1]; 80 stu[j+1] = temStu; 81 } 82 } 83 } 84 } 85 86 // 方式 2:使用 typedef 關鍵字 87 // 注意:定義函式指標要和呼叫的函式保持一致 88 typedef BOOL(*SORT)(Student s1,Student s2); 89 // 注意引數 SORT st 90 void sortStudent(Student *stu,int count,SORT st,char methodName[20]){ 91 92 for (int i = 0; i < count -1; i++) { 93 for (int j = 0; j < count - 1 -i; j++) { 94 95 if (st(stu[j],stu[j+1])) { 96 Student temStu = stu[j]; 97 stu[j] = stu[j+1]; 98 stu[j+1] = temStu; 99 } 100 } 101 } 102 103 printf("----------------\n按照 %s 進行排序\n\n",methodName); 104 for (int i = 0; i < count; i++) { 105 106 printf("%s %d %.2f\n",stu[i].name,stu[i].age,stu[i].score); 107 }; 108 printf("\n----------------\n"); 109 110 } 111 112 113 int main(int argc, const char * argv[]) { 114 115 Student stu[] = { 116 {"zhangsan",13,19.3}, 117 {"lisi",15,18.3}, 118 {"wangwu",11,12.9}, 119 {"zhaoliu",14,11.1} 120 }; 121 122 printStudent(stu, sizeof(stu)/sizeof(stu[0])); 123 sortByAge(stu,4); 124 125 126 // 動態排序:只需要輸入函式名,就可以實現排序目的 127 sortStudent(stu, 4, sortWithAge,"age"); 128 sortStudent(stu, 4, sortWithScore,"score"); 129 sortStudent(stu, 4, sortWithName,"name"); 130 131 return 0; 132 }