main.m 文件
9
#import <Foundation/Foundation.h>
10#import"Pointer.h" 11////////////////Lesson 11 函数指针 课堂笔记 与 习题练习////////////
12 13 14//函数指针:指向函数的指针叫做函数指针,用来存储函数的地址
15//函数名代表函数的入口地址
16 17 18//回调函数; 函数指针变量,存储对应的函数的地址
19//给函数指针类型,取一个新的名字 typedef,修饰的新名字,不是变量
20//typedef int (*PFun)(int, int);//起得新的名字是PFUN,此时PFUN的作用还是函数指针的类型,不是变量
21//int getValue(int a, int b, PFun p);
22//int getValue(int a, int b, PFun p){
23// return p(a, b);
24//} 25 26 27///////////////////下午课程,动态排序 28/* 29struct student{
30 char name[20];
31 int age;
32 int number;
33 float score;
34 float weight;
35 float height;
36};
37typedef struct student Stu;
38 39 40///所有排序函数, if语句中的比较条件是不一样的,其余部分代码都是一样的;把相同的内容,取出来放在一个函数中,只写一次,把 不同的内容写成一个一个的函数。
41 42 43////利用回调函数,调取排序方式
44//bool arrayWay(Stu *stu1, Stu *stu2, bool (*p)(Stu *, Stu *)){
45// return p(stu1, stu2) ? YES : NO;
46//}
47 48//为函数指针变量,重命名
49typedef bool(*SORT) (Stu *, Stu*);
50//动态排序, 函数指针变量作为参数,存储排序的函数地址
51void sortArrayStudent(Stu *stu, int n, SORT p);
52void sortArrayStudent(Stu *stu, int n, SORT p){
53 for (int i = 0; i < n - 1; i++) {
54 for (int j = 0; j < n - 1 - i; j++) {
55 //函数指针,调用对应的函数
56 if (p(stu + j, stu + j + 1)) {
57 Stu temp = *(stu + j);
58 *(stu + j) = *(stu + j + 1);
59 *(stu + j + 1) = temp;
60 }
61 }
62 }
63}
64 65//成绩升序 升级版
66bool scoreSortAscUpdate(Stu *stu1, Stu *stu2){
67 return stu1->score > stu2->score;
68}
69//姓名升序
70bool nameSortAscUpdate(Stu *stu1, Stu *stu2){
71 return strcmp(stu1->name, stu2->name) > 0;
72}
73//输出学生信息
74void outputAllStudentInfo(Stu *stu, int n){
75 for (int i = 0; i < n ; i++) {
76 printf("name = %s,num = %d, age = %d, score = %.2f, weight = %.2f, height = %.2f n", (stu + i)->name,(stu + i)->number, (stu + i)->age, (stu + i)->score, (stu + i)->weight, (stu + i)->height);
77 }
78}
79 80//创建匹配结构体
81struct mapNameFunction{
82 char name[10];//存储名字
83 SORT functon;//存储对应的函数的地址
84};
85typedef struct mapNameFunction MAP;
86 87//根据输入的字符串,从匹配表中,匹配到对应的函数地址
88SORT matchFunctionByName(MAP *m, char *temp, int n){
89 for (int i = 0; i < n; i++) {
90 if (strcmp((m + i)->name, temp) == 0) {
91 return (m + i)->functon;
92 }
93 }
94 return NULL;
95}*/ 96 97 98////////////////////////////美丽分割线/////////////////////////////// 99//练习最大公约,最小公倍,余数, 最大,最小, 和, 差,积100 typedef int (*CALC) (int, int);
101int getValue(int a, int b, CALC p){
102return p(a, b);
103}
104105//因为Pointer.h中已经存在maxValue,所有在这里就写maxValue1106int maxValue1(int a ,int b){
107return a > b ? a : b;
108}
109int minValue1(int a, int b){
110return a > b ? b : a;
111}
112int getGcd(int a, int b){
113// int r = a % b;
114// while(r){
115// a = b;
116// b = r;
117// r = a % b;
118// }119return b == 0 ? a : getGcd(b, a % b);
120}
121int getMod(int a, int b){
122return a % b;
123}
124 typedef struct MapOperator{
125char oper;
126 CALC calFunction;
127}MAPOPER;
128129 CALC matchOperator(MAPOPER *m, int n, char c){
130for (int i = 0; i < n; i++) {
131if ((m + i)->oper == c) {
132return (m + i)->calFunction;
133 }
134 }
135return NULL;
136}
137138int main(int argc, constchar * argv[]) {
139140////上午课程141/*142 //
143 // int max = maxValue(15, 20);
144 // printf("max = %dn", max);
145146 //定义一个函数指针指向maxValue函数
147 //类型:int (*ptr)(int x, int y);
148149 // //1.定义的ptr,可以指向返回值类型为int,并且有两个整形参数的函数
150 // int (*ptr)(int a, int b) = NULL;
151 // //2.指针变量指向maxValue函数
152 // ptr = maxValue;
153 // //3.通过指针变量实现函数的调用
154 // int max = ptr(4, 5);
155 // printf("max = %dn", max);
156157 // void (*plove)(int n) = NULL;
158 // plove = sayLove;
159 // plove(5);
160 //
161162 //
163 // int (*pMoney)() = NULL; //指向一个无参数,有一个返回值的函数
164 // pMoney = getMoney;
165 // int money = pMoney();
166 // printf("getmoney = %dn ", money);
167 //
168 // char* (*pstr)() = NULL;
169 // pstr = getString;
170 // char *str = pstr();
171 // printf("%sn", str);
172 //
173 // void (*pHello)() = NULL;
174 // pHello = sayHello;
175 // pHello();
176 //
177 // char *(*pstr1)(char *s) = NULL;
178 // pstr1 = getStr;
179 // char *str1 = pstr1("Welcome to Lanou!");
180 // printf("%sn", str1);
181 //
182 //
183 // void (*pSwap)(int *a, int *b) = NULL;
184 // pSwap = swap;
185 // int aa = 5, bb = 10;
186 // pSwap(&aa, &bb);
187 // printf("%dn", aa);
188189190 //定义两个函数,?一个求最?大值,?一个求和,输?入max或sum分别求3,5的 最?大值或和(提?示,定义?一个函数指针,根据输?入内容指向不同函数,最后 ?一次调?用完成)。
191 // char temp[10] = {0};
192 // printf("Input max or sum or min:");
193 // scanf("%s", temp);
194 // int (*ptemp)(int a, int b) = NULL;
195 // strcmp("max", temp) == 0 ? (ptemp = maxValue) : strcmp("sum", temp) == 0 ? (ptemp = sumValue) : strcmp("min", temp) == 0?(ptemp = minValue):(ptemp = minValue);
196 // printf("%s = %d ",temp, ptemp(3, 5));
197198199 // char c = 0;
200 // printf("Input a or b or c:");
201 // scanf("%c", &c);
202 // int (*ptemp)(int, int) = NULL;
203 //
204 // switch (c) {
205 // case ‘a‘: //case后面只能是常量(整型常量) 或者常量(整型常量)表达式
206 // ptemp = sumValue;
207 // break;
208 // case ‘b‘:
209 // ptemp = maxValue;
210 // break;
211 // case ‘c‘ :
212 // ptemp = minValue;
213 // break;
214 // default:
215 // break;
216 // }
217 // printf("%c = %d ",c, ptemp(3, 5));
218219220221 //函数调用,回调函数
222 // int value = getValue(3, 5, maxValue);
223 // printf("value = %dn", value);
224225*/226227////////////////////////////美丽分割线///////////////////////////////
228229///////////下午课程230//DynamicSort 动态排序231/*232// Stu students[5] = {
233// {"Ashen0", 24, 100, 89, 55, 180},
234// {"zAshen1", 45, 101, 90, 60, 173},
235// {"Ashen2", 32, 103, 60, 70, 165},
236// {"aAshen3", 16, 102, 100, 65, 178},
237// {"Ashen4", 99, 104, 54, 80, 173}
238// };
239//
240// //创建函数匹配表
241// MAP maps[2] ={
242// {"score",scoreSortAscUpdate},
243// {"name", nameSortAscUpdate}
244// };
245// //根据用户在控制台的输入,选择对应的排序方式
246// char temp[10] = {0};//存储输入的字符串
247// printf("输入排序方式(e.g. 成绩(score), 年龄(age), 姓名(name), 学号(num)):");
248// scanf("%s",temp);
249// //匹配获取对应的函数地址
250// SORT sortTemp =matchFunctionByName(maps, temp, 2);
251//
252// while (sortTemp == NULL) { //如果sortTemp == NULL ,说明没有匹配到对应的函数地址,继续输入匹配
253// printf("请重新输入排序方式(e.g. 成绩(score), 年龄(age), 姓名(name), 学号(num)):");
254// scanf("%s",temp);
255// sortTemp =matchFunctionByName(maps, temp, 2);
256// }
257//
258// sortArrayStudent(students, 5, sortTemp);//调用动态排序函数, 传入排序方式
259// outputAllStudentInfo(students, 5);
260//
261262*/263////////////////////////////美丽分割线///////////////////////////////264265266267 MAPOPER mapopers[4] = {
268 {‘b‘, maxValue1},
269 {‘s‘, minValue1},
270 {‘g‘, getGcd},
271 {‘m‘, getMod}
272 };
273274char c = 0;
275 printf("Input operator:e.g. 最大值(b), 最小值(s),最大公约数(g),余数(m):");
276 scanf("%c",&c);
277278 CALC temp = matchOperator(mapopers, 4, c);
279while (temp == NULL) {
280 printf("Input operator:(e.g. 最大值(b), 最小值(s),最大公约数(g),余数(m):");
281 rewind(stdin); //每次输入之前清空键盘缓冲区282 scanf("%c", &c);
283 temp = matchOperator(mapopers, 4, c);
284 }
285 printf("value = %d", getValue(3, 4, temp));
286287return0;
288 }
Pointer.h
1
//
2
//
Pointer.h
3
//
LessonC11FunctionPointer
4
//
5
//
Created by lanouhn on 15/3/31.
6
//
Copyright (c) 2015年 Ashen. All rights reserved.
7
//
8
9
#import <Foundation/Foundation.h>
1011//定义两个函数,?一个求最?大值,?一个求和,输?入max或sum分别求3,5的 最?大值或和(提?示,定义?一个函数指针,根据输?入内容指向不同函数,最后 ?一次调?用完成)。12int maxValue(int a, int b);
13int sumValue(int a, int b);
14int minValue(int a, int b);
1516171819void sayLove(int number);
2021//Practice练习22int getMoney();
2324char *getString();
2526void sayHello();
2728char *getStr(char *s);
2930void swap(int *a, int *b);
Pointer.m文件
//
//
Pointer.m
//
LessonC11FunctionPointer
//
//
Created by lanouhn on 15/3/31.
//
Copyright (c) 2015年 Ashen. All rights reserved.
//
#import
"
Pointer.h
"
int maxValue(int a, int b){
return a > b ? a : b;
}
int sumValue(int a, int b){
return a + b;
}
int minValue(int a, int b){
return a > b ? b: a;
}
void sayLove(int number){
while (number) {
printf("Love + %dn", number);
number--;
}
}
//Practice练习int getMoney(){
return100;
}
char *getString(){
return"I love you";
}
void sayHello(){
printf("Hello, Ashenn");
}
char *getStr(char *s){
return s;
}
void swap(int *a, int *b){
*a ^= *b^= *a^= *b;
}
- 课后习题
1
//
2
//
main.m
3
//
HomeworkFunctionPointer
4
//
5
//
Created by lanouhn on 15/3/22.
6
//
Copyright (c) 2015年 Ashen. All rights reserved.
7
//
8
9
#import <Foundation/Foundation.h>
10int maxValue(int a, int b);
11int sumValue(int a, int b);
12void printHello();
13 14 typedef struct student{
15int score;
16char name[20];
17int age;
18int attend;
19} Stu;
20 21//函数声明 22void getName(Stu *students, int n);
23void getValue(int n, void (*p)(Stu *,int));
24 25 26 Stu students[10] = {};
27 28 29int main(int argc, constchar * argv[]) {
30// @autoreleasepool {
31//// insert code here...
32// NSLog(@"Hello, World!");
33// } 34//////////////////Lesson11 函数指针 练习//////////////////
35 36//带参数的函数指针
37// int (*p)(int a,int b)=NULL;
38// p = maxValue;
39// printf("%d ", p(3,4));
40 41//不带参数的函数指针
42// void (*b)() = NULL;
43// b = printHello;
44// b();
45// 2.定义两个函数,?一个求最?大值,?一个求和,输?入max或sum分别求3,5的 最?大值或和(提?示,定义?一个函数指针,根据输?入内容指向不同函数,最后 ?一次调?用完成)。
46// int (*p)(int a, int b) = NULL;
47// 48// printf("max 或 sum :");
49// char choose[4] = {0};
50// scanf("%s",choose);
51// if (strcmp(choose, "max") == 0) {
52// p = maxValue;
53// }else if(strcmp(choose, "sum") == 0){
54// p = sumValue;
55// }else {
56// printf("输入有误!n");
57// }
58// printf("%s = %d n", choose, p(3,5));
59 60 61// 3 写?一函数查找成绩90分以上的学员,使?用回调函数在姓名后加”?富 帅”。 62 63char cname[10][20] = {"张三", "李四", "王二", "EZ", "卡特", "小Z", "DanDan", "BenBen", "Ying", "Ashen"};
64for (int i = 0; i < 10; i++) {
65 strcpy(students[i].name, cname[i]);
66 students[i].score = arc4random() % (100 - 40 + 1) + 40;
67 printf("%d ", students[i].score);
68 }
69 70 printf("n");
71 getValue(10,getName);
72 73 74// 4.有30个学?需要排序 1.按姓名排 2.按成绩排 3.按年龄排
75// .... ?一周后新需求,按出勤率排?
76// int sortName(Stu *stu, int j);
77// int sortAge(Stu *stu, int j);
78// void sortArray(Stu *stu, int count, int(*sort)(Stu *, int));
79// char cname[10][10] = {"zhang", "lisi", "wanger", "ez", "kate", "ashen", "danDan", "benBen", "ying", "zhong"};
80// for (int i = 0; i < 10; i++) {
81// strcpy(students[i].name, cname[i]);
82// students[i].score = arc4random() % (100 - 40 + 1) + 40;
83// students[i].age = arc4random() % (25 - 15 + 1) + 15;
84// students[i].attend = arc4random() % 100;
85// printf("%s ", students[i].name);
86// }
87// printf("n");
88// sortArray(students, 10, sortName);
89// for (int i = 0; i < 10; i++) {
90// printf("%s ", students[i].name);
91// }
92// 93 94// 5 对list增加元素,sum求和,mul求乘积. 95 96 97 98 99100101////////////////////Lesson11 函数指针, 作业///////////102// 1.(***)随机生成一个 10 个元素的数组,找到 3 的倍数,并将其值修改成 0.(注意:修改数值使用回掉函数处理)
103// int editNum(int num);
104// int getEditNum(int num, int (*p)(int));
105// int ranArr[10] = {0};
106// for (int i = 0; i < 10; i++) {
107// ranArr[i] = arc4random() % 100 + 1;
108// printf("%d ", ranArr[i]);
109// }
110// printf("n");
111// for (int i = 0; i < 10; i++) {
112// ranArr[i] = getEditNum(ranArr[i], editNum);
113// printf("%d ", ranArr[i]);
114// }
115// 2. (***)有两个10个元素的数组,分别为A和B,编程实现相同位置的元素, 如果 B 的元素小于 A 的元素进行数值交换:(使用回调函数实现)116void swap(int *a, int *b);
117void getSwap(int *a, int *b, void (*p)(int *,int *));
118int A[10] = {9, 19, 11, 96, 26, 18, 2, 70, 64, 17, };
119int B[10] = {19, 10, 10, 32, 90, 1, 23, 40, 23, 20};
120for (int i = 0; i < 10; i++) {
121 getSwap(&A[i], &B[i], swap);
122 }
123for (int i = 0; i < 10; i++) {
124 printf("%d ",A[i]);
125 }
126 printf("n");
127for (int i = 0; i < 10; i++) {
128 printf("%d ",B[i]);
129 }
130return0;
131}
132///////////////// 练习1 和练习2133int maxValue(int a, int b){
134return a > b ? a : b;
135}
136int sumValue(int a, int b){
137return a + b;
138}
139void printHello()
140{
141 printf("Hellon");
142}
143144145///////////////////////练习3146void getName(Stu *students, int n){
147for (int i = 0 ; i < n; i++) {
148if (students[i].score > 90) {
149 strcat(students[i].name, "[高富帅]");
150 printf("%s: %d n", students[i].name, students[i].score);
151 }else{
152 printf("%s: %d n", students[i].name, students[i].score);
153 }
154 }
155}
156//回调函数157void getValue(int n, void (*p)(Stu *,int)){
158 p(students,n);
159}
160////////////////练习4161void sortArray(Stu *stu, int count, int(*sort)(Stu *, int)){
162for (int i = 0; i < count; i++) {
163for (int j = 0; j < 10 - 1 -i; j++) {
164if (sort(stu, j)) {
165 Stu temp = stu[j];
166 stu[j] = stu[j + 1];
167 stu[j + 1] = temp;
168 }
169 }
170 }
171}
172173//174//int sortName(Stu *stu, int j){
175// return strcmp(stu[j].name, stu[j + 1].name) > 0 ? 1 : 0;
176//}
177//int sortAge(Stu *stu, int j){
178// return stu[j].age> stu[j + 1].age ? 1 : 0;
179//}180////////////////////////作业1181int editNum(int num){
182if (num % 3 == 0) {
183 num = 0;
184 }
185return num;
186}
187int getEditNum(int num, int (*p)(int)){
188return p(num);
189}
190/////////////////////////作业2191//如果 B 的元素小于 A 的元素进行数值交换:192void swap(int *a, int *b){
193if (*b < *a) {
194int temp = *b;
195 *b = *a;
196 *a = temp;
197 }
198}
199void getSwap(int *a, int *b, void (*p)(int *,int *)){
200 p(a,b);
201 }
原文:http://www.cnblogs.com/zhaoashen/p/4387307.html
【说明】:本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:254677821)!