//
// MyFunction.h
// C12_函数指针
//
// Created by dllo on 15/7/14.
// Copyright (c) 2015年 Clare. All rights reserved.
//
#import <Foundation/Foundation.h>
找到两个数中的最大值
//int maxValue(int a, int b);
//
三个数的最大值
函数回调时,需要把要调用的函数作为第四个参数,然后针对地址进行调用
//int maxValue3(int a, int b, int c, int (*p)(int, int));
//
四个数的最大值
//
声明学生类型的结构体
//struct student{
// int stuAge;
// float stuScore;
// char stuSex;
// char stuName[50];
//};
//typedef struct student Student;
//
回调部分的函数
//Student changeName(Student stu);
//
用来查找90以上的学生,满足条件的调用改名的函数进行名的拼接
//void checkStudent(Student stu[], Student (*p)(Student));
//
动态排序
先写3个排序条件
按照年龄排序
//BOOL sortByAge(Student stu1, Student stu2);
分数
//BOOL sortByScore(Student stu1, Student stu2);
姓名
//BOOL sortByName(Student stu1, Student stu2);
//
//typedef BOOL (*FUN)(Student, Student);
用来排序的函数
//void bubbleSort(Student stu[], int count, FUN p);
// 函数的返回值是函数指针
int sumNum(int a, int b);
int mulNum(int a, int b);
int minNum(int a, int b);
typedef int (*PFUN)(int, int);
// 让功能名和地址能关联起来,为他们两个写一个结构体,一个存名,一个存对应功能的地址
struct nameFunction{
char name[20]; // 功能名
PFUN p; // 对应的功能地址
};
typedef struct nameFunction NameFunction;
// 通过功能的名,找到对应功能的函数地址,返回函数地址
PFUN checkFunctionName(NameFunction name[], char str[]);
//
// MyFunction.m
// C12_函数指针
//
// Created by dllo on 15/7/14.
// Copyright (c) 2015年 Clare. All rights reserved.
//
#import "MyFunction.h"
//int maxValue(int a, int b){
// return a > b ? a : b;
//}
//
//int maxValue3(int a, int b, int c, int (*p)(int, int)){
// // 函数的嵌套调用: int max = maxValue(a, b);
// int max = p(a, b);
// max = p(max , c);
// return max;
//}
//
//Student changeName(Student stu){
// // 字符串的拼接
// strcat(stu.stuName, "高富帅");
// return stu;
//}
//
//void checkStudent(Student stu[], Student (*p)(Student)){
// // 进行for遍历
// for (int i = 0; i < 4; i++) {
// if (stu[i].stuScore > 90) {
// stu[i] = p(stu[i]);
// printf("%s\n", stu[i].stuName);
// }
// }
//}
按照年龄排序
//BOOL sortByAge(Student stu1, Student stu2){
// return stu1.stuAge > stu2.stuAge ? YES : NO;
//}
分数
//BOOL sortByScore(Student stu1, Student stu2){
// return stu1.stuScore > stu2.stuScore ? YES : NO;
//}
姓名
//BOOL sortByName(Student stu1, Student stu2){
// return strcmp(stu1.stuName, stu2.stuName) > 0 ? YES : NO;
//}
//
//void bubbleSort(Student stu[], int count, FUN 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;
// }
// }
// }
//}
int sumNum(int a, int b){
return a + b;
}
int mulNum(int a, int b){
return a * b;
}
int minNum(int a, int b){
return a - b;
}
PFUN checkFunctionName(NameFunction name[], char str[]){
for (int i = 0; i < 3; i++) {
if (strcmp(name[i].name, str) == 0) {
return name[i].p;
}
}
return NULL;
}
//
// main.m
// C12_函数指针
//
// Created by dllo on 15/7/14.
// Copyright (c) 2015年 Clare. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "MyFunction.h"
定义结构体
//struct student{
// int stuAge;
// float stuScore;
// char stuSex;
// char stuName[20];
//};
//typedef struct student Student;
//
比较两个数的大小,并且返回结果
//int maxValue(int a , int b){
// return a > b ? a : b;
//}
//
//int sumNum(int x, int y){
// return x + y;
//}
//
//void test1(int b, float c){
//
//}
//
//void test2 (float c, int a){
//
//}
//
//
1 - n的和,在函数内打印结果
//void sum(int n){
// int count = 0;
// for (int i = 0; i <= n; i ++) {
// count += i;
// }
// printf("%d\n", count);
//}
int main(int argc, const char * argv[]) {
//
// Student stu1 = { 20 , 15, 'w', "zhangsan"};
// Student stu2 = {21, 16, 'm', "lisi"};
// Student stu3 = {22, 17, 'w', "wangwu"};
// Student *stu = calloc(3, sizeof(Student));
// stu[0] = stu1;
// stu[1] = stu2;
// stu[2] = stu3;
//
// printf("%p\n", maxValue);
// // 定义一个函数指针的变量
// int (*p) (int , int) = NULL;
// int *p =NULL;
//
// // 对函数指针进行赋值
// p = maxValue;
//
// // 打印地址所保存的地址
// printf("%p\n", p);
// int *p = NULL;
//
// void(*p1)(int, int) = NULL;
//
// // 通过函数指针进行函数的调用
// maxValue(10, 20);
// printf("%d\n", p(10, 20));
//
// p = sumNum;
// printf("%d\n", p(10, 20));
// // 函数指针在变量的位置要加上()
// void (*p)(int) = sum;
// p(8);
// // 键盘输入数字,要求实现功能,1是求最大值,2是求和,用函数指针来完成调用
// int enterNum = 0;
// scanf("%d", &enterNum);
//
// int (*p)(int, int) = NULL;
//
// switch (enterNum) {
// case 1:
// p = maxValue;
// printf("%d\n", p(29, 10));
// break;
// case 2:
// p =sumNum;
// printf("%d\n", p(29, 10));
// break;
// default:
// break;
// }
//
// 回调函数
// int result = maxValue3(3, 6, 1, maxValue);
// printf("%d\n", result);
// /// 写⼀函数查找成绩90分以上的学员,使⽤回调函数在姓名后加”⾼富帅”。
//
// Student stu1 = {18, 70.5, 'w', "liushanshan"};
// Student stu2 = {20, 60, 'm', "yanlin"};
// Student stu3 = {19, 91, 'w', "lisi"};
// Student stu4 = {21, 95, 'm', "zhangsan"};
// Student stu[4] = {stu1, stu2, stu3, stu4};
//
// Student newStu = changeName(stu1);
// printf("%s\n", newStu.stuName);
// 进行函数调用,第二个参数是用来回调的函数地址
// checkStudent(stu, changeName);
// printf("请输入排序方式,1是年龄,2是成绩,3是姓名\n");
// int enterNum = 0;
// scanf("%d", &enterNum);
// // 定义函数指针的变量
// FUN p = NULL;
// switch (enterNum) {
// case 1:
// p = sortByAge;
// break;
// case 2:
// p = sortByScore;
// break;
// case 3:
// p = sortByName;
// break;
// default:
// printf("输入错误!\n");
// break;
// }
// // 调用
// bubbleSort(stu, 4, p);
// for (int i = 0; i < 4; i++) {
// printf("%s\n", stu[i].stuName);
// }
//
NameFunction name1 = {"sum", sumNum};
NameFunction name2 = {"mul", mulNum};
NameFunction name3 = {"min", minNum};
NameFunction name[3] = {name1, name2, name3};
// 调用一下下
PFUN p = checkFunctionName(name, "min");
if (p == NULL) {
printf("功能不对\n");
} else {
printf("%d\n", p(3, 5));
}
return 0;
}