我的iOS学习历程 - 第十一天

本文详细阐述了函数指针的概念、定义方法及应用实例,包括如何使用函数指针作为参数传递函数,实现结构体排序,以及通过回调函数进行特定操作。文章通过具体的代码示例展示了函数指针在解决实际编程问题时的强大能力。

今天主要说的是函数指针


#import<Foundation/Foundation.h>



//  两个数的和的函数两个数比较大小返回大的

int Sum(int a,int b);

int MaxValue (int a,int b);


//void printHello();


//  指针函数作为参数的函数

int getValue(int a,int b,int (*p)(int a,int b));

//***********************************************************************


typedefstruct Student{

    char name[50];

    int age;

    float score;

    int number;

}Student;


//  比较两个学生的分数

BOOL compareStudentByScore(Student *stu1,Student *stu2);

BOOL compareStudentByAge(Student *stu1,Student *stu2);

BOOL compareStudentByNumber(Student *stu1,Student *stu2);


//  给函数指针的类型起别名

//  新的名称起在 *的后面

typedefBOOL (*SORT)(Student *stu1,Student *stu2);


void Studentprintf(Student stu);

void AgeStudent(Student *stus,int count,SORT p);

void Allprintf(Student *stus,int count);

void ScoreStudent(Student *stus,int count);

void NumberStudent(Student *stus,int count);


//  新的排序方法参数是函数指针

void sortStudents(Student *stus,int count,SORT p);


//***********************************************************************


//  练习使用

void addStr(char *name);

void findStudentByScore(Student *stus,int count,void(*p)(char *name));


//**********************************************************************


//  声明一个结构体


typedefstruct SelectWork{

    char work[20];

    SORT x;

}SelectWork;


SORT getFunction(char *Name);





#import"Function.h"


int Sum(int a,int b){

    return a + b;

}


int MaxValue (int a,int b){

    return a > b ? a : b;

}

//void printHello(){

//    printf("xixi\n");

//}


//  指针函数作为了参数的函数

int getValue(int a,int b,int (*p)(int ,int )){

    

    int result = p(a,b);

    return  result;

}

//***********************************************************************

void Studentprintf(Student stu){

    printf("%s, %d, %.2f, %d\n", stu.name, stu.age, stu.score, stu.number);

}


void Allprintf(Student *stus,int count){

    for (int n =0; n < count; n++) {

        Studentprintf(stus[n]);

    }

}



void AgeStudent(Student *stus,int count,SORT p){

    for (int i =0; i < count -1; i++) {

        for (int j =0; j < count - i -1; j++) {

            if ((stus + j)->age > (stus + j +1)->age) {

                Student temp = *(stus + j);

                *(stus + j) = *(stus + j +1);

                *(stus + j +1) = temp;

            }

        }

    }

    Allprintf(stus, count);

}


void ScoreStudent(Student *stus,int count){

    for (int i =0; i < count -1; i++) {

        for (int j =0; j < count - i -1; j++) {

            if ((stus + j)->score > (stus + j +1)->score) {

                Student temp = *(stus + j);

                *(stus + j) = *(stus + j +1);

                *(stus + j +1) = temp;

            }

        }

    }Allprintf(stus, count);

}


void NumberStudent(Student *stus,int count){

    for (int i =0; i < count -1; i++) {

        for (int j =0; j < count - i -1; j++) {

            if ((stus + j)->number > (stus + j +1)->number) {

                Student temp = *(stus + j);

                *(stus + j) = *(stus + j +1);

                *(stus + j +1) = temp;

            }

        }

    }Allprintf(stus, count);

}


//***************************************************************



BOOL compareStudentByScore(Student *stu1,Student *stu2){

    return stu1->score > stu2->score;

}


BOOL compareStudentByAge(Student *stu1,Student *stu2){

    return stu1->age > stu2->age;

}

BOOL compareStudentByNumber(Student *stu1,Student *stu2){

    return stu1->number > stu2->number;

}



void sortStudents(Student *stus,int count,SORT 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;

            }

        }

    }Allprintf(stus, count);

    

}

//***********************************************************************

//  练习实现

void addStr(char *name){

    strcat(name,"高富帅");

}


void findStudentByScore(Student *stus,int count,void(*p)(char *name)){

    for (int n =0; n < count; n ++) {

        if ((stus + n)->score >90) {

            p((stus + n)->name);

            Studentprintf(stus[n]);

        }

    }

}

//***********************************************************************


