C语言 - 指针 1.5:使用函数指针实现动态地调用函数
作者:互联网
动态地调用函数
1 - 代码示例
① 使用函数指针实现函数的动态调用
// - main.c
1 #include <stdio.h> 2 // 定义 BOOL 变量 3 typedef enum{ 4 false, 5 true 6 }BOOL; 7 8 // 结构体:存储学生信息 9 typedef struct stu{ 10 11 char name[50]; 12 int age; 13 float score; 14 15 }Student; 16 17 18 // 打印学生原有信息 19 void printOriginalStudent(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"); 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 } 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 // 步骤二:定义同上函数类型的函数指针 71 // 方式 ①:不使用 typedef 关键字。该方式将函数指针作为参数,苦涩难懂,极为不便 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 // 调用函数返回结果 78 if (sortDemo(stu[j],stu[j+1])) { 79 Student temStu = stu[j]; 80 stu[j] = stu[j+1]; 81 stu[j+1] = temStu; 82 } 83 } 84 } 85 } 86 87 // 方式 ②:使用 typedef 关键字 88 // @methodName 无实际意义,仅调用时传入固定字符串,方便查看日志信息 89 typedef BOOL(*SORT)(Student s1,Student s2); 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 // 调用函数返回结果 96 if (st(stu[j],stu[j+1])) { 97 Student temStu = stu[j]; 98 stu[j] = stu[j+1]; 99 stu[j+1] = temStu; 100 } 101 } 102 } 103 104 printf("----------------\n按照 %s 进行排序\n\n",methodName); 105 for (int i = 0; i < count; i++) { 106 107 printf("%s %d %.2f\n",stu[i].name,stu[i].age,stu[i].score); 108 }; 109 } 110 111 int main(int argc, const char * argv[]) { 112 113 // -------------- 原有的学生信息 -------------- 114 Student stu[] = { 115 {"zhangsan",13,19.3}, 116 {"lisi",15,18.3}, 117 {"wangwu",11,12.9}, 118 {"zhaoliu",14,11.1} 119 }; 120 121 printOriginalStudent(stu, sizeof(stu)/sizeof(stu[0])); 122 sortByAge(stu,4); 123 124 //---------------- 排序 ---------------- 125 // 只需要输入函数名,就可以执行相应的函数 126 sortStudent(stu, 4, sortWithAge,"age"); 127 sortStudent(stu, 4, sortWithScore,"score"); 128 sortStudent(stu, 4, sortWithName,"name"); 129 130 return 0; 131 }
日志打印
② 根据字符串调用相应的函数
// - main.c
1 #include <stdio.h> 2 // ----------步骤 ①:确定方法并声明同类型的函数指针 3 // 最大值 4 int maxValue(int a,int b){ 5 return a > b? a : b; 6 } 7 8 // 最小值 9 int minValue(int a,int b){ 10 return a > b ? b : a; 11 } 12 13 // 搞一个错误提示 14 int errorMessage(int x,int y){ 15 printf("未找到将要调用的函数\n"); 16 return 0; 17 } 18 19 // 声明函数指针 20 typedef int (*PFUN)(int a,int b); 21 22 23 // -----------步骤 ② 24 // 搞一个结构体,包含字符串(函数名)、指向该函数的指针 25 typedef struct nameFunctionPair{ 26 char name[20]; 27 PFUN functionDemo; 28 }NFPDemo; 29 30 // 把方法放进结构体 31 NFPDemo nfp[2] = { 32 33 {"max",maxValue}, 34 {"min",minValue} 35 }; 36 37 // -------------步骤 ③:定义一个新的函数,返回值是函数指针 38 PFUN getFunctionWithName(char name[]){ 39 40 for (int i = 0; i <2; i ++) { 41 // 不匹配则按照失败处理 42 if (strcmp(nfp[i].name, name) == 0) { 43 return nfp[i].functionDemo; 44 } 45 } 46 47 // 返回错误信息 48 return errorMessage; 49 }; 50 51 int main(int argc, const char * argv[]) { 52 53 // 根据字符串调用相应的函数 54 55 PFUN pp = getFunctionWithName("max"); // 正常 56 PFUN QQ = getFunctionWithName("mmm"); // 异常 57 58 59 printf("%d\n%d\n", pp(30,40),QQ(300,400)); 60 return 0; 61 }
标签:count,1.5,name,int,++,调用函数,C语言,stu,Student 来源: https://www.cnblogs.com/self-epoch/p/15361220.html