函数指针

来源:互联网 发布:淘宝客微信采集软件 编辑:程序博客网 时间:2024/05/21 09:20

//

//  main.m

// 第三周第四天

//

//  Created by scjy on 15/10/29.

//  Copyright (c) 2015第三周. All rights reserved.

//


#import <Foundation/Foundation.h>

//函数声明

//int sum (int x,int y);

////函数定义

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

//

//    return  x+y;

//}


int minus (int x,int y);//minus

int minus (int x,int y){

    return x - y;

  }


int mult (int x,int y);

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

    return x * y;

}


int divi (int x,int y);

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

    return x / y;

}


int mod (int x,int y);

int mod (int x,int y){//取余

    return x % y;

}



void printHello(char *str){

    printf("printHello%s\n",str);


}


void s(){

    printf("kkkkk");

}




typedef int (*MAXV)(int x,int y);

//相当于把int (*)(int x,int y)定义成了MAXV

//typedef int (*MAXV)(int ,int );

//形参名可以省略。



//int maxValue(int x,int y);

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

//    return x > y ? x : y;

//}




//typedef int (*MAV)(int x,int y);

//int mValue(int x,int y);

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

//    return x > y ? x : y;

//}

//

//typedef int (*MV)(int x,int y);

//int AK(int x,int y);

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

//    return x > y ? x : y;

//}


typedef int  (*cd)(int x,int y);

int vcd(int x,int y);

int vcd(int x,int y){

    return x > y ? x : y;

}





int sum (int x,int y);

int sum (int x,int y){

    return  x+y;

}


int maxValue(int x,int y);

int maxValue(int x,int y){

    return x > y ? x : y;

}



int getValue (int x,int y,MAXV p){

    return p(x,y);

}



typedef struct stu{

    char name[20];

    int age;

    float score;

    int weight;

}Student;

//输出结构体数组中成员的信息

void printstuden(Student * s,int count);

void printstuden(Student * s,int count){

    for (int i = 0; i<5; i++) {

        printf("%s,%d,%.1f,%d\n",s[i].name,s[i].age,s[i].score,s[i].weight);

    }

}

//定义冒泡排序,给学生排序

void bubbleSort(Student *s,int count);

