函數指針
一、函數指針定義
//函數聲明:聲明我是一個什麼函數 //求兩個數的和 //函數的類型:int (int x,int y) //即:我是一個返回值為整型,有兩個整型參數的函數。 //函數名是 sum int sum(int x,int y);
函數指針定義p是變量,其他是類型(通常沒有形參a,b)
//函數指針類型 int (*)(int x,int y)
//描述:指向 返回值為 int 兩個int參數 的 指針類型
//函數指針變量: p
//初始值 : sum
printf("%d",p(5,8));
1 typedef int (*FP)(int x,int y);//此時此刻 FP 就是 int(*)(int x,int y)
二、函數回調
// printf("%d\n",getValue(10, 8, sum));
// printf("%d\n",getValue(10, 8, maxx));
int sum(int x,int y)
{
return x+y;
}
//求兩個數的最大值
int maxx(int x,int y)
{
int max=x>y?x:y;
return max;
}
int getValue(int x,int y,int (*p)(int a,int b))
{
return p(x,y);
}
三、動態排序
排序需求不定
BOOL compareByAge(Student s1,Student s2);// 比較兩個學生的年齡大小
BOOL compareByScore(Student s1,Student s2);//比較兩個學生的分數大小
BOOL compareByAttendance(Student s1,Student s2);//比較兩個學生的出勤率大小
BOOL compareByName(Student s1,Student s2);//比較兩個學生的姓名大小
typedef BOOL (*CFP)(Student s1,Student s2);;
void sortArray(Student stus[],int count,CFP P);//排序
void printstudent(Student stus[],int count);//打印學生數組
//實現函數
// 比較兩個學生的年齡大小
BOOL compareByAge(Student s1,Student s2)
{
return (s1.age>s2.age)?YES:NO;
}
//比較兩個學生的分數大小
BOOL compareByScore(Student s1,Student s2)
{
return (s1.score>s2.score)?YES:NO;
}
//比較兩個學生的出勤率大小
BOOL compareByAttendance(Student s1,Student s2)
{
return (s1.attendance>s2.attendance)?YES:NO;
}
//比較兩個學生的姓名大小
BOOL compareByName(Student s1,Student s2)
{
return strcmp(s1.name, s2.name)>0?YES:NO;
}
//按年齡排序
void sortArray(Student stus[],int count,CFP P)
{
for (int i = 0; i<count-1; i++) {
for (int j = 0; j<count-i-1; j++) {
if (P(stus[j],stus[j+1])) {
Student temp = stus[j];
stus[j] = stus[j+1];
stus[j+1] = temp;
}
}
}
}
//打印學生數組
void printstudent(Student stus[],int count)
{
for ( int i = 0; i<count; i++) {
printf("%s\t%.2f\t%d\t%.2f\n",stus[i].name,stus[i].score,stus[i].age,stus[i].attendance);
}
}
//主函數
Student stus[3]={{"lisi",89.5,18,1},{"zhangsan",92,20,0.5},{"wangwu",96,14,0.8}};
printstudent(stus, 3);
sortArray(stus, 3,compareByName);
printstudent(stus, 3);
四、函數返回值是函數指針
//GetValue.h中代碼
typedef int (*PFUN)(int x,int y);
//新類型 PFUN 舊類型int (*)(int x,int y)
//映射表是一個 結構體數組,為了建立映射表,我們先建立一個結構體.此結構體包含 一個字符串 和一個函數指針
struct NameFunctionPair {
char name[30]; //字符串
PFUN function; //函數指針
};
typedef struct NameFunctionPair NameFunctionPair;
//求2個數的最大值
int maxValue(int x,int y);
//求2個數的最小值
int minValue(int x,int y);
int sum(int x,int y);
int minus(int x,int y);
int multiple(int x,int y);
int divide(int x,int y);
int gcd(int x,int y);//最大公約數
int gbs(int x,int y);//最小公倍數
//根據字符串 獲取 函數名
PFUN functionOfName(char *name);
//三個參數 ,前2個是參與運算的數字,第三個參數用於查詢映射表
//返回值是 運算結束後的結果,如何運算,取決於第三個參數.
int getValue(int x,int y, char *name);
1 //GetValue.m中代碼
2 #import "GetValue.h"
3
4 NameFunctionPair nfps[] = {
5 {"max",maxValue},
6 {"min",minValue},
7 {"sum",sum},
8 {"minus",minus},
9 {"mul",multiple},
10 {"div",divide},
11 {"gcd",gcd},
12 {"gbs",gbs}
13 };
14
15 PFUN functionOfName(char *name)
16 {
17 for (int i = 0; i < sizeof(nfps)/sizeof(nfps[0]); i++) {
18 if (strcmp(name, nfps[i].name) == 0){
19 //如果映射表裡 有對應的 function, 返回這個 function
20 return nfps[i].function;
21 }
22 }
23 //如果沒找到,默認求最大值
24 return maxValue;
25 }
26 int getValue(int x,int y, char *name)
27 {
28 PFUN fun = functionOfName(name);//根據 name 獲取對應的函數
29 return fun(x,y);//使用選定的函數計算結果
30 }
31
32 int gbs(int x,int y)
33 {
34 return x * y / gcd(x, y);
35 }
36
37 int gcd(int x,int y)
38 {
39 while (x % y != 0) {
40 int temp = x % y;
41 x = y;
42 y = temp;
43 }
44 return y;
45 }
46
47 int divide(int x,int y)
48 {
49 return x/y;
50 }
51
52 int multiple(int x,int y)
53 {
54 return x * y;
55 }
56
57 int minus(int x,int y)
58 {
59 return x - y;
60 }
61
62 int sum(int x,int y)
63 {
64 return x + y;
65 }
66
67 int minValue(int x,int y)
68 {
69 return x < y ? x : y;
70 }
71
72 int maxValue(int x,int y)
73 {
74 return x > y ? x : y;
75 }
//主函數中代碼
#import <Foundation/Foundation.h>
#import "GetValue.h"
int main(int argc, const char * argv[])
{
/**
* 建立一張映射表,存儲 字符串-函數名 對兒
函數調用時,檢查給定的字符串是否在映射表中,如果在,取出對應的函數名
使用取出的函數名,調用函數,完成結果.
*/
printf("%d\n",getValue(8, 12, "mul"));
return 0;
}