iOSDay09C语言函数指针
2016-03-16 17:41
405 查看
本次主要学习和理解函数指针
函数类型:int (int, int)
函数指针的类型:int (*p)(int, int)
p是函数指针变量名
函数存放在代码区,函数名是函数存储空间的首地址
3> 通过函数指针调用函数
代码第7行
与通过函数名调用函数是一样的
练习:定义两个函数,一个求两个数的 最大值,一个求两个数的 和,从控制台输入maxValue或sum分别求3和5的最大值或和(提示:定义一个函数指针,根据输入内容指向不同函数,最后一次 调用完成)。
Func.h
Func.m
main.m
第20行代码是一个安全判断,是程序更加健壮。
练习:写一函数查找成绩90分以上的学员,使用回调函数在姓名后加”高富帅”。
提示:定义两个函数
1、void addStr(char *name);
2、void findStudentByScore(Student *stus, int count, void(*p)(char *))
Func.h
func.m
main.m
Function.h
Function.m
main.m
1.函数指针
void printValue(int number) { printf("number = %d\n", number); } int main(int argc, const char * argv[]) { void (*p1)(int) = NULL; p1 = printValue; p1(5); }
1> 定义
代码第5行函数类型:int (int, int)
函数指针的类型:int (*p)(int, int)
p是函数指针变量名
2> 给函数指针赋值(使用函数首地址)
代码第6行函数存放在代码区,函数名是函数存储空间的首地址
3> 通过函数指针调用函数
代码第7行与通过函数名调用函数是一样的
练习:定义两个函数,一个求两个数的 最大值,一个求两个数的 和,从控制台输入maxValue或sum分别求3和5的最大值或和(提示:定义一个函数指针,根据输入内容指向不同函数,最后一次 调用完成)。
Func.h
#import <Foundation/Foundation.h> int maxValue(int a, int b); int sum(int a, int b);
Func.m
#import "Func.h" int maxValue(int a, int b) { return a > b ? a : b; } int sum(int a, int b) { return a + b; }
main.m
#import <Foundation/Foundation.h> #import "Func.h" int main(int argc, const char * argv[]) { char str[20] = {0}; int (*p)(int, int) = NULL; printf("输入maxValue或sum:\n"); scanf("%s", str); if (strcmp("maxValue", str) == 0) { p = maxValue; printf("最大值:"); } else if (strcmp("sum", str) == 0) { p = sum; printf("和:"); } else { printf("输入有误,函数名不存在!\n"); } // 安全判断 if (p != NULL) { printf("%d\n", p(3, 5)); } return 0; }
第20行代码是一个安全判断,是程序更加健壮。
2、调用回调函数
回调函数就是 函数指针作为函数参数(一般是函数的最后一个参数)练习:写一函数查找成绩90分以上的学员,使用回调函数在姓名后加”高富帅”。
提示:定义两个函数
1、void addStr(char *name);
2、void findStudentByScore(Student *stus, int count, void(*p)(char *))
Func.h
#import <Foundation/Foundation.h> struct student { char name[20]; int age; int score; }; typedef struct student Student; void addStr(char *name); void findStudentByScore(Student *stus, int count, void(*p)(char *)); void printArray(Student *stus, int count);
func.m
void addStr(char *name) { strcat(name, "高富帅"); } void findStudentByScore(Student *stus, int count, void(*p)(char *)) { for (int i = 0; i < count; i++) { if (stus[i].score >= 90) { p(stus[i].name); } } } void printArray(Student *stus, int count) { for (int i = 0; i < count; i++) { printf("name = %s,age = %d,score = %d\n", stus[i].name, stus[i].age, stus[i].score); } }
main.m
#import <Foundation/Foundation.h> #import "Func.h" int main(int argc, const char * argv[]) { Student stu1 = {"张三", 18, 80}; Student stu2 = {"张四", 19, 90}; Student stu3 = {"张五", 17, 99}; Student stus[3] = {stu1, stu2, stu3}; findStudentByScore(stus, 3, addStr); printArray(stus, 3); return 0; }
3、动态排序
主要的思想是:使用回调函数,提高代码的复用性,提高代码的可修改性(当需求有变化时,可以快速简单的修改)Function.h
#import <Foundation/Foundation.h> struct student { char name[20]; // 姓名 int age; // 年龄 double score; // 成绩 int num; // 学号 }; typedef struct student Student; // 函数指针重定义,重定义的名字为 * 后面的名字 typedef BOOL (*Function)(Student, Student); // 打印数组元素 void printArray(Student *stu, int count); // 比较两个学生的年龄 BOOL compareStuAgeAsc(Student stu1, Student stu2); // 比较两个学生的姓名 BOOL compareStuNameAsc(Student stu1, Student stu2); // 比较两个学生的成绩 BOOL compareStuScore(Student stu1, Student stu2); // 比较两个学生的学号 BOOL compareStuNumAsc(Student stu1, Student stu2); // 排序函数 void sortStudents(Student stu[], int count, Function p);
Function.m
#import "Function.h" // 输出数组 void printArray(Student *stu, int count) { for (int i = 0; i < count; i++) { printf("name = %s, age = %d, score = %.2f, num = %d\n", stu[i].name, stu[i].age, stu[i].score, stu[i].num); } } // 比较两个学生的年龄 BOOL compareStuAgeAsc(Student stu1, Student stu2) { return stu1.age > stu2.age; } // 比较两个学生的姓名 BOOL compareStuNameAsc(Student stu1, Student stu2) { return strcmp(stu1.name, stu2.name) > 0; } // 比较两个学生的成绩 BOOL compareStuScore(Student stu1, Student stu2) { return stu1.score < stu2.score; } // 比较两个学生的学号 BOOL compareStuNumAsc(Student stu1, Student stu2) { return stu1.num > stu2.num; } // 排序函数 void sortStudents(Student stu[], int count, BOOL (*p)(Student, Student)) { 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; } } } }
main.m
#import <Foundation/Foundation.h> #import "Function.h" int main(int argc, const char * argv[]) { Student stu1 = {"zh", 45, 9.9, 315}; Student stu2 = {"s", 3, 100, 666}; Student stu3 = {"f-1", 18, -100, 38}; Student stu4 = {"Zboomsky", 19, 99, 17}; Student stu5 = {"Boos", 16, 150, 6}; Student stuArray[] = {stu1, stu2, stu3, stu4, stu5}; int count = sizeof(stuArray) / sizeof(Student); // 比较两个学生的年龄 sortStudents(stuArray, count, compareStuAgeAsc); printArray(stuArray, count); // 比较两个学生的姓名 sortStudents(stuArray, count, compareStuNameAsc); printArray(stuArray, count); // 比较两个学生的成绩 sortStudents(stuArray, count, compareStuScore); printArray(stuArray, count); // 比较两个学生的学号 sortStudents(stuArray, count, compareStuNumAsc); printArray(stuArray, count); return 0; }
相关文章推荐
- 【C++探索之旅】第一部分第十二课:指针一出,谁与争锋
- 【C++探索之旅】第一部分第十二课:指针一出,谁与争锋
- C语言求一组分数数列之和
- 含所有运算符重载的讲解(附实例)
- C语言第零篇:C语言学习
- 【C++解密】:C++不能直接给文件中写入数字怎么办
- 红黑树C++描述
- 红黑树C++描述
- 红黑树C++描述
- 红黑树C++描述
- c语言typedef的用法
- leetcode 337:House Robber III
- try c++11 foreach(1)
- 桶排序 c++ 实现
- 目录下面文件名的排序
- 信息学奥林匹克竞赛-C语言输出3个数中最大值
- C++ 标识符
- C++ 学习之路(6):类的组合
- [Sicily][广搜]1215. 脱离地牢
- C++之继承与派生(1)