SORT getFunction(char *Name){

    SORT x1 =compareStudentByAge;

    SORT x2 =compareStudentByScore;

    SORT x3 =compareStudentByNumber;

    SelectWork sew1 = {"age", x1};

    SelectWork sew2 = {"score", x2};

    SelectWork sew3 = {"number", x3};

    SelectWork sews[] = {sew1, sew2, sew3};

    for (int n =0; n <3; n++) {

        if (strcmp(Name, sews[n].work) ==0) {

            return sews[n].x;

        }

    }

    returnNULL;

}




#import<Foundation/Foundation.h>

#import"Function.h"

int main(int argc,constchar * argv[]) {


      函数指针:就是指向函数的指针

      函数的名字是一个地址是一个常量地址

      定义函数指针步骤 :

      1.把要指向的函数复制过来把函数名删了

      int Sum(int a,int b); ->int (int a,int b);

      2.把原来函数名的位置替换成 (*) (此时该式子就是函数指针的类型)

      int (*)(int a,int b);

      3.给函数指针起一个名字

      int (*函数指针名字)(int num1,int num2)

    

      注意:函数指针只能指向同类型的函数

    

      定义函数指针指向求最大值的函数


int (*p) (int a,int b) =NULL;

    p = MaxValue;

    // 可以用函数指针的名字代替函数名来调用函数

    printf("%d\n", p(3,5));


    void (*p1)() =NULL;

    p1 = printHello;

    // 无参数调用时也要加括号

    p1();

    

      定义函数指针

    定义两个函数,⼀个求最⼤值,⼀个求和,输⼊maxsum分别求3,5的最⼤值或和(提⽰,定义⼀个函数指针,根据输⼊内容指向不同函数,最后⼀次调⽤完成)

      定义函数指针的时候参数的名字可以省略

    

    int (*p) (int a,int b) =NULL;

    printf("请输入一个请求\n");

    char str1[20] = {0};

    char str2[] ="maxValue";

    char str3[] ="sum";

    scanf("%s",str1);

    if (strcmp(str1, str2) ==0) {

        // 执行maxValue

        p = MaxValue;

        printf("%d\n",p(3,5));

    }elseif (strcmp(str1, str3) ==0){

        // 执行Sum

        p = Sum;

        printf("%d\n",p(3,5));

    }else{

        printf("error\n");

    }

    

      调用函数指针如果在外面调用指针的话需要进行安全判断p !=NULL时才能进行调用

    

        int (*p) (int a,int b) =NULL;

        printf("请输入一个请求\n");

        char str1[20] = {0};

        char str2[] ="maxValue";

        char str3[] ="sum";

        scanf("%s",str1);

        if (strcmp(str1, str2) ==0) {

            // 执行maxValue

            p = MaxValue;

        }elseif (strcmp(str1, str3) ==0){

            // 执行Sum

            p = Sum   ;

            

        }else{

            printf("error\n");

            

        }

    if (p !=NULL) {

        printf("%d\n",getValue(3,5, p));

    }

    

    Student stu1 = {"Meiko",16,82.8,2};

    Student stu2 = {"Pawn",12,84.8,3};

    Student stu3 = {"Deft",19,91.8,1};

    Student stu4 = {"Koro",26,88.8,5};

    Student stu5 = {"ClearLove",18,98.8,4};

    Student stus[]= {stu1, stu2, stu3, stu4, stu5};

    AgeStudent(stus, 5);

    NumberStudent(stus,5);

    ScoreStudent(stus,5);


    SORT p =NULL;

        调用排序

    p = compareStudentByAge;

    sortStudents(stus,5, p);

      char arr[20] = {0};

    printf("请输入信息\n");

    scanf("%s",arr);

    if (strcmp(arr,"age") ==0) {

        p = compareStudentByAge;

    }elseif (strcmp(arr,"score") == 0){

        p = compareStudentByScore;

    }elseif (strcmp(arr,"number") == 0){

        p = compareStudentByNumber;

    }

    if (p !=NULL) {

        sortStudents(stus,5, p);

    }

      练习:写⼀函数查找成绩90分以上的学员,使⽤回调函数在姓名后加⾼富帅

    void (*p)(char *name) =NULL;

    p = addStr;

    findStudentByScore(stus,5, p);


    SORT P =NULL;

    while (1) {

          char arr[20] = {0};

        printf("age/score/number\n");

        scanf("%s",arr);

        P = getFunction(arr);

        

        break;

    }

    if (P !=NULL) {

        sortStudents(stus,5, P);

    }else{

        printf("false\n");

    }

return0;

}

转载于:https://www.cnblogs.com/888yf/p/4992722.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值