C语言-FunctionPointer
2015-08-10 20:36
459 查看
// main.m
// Student.h/Student.m
#import <Foundation/Foundation.h> #import "Student.h" typedef int (*PFUN)(int ,int );//用 typedef 将数据类型 int(*)(int ,int )重新命名为 PFUN #pragma mark - 函数声明 //求两数最大值 int maxValue(int , int ); //求两数之和 int sumValue(int , int ); //通过传入的函数做不同的计算 int getValue(int ,int ,PFUN );// 用 PFUN 来代替int(*)(int ,int )类型 #pragma mark - 函数定义 //求两数最大值 int maxValue(int a ,int b){ return a > b ? a : b; } //求两数之和 int sumValue(int a, int b){ return a + b; } //通过传入的函数做不同的计算 int getValue(int a,int b,PFUN p){ int result = p(a,b); return result; } int main(int argc, const char * argv[]) { //指针定义 //整型指针 // int a = 10; // int *p_int = NULL; // //定义了一个类型为 int *,变量名为 p_int, 初始值为 NULL 的一个指针变量 // p_int = &a; // //将变量 a 的地址赋值给 p_int, 那么 p_int 就指向变量a,p_int 就可以代替 a 进行取值的赋值运算 // *p_int = 20; //a =20; // // // //字符串 // char str[] = "aaa"; // char *p_str = NULL; // //定义了一个数据类型为 char *,变量名为 p_str, 初始值为 NULL 的一个指针变量 // p_str = str; // //将数组首地址赋值给指针变量 p_str,p_str 指向数组 str,p_str就可以代替数组名来使用 // // // // //结构体指针 // // Student stu1 = {"Zhangsan",'m',16,1001,99}; // // Student *p_stu = NULL; // //定义了一个类型为 student *, 变量名为 p_stu 的,初始值为 NULL 的一个指针变量 // p_stu = &stu1; // //将结构体 stu1的地址赋值给 p_stu,p_stu 就指向了 stu1,那么* p_stu 就可以代替 stu1进行取值赋值运算,另外 p_stu 和'->'结合也可以直接访问成员变量(p_stu->name); // //函数指针 // //函数声明部分: int maxVlue(int , int ); // int(*p_max)(int ,int ) = NULL; // //定义了一个数据类型为 int(*)(int, int),变量名为 p_max, 初值我NULL的一个指针变量; // p_max = maxValue; // //将函数的地址赋值给 p_max,p_max 就指向函数 maxvalue, 那么指针可以替换 maxvalue 函数名,进行函数调用 // int result = p_max(10,5); // printf("result = %d",result); // // //定义了一个指向函数 sumValue 的函数指针 // int(*p_sum)(int ,int ) = sumValue; // int result = p_sum(5,6); // printf("result = %d",result); // //如果函数的返回类型相同,参数个数和类型都相同,就可以使用同类型的函数指针指向 // int (*p_value)(int ,int ) = NULL; // p_value = maxValue; // p_value = sumValue; // //定义两个函数,一个求最大值,一个求和,输入max或sum分别求3,5的最大值或和.(提示,定义一个函数指针,根据输入内容指向不同函数,最后一次调⽤完成) // //定义一个能同时指向 maxValue 和 sumValue 的指针 // int (*p_fun)(int ,int )= NULL; // //存储字符串 // char ch[20] ={0}; // // //提示输入 // printf("请输入 maxValue 或者 sumValue 进行运算:\n"); // scanf("%s",ch); // if (memcmp(ch, "maxValue", 8) == 0 ) { // p_fun = sumValue; // }else if(memcmp(ch, "maxValue", 8) == 0 ){ // p_fun = maxValue; // }else{ // printf("输入错误"); // } // printf("%d",p_fun(3,5)); // // // //写一函数查找成绩90分以上的学员,使用回调函数在姓名后加”⾼富帅”。 //创建学生数组 Student stu[5] = {{"zhangsan",'m',01,18,60}, {"lisi",'w',02,16,99}, {"wangwu",'m',03,20,80}, {"zhaoliu",'?',1103,18,99}, {"zhouqi",'?',1105,44,69}}; //调用函数 findStuByScore(stu, 5, addString); //遍历打印 for (int i = 0; i < 5; i++) { printf("name = %s,sex = %c,age = %d,num = %d,score = %d\n",stu[i].name,stu[i].sex,stu[i].age,stu[i].num,stu[i].score); } sortStu(stu, 5, compareStuByAge); /* ============================结构体函数结合================================ */ //创建匹配表单 //将字符串"name"和函数 compareByName 绑定在一起 functionName list1 = {"name",compareStuByName}; functionName list2 = {"age",compareStuByAge}; functionName list3 = {"num",compareStuByNum }; functionName list4 = {"score",compareStuByScore }; functionName list[4] = {list1,list2,list3,list4}; SORT *p = NULL; //接受返回值的函数地址的 char str_1[20] = {0}; //用户输入字符串 while (1) { printf("请输入\n"); scanf("%s",str_1); p = getFunctionNameByStr(list,4,str_1); if(p == NULL) { continue; } sortStu(stu,5,p); for (int i = 0; i < 5; i++) { printf("name = %s,sex = %c,age = %d,num = %d,score = %d\n",stu[i].name,stu[i].sex,stu[i].age,stu[i].num,stu[i].score); } } return 0; }
// Student.h/Student.m
<.h> #import <Foundation/Foundation.h> typedef struct student{ char name[20]; char sex; int age; int num; int score; }Student; /* ============================================================ */ //重新定义函数名称 typedef BOOL(*SORT) (Student stu1,Student stu2); /* ============================================================ */ //写一函数查找成绩90分以上的学员,使用回调函数在姓名后加”⾼富帅”。 //学生名后添加字符串 void addString(char *name); //通过分数条件查找符合的学生 void findStuByScore(Student *stu,int count,void(*p)(char *)); //按年龄排序 void sortStuByAge(Student *stu, int count); //按姓名 void sortStuByName(Student *stu, int count); //按学号 void sortStuByNum(Student *stu, int count); //按分数 void sortStuByScore(Student *stu, int count); //比较两个学生的年龄 BOOL compareStuByAge(Student stu1,Student stu2); //比较两个学生的姓名 BOOL compareStuByName(Student stu1,Student stu2); //比较两个学生的学号 BOOL compareStuByNum(Student stu1,Student stu2); //比较两个学生的分数 BOOL compareStuByScore(Student stu1,Student stu2); /* ============================冒泡排序和两个学生比较的函数结合================================ */ //按年龄 void sortByAge(Student *stu, int count); //按姓名 void sortByName(Student *stu, int count); //按学号 void sortByNum(Student *stu, int count); //按分数 void sortByScore(Student *stu, int count); /* ==========================综合函数排序================================== */ //学生排序函数 void sortStu(Student *stu,int count ,SORT p); /* ============================结构体函数结合================================ */ typedef struct { char funName[20]; //函数名字符串; SORT function; //存储函数地址; }functionName; //根据字符串找到相应的函数 SORT getFunctionNameByStr(functionName *list,int count,char *str); <.m> #import "Student.h" //写一函数查找成绩90分以上的学员,使用回调函数在姓名后加”⾼富帅”。 //学生名后添加字符串 void addString(char *name){ strcat(name, "高富帅"); } //通过分数条件查找符合的学生 void findStuByScore(Student *stu,int count,void(*p)(char *)){ for (int i = 0; i < count ; i++) { if (stu[i].score > 90) { p(stu[i].name); } } } /* ============================两个学生比较函数================================ */ //比较两个学生的年龄 BOOL compareStuByAge(Student stu1,Student stu2){ return stu1.age > stu2.age; } //比较两个学生的姓名 BOOL compareStuByName(Student stu1,Student stu2){ return strcmp( stu1.name , stu2.name) > 0; } //比较两个学生的学号 BOOL compareStuByNum(Student stu1,Student stu2){ return stu1.num > stu2.num; } //比较两个学生的分数 BOOL compareStuByScore(Student stu1,Student stu2){ return stu1.score > stu2.score; } /* =============================综合函数排序=============================== */ //学生排序函数 void sortStu(Student *stu,int count ,SORT p){ for ( int i = 0; i < count - 1 ; i++) { for (int j = 0; j < count - 1 - i; j++) { if (p(stu[j] ,stu[j + 1])) { Student temp = *(stu + j); *(stu + j ) = *(stu + j + 1); *(stu + j + 1) = temp; } } } } /* ============================结构体函数结合================================ */ SORT getFunctionNameByStr( functionName *list,int count,char *str){ for (int i = 0; i < count ; i++) { if (strcmp(list[i].funName,str) == 0) { return list[i].function; } } printf("有误\n"); return NULL; }
相关文章推荐
- c与c++中struct区别
- C语言-DynamicMemory
- C++标准程序库笔记(2)
- [c语言]冒泡排序-------对折判断法
- [c语言]冒泡排序升级版------选择排序简单版
- C语言-Pointer(high)AndMacro
- C语言 sscanf,sprintf函数 使用总结
- C语言-Pointer
- C语言结构体的字节对齐实例【C语言笔试题】
- C语言-Struct
- C语言第一天
- C语言-Function
- C语言-Array(High)
- C语言-Array
- C语言-Loop
- C++学习笔记之erase
- 一道面试题细说C++类型转换
- C语言-Branch
- C++标准程序库笔记(1)
- c++ void,内存操作函数