c_函数指针

来源:互联网 发布:淘宝发布宝贝怎么预览 编辑:程序博客网 时间:2024/05/29 16:32

(1)//

//  main.m

//  c_12函数指针

//

//  Created by dllo on 15/7/14.

//  Copyright (c) 2015 flg. All rights reserved.

//


#import <Foundation/Foundation.h>

#import "712.h"

//int maxValue (int a,int b){

//    return a>b?a:b;

//};

//int sumNum(int x,int y){

//    return x+y;

//

//};

////1-n的和函数内打印结果

//void sum(int n){

//     int add=0;

//    for (int i=1; i<=n; i++) {

//        add+=i;

//    }

//    printf("%d\n",add);

//};

int i;

void prt( )

{

   for(i=5;i<8;i++) printf("%c",'*');

    printf("");

}


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

//    printf("%p\n",maxValue);

//    int (*p) (int ,int )=NULL;

//    //对函数指针进行赋值

//    p=maxValue;

//    //打印地址所保存的地址

//    printf("%p\n",p);

//    //int *p=NULL;

//    //通过函数指针进行函数调用

//   // maxValue(10, 20);

//    printf("%d\n",p(10,20));//最大为20

//    p=sumNum;

//    printf("%d\n",p(10,20));//10+20=30

////函数指针在变量的位子要记得加上小括号

//    void (*q)(int)=sum;

//    q(8);


//键盘输入数字要求实现功能,1求最大值,2求和

//    int enterNum=0;

//    scanf("%d",&enterNum);

//    int(*w)(int ,int)=NULL;

//    switch (enterNum) {

//        case 1:

//            w=maxValue;

//            printf("%d\n",w(29,10));

//            break;

//            case 2:

//                   w=sumNum;

//                   printf("%d\n",w(29,10));

//            break;

//        default:

//            break;

//    }



//

//        for(int i=5;i<=8;i++) prt( );

//


//    char s1[40]="country",s2[20]="side";

//    int i=0,j=0;

//    while(s1[i]!='\0') i++;

//    while(s2[j]!='\0') s1[i++]=s2[j++];

//    s1[i]=0;

//    printf("%s ",s1);


//回调函数


    //int result=maxValue3(3, 6, 1, maxValue);


    //printf("%d\n",result);

//    student stu1={23,"anyichen",97.5,'m'};

//    student stu2={24,"ningzhiyuan",64.5,'m'};

//    student stu3={25,"wenshixuan",93,'m'};

//    student stu4={26,"anleyan",87,'w'};

//    student stu[4]={stu1,stu2,stu3,stu4};

    //student newstu=changeName(stu1);

    //printf("%s\n",newstu.stuName);

    //checkStudent(stu, changeName);


 for(i=5;i<=8;i++) prt( );










    ///////////动态排序

//    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 ",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));}

//



   return0;

}




(2)//

//  712.h

//  c_12函数指针

//

//  Created by dllo on 15/7/14.

//  Copyright (c) 2015 flg. All rights reserved.

//


#import <Foundation/Foundation.h>

#import "712.h"

//比较两个数的大小

int maxValue (int a,int b);

//找到两个数中的最大值

//三个数中的最大值

//函数回调时候,需要把要调用的函数作为第四个参数,然后针对地址进行调用

int maxValue3(int a,int b,int c,int(*p)(int ,int));

//四个数中的最大值


////写一函数查找成绩90分以上的学员,使⽤用回调函数在姓名后加

struct student{

   int stuAge;

   char stuName[20];

   float stuScore;

   char stuSex;

};

typedefstruct student student;

//回调部分函数

student changeName(student stu);

//用来查找90分以上的学生,满足条件的调用改名的函数进行名的拼接

void checkStudent(student stu[],student (*p)(student));

///////////动态排序

//先写三个排序条件

//按照年龄排序

BOOL sortByAge(student stu1,student stu2);

BOOL sortByScore(student stu1,student stu2);

BOOL sortByName(student stu1,student stu2);

typedefBOOL (*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);

//PFUN是新的类型

typedef int(*PFUN) (int,int);

//让功能名和地址能关联起来,为他们两个写一个结构体,一个存名,一个存对应功能的地址

struct nameFunction{

   char name[20];//功能名

    PFUN p;      //对应的功能地址

};

typedefstruct nameFunction NameFunction;

//通过功能函数,找到对应功能的函数地址,返回函数地址

PFUN checkFunctionName(NameFunction name[],char str[]);






(3)//

//  712.m

//  c_12函数指针

//

//  Created by dllo on 15/7/14.

//  Copyright (c) 2015 flg. All rights reserved.

//


#import "712.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;

}



0 0
原创粉丝点击