void bubbleSort(Student *s,int count){

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

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

            //相邻两个数作比较,交换

            if (s[j].age>s[j+1].age) {

                Student temp = s[j];

                s[j] = s[j+1];

                s[j+1]=temp;

            }

        }

    }}


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

    @autoreleasepool {

        // insert code here...

        NSLog(@"Hello, World!");

        

       //  C语言第十一讲,函数指针

        /*函数指针的定义:

        函数是存在哪个位置:函数名存在代码区,是有内存地址的

         */

        //上面定义的调用*************

//        int z =sum(123, 456);

//        printf("z = %d\n",z);

//        printf("&sum = %p\n",&sum);

        

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

        

//        int x =minus(123, 456);

//        printf("x = %d\n",x);

//        printf("&minus = %p\n",&minus);

//        

//        int c =mult(123, 456);

//        printf("c = %d\n",c);

//        printf("&mult = %p\n",&mult);

//        

//        int v =divi(123, 456);

//        printf("v = %d\n",v);

//        printf("&divi = %p\n",&divi);

//        

//        int b =mod(123, 456);

//        printf("b = %d\n",b);

//        printf("&mod = %p\n",&mod);

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

        

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

//        函数指针定义:

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

        

       //   函数指针定义:

        

        //类型:int (*)(int x,int y);

//        函数名:p

//        初值:NULL

       //数据类型(*指针变量名)(形式参数列表)

        // 比如:int (*a)(int a,int b)

        

        //1.

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

//        p=sum;

//        int y = p(7,9);

//        int x = p(5,7);

//        printf("y=%d\n",y);

//        printf("x=%d\n",x);

        

        /*

         //2.

         int (*p)(int x, int y);

         p = sum;

         int y = p(7, 9);

         printf("y = %d\n", y);

         

         //3.

         int (*p)(int x, int y) = sum;

         int y = p(7, 9);

         printf("y = %d\n", y);

         

         //4.

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

         p = sum;

         int y = p(7, 9);

         printf("y = %d\n", y);

         */

        

        // 练习1void printHello(char *str),定义一个可以指向上述函数的指针,并通过函数指针实现,调用该函数;

        

//        void (*p1)(char *str) = NULL;

//        p1=printHello;

//        p1("张混蛋");

//        

//        void (*p2)() = NULL;

//        //如何定义函数指针类型  void(*p)()

//        p2=s;

//        p2();

        

        

        //函数指针重指向

//        int (*p)(int x,int y);

//        

//         p=mult(5, 12);

//        int x = p(5,12);

//        printf("x = %d\n",x);

////

//        p=minus(5, 12);

//        int w = p(5,12);

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

////

//        p=divi(5, 12);

//        int v = p(5,12);

//        printf("v = %d\n",v);

//        

//        p=mod(5, 12);

//        int u = p(5,12);

//        printf("u = %d\n",u);

    

        

        

        

        //函数指针定义

//        typedef int (*PFUN)(int x, int y);//形参可以省略

//        PFUN p = NULL;

 //       类型 变量 初始值

        

//        MAXV p3= maxValue;//   int (*p3)(int x,int y) = NULL;p3 = maxValue;

//       int t = p3 (35,58);

//        printf("t=%d\n",t);

        

//        MAV p4= mValue;

//        int h = p4 (3558,3558);

//        printf("h=%d\n",h);

//        

//        

//        MV p5 = AK;

//        int g = p5(213,123);

//        printf("g=%d\n",g);

//        

//        cd p6 =vcd;

//        int f = p6(147,141);

//        printf("f = %d\n",f);

        

       // 函数指针赋值的 函数,应该和函数指针定义的 函数原型 保持一致

        

        

       //习题2:定义两个函数 ,一个求最大值,一个求和,输入maxValuesum分别求3,5的最大值或和(提示,定义一个函数指针 ,根据输入内容指向不同函数,最后一次调用完成)

       

//        typedef int (* zhizhen) (int x,int y);

//        zhizhen  w;

//        

//        

//        char  b[9];

//        

//        char  *w2=b;

//        scanf("%s",b);

//        

//        if (strcmp(w2, "max")==0) {

//            w=maxValue;

//            w(3,5);

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

//         }else if (strcmp(w2, "sum")==0) {

//            w=sum;

//            w(3,5);

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

//        }else printf("输入错误,请重新输入maxValuesum");

        

        //答案:

//        MAXV p5 = NULL;

//        printf("请输入maxValuesum");

//        char name[20];

//        scanf("%s",name);

//        if (strcmp(name, "maxValue")==0) {

//            p5 = maxValue;

//        }else if (strcmp(name, "sum")==0){

//            p5 = sum;}else(strcmp(name, "")!=0);

        

        

        

        

        

        

        

       //回调函数是一个函数,它由调用方自己实现,供被调方调用的函数。是一种非常灵活的设计模式

        

        MAXV p6  =NULL;

        p6 = mult;

        int h = getValue(3,5,p6);

        printf("h = %d\n",h);

        

        int ss = (getValue(3, 5,minus));

        printf("ss = %d\n",ss);

        

        int kk = (getValue(3, 5,sum));

        printf("kk = %d\n",kk);


        int ll = (getValue(3, 5,divi));

        printf("ll = %d\n",ll);


        

        

        

        //动态排序

        //定义学生结构体数组

        Student stus[] = {

            {"zhanghundan",21,90,650},

            {"zhaohundan",23,80,66},

            {"zhonghundan",24,70,65},

            {"qiaohundan",22,60,60},

        };

//

        printstuden(stus,sizeof(stus)/sizeof(stus[0]));

        printf("---------\n");

        bubbleSort(stus,sizeof(stus)/sizeof(stus[0]));

        printstuden(stus,sizeof(stus)/sizeof(stus[0]));

//        for (int i = 0; i<5; i++) {

//            printf("%s,%d,%f,%d"\n,,stus[i].name,stus[i].age,stu[i].score,stu[i].);

//        }

        

        

            }

    return 0;

}


0 0
原创粉丝点击