码迷,mamicode.com
首页 > 移动开发 > 详细

iOS学习笔记---c语言第十一天

时间:2014-06-28 13:03:58      阅读:295      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   get   使用   strong   

函数指针

一、函数指针定义

 

//函数声明:声明我是一个什么函数
//求两个数的和
//函数的类型:int (int x,int y)
//即:我是一个返回值为整型,有两个整型参数的函数。
//函数名是 sum
int sum(int x,int y);

 函数指针定义p是变量,其他是类型(通常没有形参a,b)

//函数指针类型  int (*)(int x,int y)
    //描述:指向 返回值为 int 两个int参数 的 指针类型
    //函数指针变量: p
    //初始值 : sum
    printf("%d",p(5,8));

 

1 typedef int (*FP)(int x,int y);//此时此刻 FP 就是 int(*)(int x,int y)

 二、函数回调

 

//    printf("%d\n",getValue(10, 8, sum));
//    printf("%d\n",getValue(10, 8, maxx));

int sum(int x,int y)
{
    return x+y;
}

//求两个数的最大值
int maxx(int x,int y)
{
    int max=x>y?x:y;
    return max;
}
int getValue(int x,int y,int (*p)(int a,int b))
{
    return p(x,y);
}

三、动态排序

排序需求不定

BOOL compareByAge(Student s1,Student s2);// 比较两个学生的年龄大小
BOOL compareByScore(Student s1,Student s2);//比较两个学生的分数大小
BOOL compareByAttendance(Student s1,Student s2);//比较两个学生的出勤率大小
BOOL compareByName(Student s1,Student s2);//比较两个学生的姓名大小

typedef BOOL (*CFP)(Student s1,Student s2);;

void sortArray(Student stus[],int count,CFP P);//排序
void printstudent(Student stus[],int count);//打印学生数组

//实现函数
// 比较两个学生的年龄大小
BOOL compareByAge(Student s1,Student s2)
{
    return (s1.age>s2.age)?YES:NO;
}
//比较两个学生的分数大小
BOOL compareByScore(Student s1,Student s2)
{
    return (s1.score>s2.score)?YES:NO;
}
//比较两个学生的出勤率大小
BOOL compareByAttendance(Student s1,Student s2)
{
    return (s1.attendance>s2.attendance)?YES:NO;
}
//比较两个学生的姓名大小
BOOL compareByName(Student s1,Student s2)
{
    return strcmp(s1.name, s2.name)>0?YES:NO;
}

//按年龄排序
void sortArray(Student stus[],int count,CFP 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;
            }
        }
    }
}

//打印学生数组
void printstudent(Student stus[],int count)
{
    for ( int i = 0; i<count; i++) {
        printf("%s\t%.2f\t%d\t%.2f\n",stus[i].name,stus[i].score,stus[i].age,stus[i].attendance);
    }
}


//主函数

  Student stus[3]={{"lisi",89.5,18,1},{"zhangsan",92,20,0.5},{"wangwu",96,14,0.8}};

    printstudent(stus, 3);
    sortArray(stus, 3,compareByName);
    printstudent(stus, 3);

 四、函数返回值是函数指针

 

//GetValue.h中代码
typedef int (*PFUN)(int x,int y);
//新类型 PFUN  旧类型int (*)(int x,int y)


//映射表是一个 结构体数组,为了建立映射表,我们先建立一个结构体.此结构体包含 一个字符串 和一个函数指针

struct NameFunctionPair {
    char name[30];  //字符串
    PFUN function;  //函数指针
};

typedef struct NameFunctionPair NameFunctionPair;


//求2个数的最大值
int maxValue(int x,int y);
//求2个数的最小值
int minValue(int x,int y);

int sum(int x,int y);
int minus(int x,int y);
int multiple(int x,int y);
int divide(int x,int y);
int gcd(int x,int y);//最大公约数
int gbs(int x,int y);//最小公倍数

//根据字符串 获取 函数名
PFUN functionOfName(char *name);

//三个参数 ,前2个是参与运算的数字,第三个参数用于查询映射表
//返回值是 运算结束后的结果,如何运算,取决于第三个参数.
int getValue(int x,int y, char *name);
 1 //GetValue.m中代码
 2 #import "GetValue.h"
 3 
 4 NameFunctionPair nfps[] = {
 5     {"max",maxValue},
 6     {"min",minValue},
 7     {"sum",sum},
 8     {"minus",minus},
 9     {"mul",multiple},
10     {"div",divide},
11     {"gcd",gcd},
12     {"gbs",gbs}
13 };
14 
15 PFUN functionOfName(char *name)
16 {
17     for (int i = 0; i < sizeof(nfps)/sizeof(nfps[0]); i++) {
18         if (strcmp(name, nfps[i].name) == 0){
19             //如果映射表里 有对应的 function, 返回这个 function
20             return nfps[i].function;
21         }
22     }
23     //如果没找到,默认求最大值
24     return maxValue;
25 }
26 int getValue(int x,int y, char *name)
27 {
28     PFUN fun = functionOfName(name);//根据 name 获取对应的函数
29     return fun(x,y);//使用选定的函数计算结果
30 }
31 
32 int gbs(int x,int y)
33 {
34     return x * y / gcd(x, y);
35 }
36 
37 int gcd(int x,int y)
38 {
39     while (x % y != 0) {
40         int temp = x % y;
41         x = y;
42         y = temp;
43     }
44     return y;
45 }
46 
47 int divide(int x,int y)
48 {
49     return x/y;
50 }
51 
52 int multiple(int x,int y)
53 {
54     return x * y;
55 }
56 
57 int minus(int x,int y)
58 {
59     return x - y;
60 }
61 
62 int sum(int x,int y)
63 {
64     return x + y;
65 }
66 
67 int minValue(int x,int y)
68 {
69     return x < y ? x : y;
70 }
71 
72 int maxValue(int x,int y)
73 {
74     return x > y ? x : y;
75 }
//主函数中代码
#import <Foundation/Foundation.h>
#import "GetValue.h"
int main(int argc, const char * argv[])
{
    /**
     *  建立一张映射表,存储 字符串-函数名 对儿
        函数调用时,检查给定的字符串是否在映射表中,如果在,取出对应的函数名
        使用取出的函数名,调用函数,完成结果.
     */
    
    printf("%d\n",getValue(8, 12, "mul"));
    
    
    return 0;
}

 

 

iOS学习笔记---c语言第十一天,布布扣,bubuko.com

iOS学习笔记---c语言第十一天

标签:style   blog   color   get   使用   strong   

原文地址:http://www.cnblogs.com/limicheng/p/3798670.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!