指针函数int max(int a, int b);
* 函数指针:指向一个函数的指针变量.'
* 函数的类型:返回值类型+参数
* 函数指针变量类型: int (*)(int x, int y)
* 指针变量的名字:max
* 初始值:NULL
* max 可以指向一个返回值是int型并且有两个int型的参数的函数.
应用:学生结构体按照姓名,年龄,分数升序排序
1.函数的声明
typedef struct student{
char name[20];
int age;
float score;
}Student;
typedef BOOL (*ALL)(Student, Student);
typedef struct nameFunctionPair{
char name[10];//名字
ALL p; //对应的函数地址
}NameFunctionPair;
//年龄
BOOL compareByAge(Student stu1 , Student stu2);
//成绩
BOOL compareByScore(Student stu1 , Student stu2);
//姓名
BOOL compareByName(Student stu1 , Student stu2);
//排序
void sortStudent(Student *p, int cont , ALL a);
//输出所有学生的信息
void outputStudent(Student *p, int count);
//从匹配表中匹配到对应的函数地址.
ALL matchFunctionByName(NameFunctionPair *q, int count ,char *name);//*p接收匹配表数组.count.接受匹配表数组的元素个数.name.接受用户输入的内容.<span style="color:#ff0000;">
</span>
//输出所有学生的信息
void outputStudent(Student *p ,int count) {
for (int i = 0; i < count; i++) {
printf("%s,%d,%.1f\n",p[i].name,p[i].age,p[i].score );
}
}
//年龄
BOOL compareByAge(Student stu1 , Student stu2){
return stu1.age > stu2.age;
}
//成绩
BOOL compareByScore(Student stu1 , Student stu2) {
return stu1.score > stu2.score;
}
//姓名
BOOL compareByName(Student stu1 , Student stu2) {
return strcmp(stu1.name, stu2.name) > 0;
}
//排序
void sortStudent(Student *p, int count ,ALL a) {
for (int i = 0; i < count - 1; i++) {
for (int j = 0; j < count - 1 - i; j++) {
if (a(p[j], p[j + 1])) {
Student temp = p[j];
p[j] = p[j + 1];
p[j + 1] = temp;
}
}
}
}
ALL matchFunctionByName(NameFunctionPair *q, int count ,char *name) {
for (int i = 0; i < count ; i++) {
if (strcmp(q[i].name, name) == 0) {
return q[i].p;
}
}
return NULL;//如果没有匹配成功返回NULL.
}
Student stu[5] = {
{"zhangs", 24, 78.9},
{"xiaom", 18, 45.8},
{"xiaozhen", 19,56.0},
{"heh", 20, 64.8},
{"frak", 19, 79.0}
};
NameFunctionPair name[3] = {
{"age", compareByAge},
{"score", compareByScore},
{"name", compareByName}
};
while (YES) {
char temp[10] = {0};
printf("name-姓名,age-年龄,socre-分数:\n");
scanf("%s",temp);
//根据用户输入内容匹配到对应的函数
ALL al = matchFunctionByName(name, 3, temp);
if (al != NULL) {//while(p == NULL)
//根据匹配到的内容进行排序.
sortStudent(stu, 5, al);
outputStudent(stu, 5);
} else if (al == NULL) {
printf("你输入的字符串不正确!请重新输入\n");
}
}
二.回调函数应用.
1.随机生成一个10个元素的数组,找到3的倍数,并将其值修改成0.(注意:修改数值使用回掉函数处理)
1.函数声明
typedef int (*ZERO)(int, int);
//随机10个元素的数组
void getTenNumber(int *p, const int count);
//遍历去除元素
void outputNumber(int *p, const int count);
//找到3的倍数,并且赋值为0
void findNumberToZero(int *p, const int count, ZERO z, int n);
//找n的倍数,
int copyZero(int number,int n);
2.方法实现
//随机10个元素的数组
void getTenNumber(int *p, const int count) {
for (int i = 0; i < count; i++) {
p[i] = arc4random() % (40 - 10 + 1) + 10;
}
}
//遍历取出元素的个数
void outputNumber(int *p, const int count) {
for (int i = 0; i < count; i++) {
printf("%d ",p[i]);
}
}
//找到3的倍数,并且赋值为0
void findNumberToZero(int *p, const int count ,ZERO z,int n) {
for (int i = 0; i < count; i++) {
if (z(p[i],n) == 0) {
p[i] = 0;
}
}
}
int copyZero(int number ,int n) {
if (n == 0) {
return NO;
}
return number % n;
}
3.函数调用
int arr[10] = {0};
getTenNumber(arr, 10);
outputNumber(arr, 10);
//4对应找到3的倍数,
findNumberToZero(arr, 10, copyZero, 4);
printf("\n");
outputNumber(arr, 10);