蓝欧--lessonc12[函数指针]


#import <Foundation/Foundation.h>

//int(*)(int ,int) 原类型
//PFUN 新类型
typedef int(*PFUN)(int ,int);



typedef struct student{
   
char name[20];
   
char sex;
   
int age;
   
float score;
}STUDENT;


BOOL sortByAge(STUDENT stu, STUDENT stu1);
BOOL sortByAge(STUDENT stu, STUDENT stu1){
   
return stu.age > stu1.age;
}
BOOL sortByName(STUDENT stu, STUDENT stu1);
BOOL sortByName(STUDENT stu, STUDENT stu1){
   
return strcmp(stu.name, stu1.name) > 0;
}
BOOL sortByscore(STUDENT stu, STUDENT stu1);
BOOL sortByscore(STUDENT stu, STUDENT stu1){
   
return stu.score > stu1.score;
}
//把姓名后加高富帅(回调函数)
void sortByscoreGood(STUDENT * stu);
void sortByscoreGood(STUDENT * stu){
      
strcat((*stu).name, "高富帅");
}
//分数加5
void sortByscoreAdd(STUDENT * stu);
void sortByscoreAdd(STUDENT * stu){
    (*stu).
score += 5;
   
if ((*stu).score >= 100) {
        (*stu).
score = 100;
    }
}

//查找成绩 > 90
typedef void (*SO)(STUDENT * stu);

void checkStudentsScore(STUDENT * stu, int count,SO p);
void checkStudentsScore(STUDENT * stu, int count,SO p){
   
for (int i = 0; i < count; i++) {
       
if ((*(stu + i)).score > 90) {
           
//回调
            p(stu + i);
        }
    }

}

//回调函数的核心就是函数指针当作函数的参数
typedef BOOL (*SORTP)(STUDENT,STUDENT);
void sortOfStudent(STUDENT * stus,int count,SORTP p);
void sortOfStudent(STUDENT * stus,int count,SORTP p){
   
BOOL needNext = YES;
   
for (int i = 0; i < count - 1 && needNext == YES; i++) {
        needNext =
NO;
       
for (int j = 0; j < count - 1 - i; j++) {
           
if (p(stus[j],stus[j + 1])) {
                needNext =
YES;
               
STUDENT tem = (*(stus + j));
                (*(stus + j)) = (*(stus + j +
1));
                (*(stus + j +
1)) = tem;
            }
        }
    }
}

void printOf(STUDENT * stus,int count);
void printOf(STUDENT * stus,int count){
   
for (int i = 0; i < count; i++){
       
printf("%10s %c %d %.2f\n",(*(stus + i)).name,(*(stus + i)).sex,(*(stus + i)).age,(*(stus + i)).score);
    }
}


int maxValue(int a, int b);
int maxValue(int a, int b){
   
return a > b ? a : b;
}
int sumValue(int a, int  b);
int sumValue(int a, int  b){
   
return a + b;
}
int minus(int a, int b);
int minus(int a, int b){
   
return a - b;
}
int product(int a, int b);
int product(int a, int b){
return a * b;
}
//typedef int(*PFUN)(int ,int);
int printValue(int a, int b, PFUN p);
int printValue(int a, int b, int (*p)(int, int)){
   
//回调
   
return p(a, b);
}

void printHello();
void printHello()
{
   
printf("hello");
}



int main(int argc, const char * argv[]) {

//函数编译完成也会被存储,存储在内存的代码区
//函数名是函数在代码区的首地址
//    printf("%p",maxValue);
   
//0x100000f00[代码区]
   
   
//int (*)(int a, int b)指针的类型
   
//p 指针的变量名
//    int (*p)(int a, int b) = maxValue;
//    //使用函数指针调用函数
//    int result = p(10, 15);
//    printf("result = %d",result);
   
   
//指针变量p能够指向有一个整形返回值,两个整形参数的函数
//    int (*p)(int a, int b) = sumValue;
//    int result = p(5, 6);
//    printf("%d",result);
   
   
//空类型的指针
//定义一个可以指向上述函数的函数指针,并通过函数指针实现调用该函数。
//    void (*p)() = printHello;
//    p();
   
//定义两个函数,一个求最大值,一个求和,输入maxsum分别求3,5的最大值或和(提示,定义一个函数指针,根据输入内容指向不同函数,最后一次调用完成)
//    int a = 3, b = 5;
//    int (*p)(int a, int b) = NULL;
//    printf("请输入你要求的内容:");
//    char inputStr[20] = {0};
//    scanf("%s",inputStr);
//    if (strcmp(inputStr, "max") == 0) {
//        p = maxValue;
//    }else if(strcmp(inputStr, "sum") == 0){
//        p = sumValue;
//    }else {
//        printf("你输入的错误!");
//        //return 0;//提前结束
//        exit(0);//结束程序
//    }
//    int result  = p(a, b);
//    printf("%d",result);
   
   
//回调函数(CallBack)是指由调用方自己实现,
   
//设计模式[高内聚,低耦合]
//    int a = 5, b = 10;
//    int result = printValue(a, b, product);
//    printf("result = %d",result);
   
//    STUDENT stu[5] = {
//        {"zhangshan",'M',24,89.6},
//        {"yangzi",'W',23,81.22},
//        {"huangshang",'M',26,88.1},
//        {"shangwen",'W',22,79.52},
//        {"wenshang",'M',25,98.01}
//    };
////    sortOfStudent(stu, 5,sortByscore);
//    checkStudentsScore(stu, 5 , sortByscoreAdd);
//    printOf(stu,5);
   
   
return 0
;
}

你可能感兴趣的:(return,import)