C 入门 第十一节

int maxValue(int x,int y)
{
    return x > y ? x : y;
}

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

int max(int x,int y)
{
    return x < y ? x : y;
}
int sum(int x,int y)
{
    return x + y;
}

int getValue(int x,int y,int (*p)(int ,int ))
{
    //  p是函数指针,具体指向一个函数,由调用函数时传入的函数名决定
    int r = p(x,y);
    return r;
}


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

#pragma mark --------------------函数指针--------------------
/*
    函数: 实现某种特定功能的代码
    程序编译时,系统会将函数代码分配在代码区,这段存储空间的起始地址就是函数的地址.又称
    函数的指针
    函数名和数字名一样是地址
 
//  通过函数名调用函数
    printf("%d ",maxValue(3,5));
    
    函数指针: 指向函数的指针.可以通过函数指针调用指向的函数
    定义函数指针指向maxValue函数
    指针类型必须与指向的数据的类型匹配,函数有类型,函数指针必须和函数的类型保持一致
    maxValue函数的类型 int(int,int)
    返回值类型 (*函数指针名)(参数类型) = 函数名

    int (*p)(int x,int y) = maxValue;
    printf("%d ",p(30,50));
    
//    函数指针类型:返回值类型 (*)(参数类型) 例如:
    int (*)(int x ,int y);
 
//  练习: void printHello(); 定义一个可以指向上述函数的函数指针,并通过函数指针实现调用该函数
    
    void (*p)() = printHello;
    p();

    
//  练习 定义两个函数,⼀一个求最⼤大值,⼀一个求和,输⼊入max或sum分别求3,5的 最⼤大值或和(提⽰示,定义⼀一个函数指针,根据输⼊入内容指向不同函数,最后 ⼀一次调⽤用完成)
    
    char a[5] = {0};
    scanf("%s",a);
        if (strcmp(a, "sum") == 0)
    {
        int (*p)(int x,int y) = sum;
        printf("%d ",p(3,5));

    }
    if (strcmp(a, "max") == 0)
    {
        int (*p)(int x,int y) = max;
        printf("%d ",p(3,5));
        
    }
    
    printf(" ");
*/
#pragma mark --------------回调函数------------------
/*
    //函数指针作为函数参数
    int (*p)(int ,int ) = max;
    int (*q)(int ,int ) = sum;
    int max1 = getValue(3, 5, p);
    int sum2 = getValue(3, 5, q);
    printf("%d %d ",max1,sum2);
    
    回调过程: 在getValue执行的过程中,体内各国传入的函数指针,调用执行某个函数
    回调函数: 在函数执行过程中.通过函数指针被调用的函数
    函数回调: 调用回调函数的操作
*/
//  练习 写⼀一函数查找成绩90分以上的学员,使⽤回调函数在姓名后加”⾼富帅”。
/*
    Student stu1[3] =
    {
        {1,"zhangsan",84.0},
        {2,"lisi",59.0},
        {3,"wangwu",96.0}
    };
    Student *p = stu1;
    findstudentScorechengji(p);
*/
/*
    Student stu1 = {"wukong", 'm', 500, 60.0};
    Student stu2 = {"zixia", 'w', 18, 98.0};
    Student stu3 = {"sanzang", 'w', 40, 100.0};
    Student stu4 = {"longma", 'w', 27, 93.0};
    Student stu5 = {"bajie", 'w', 300, 59.0};
    Student stus[5] = {stu1, stu2, stu3, stu4, stu5};
    //    定义一个函数指针,指向拼接字符串函数
    void (*p)(char*) = addStr;
    
    findStudentByScore(stus, 5, p);
    printAllStudent(stus, 5);
*/   
    
#pragma mark ----------动态排序--------------
    
    //  排序: 排序的规则不同,年龄,姓名,成绩,升序,降序
    //利用回调函数实现动态排序
    
    Student stu1 = {"wukong", 'm', 500, 60.0};
    Student stu2 = {"zixia", 'w', 18, 98.0};
    Student stu3 = {"sanzang", 'w', 40, 100.0};
    Student stu4 = {"longma", 'w', 27, 93.0};
    Student stu5 = {"bajie", 'w', 300, 59.0};
    Student stus[5] = {stu1, stu2, stu3, stu4, stu5};
//    printf("按姓名排序 ");
//    sortStudentByName(stus,5);
//    AllStudent(stus,5);
//    printf("按分数排序 ");
//    sortStudentByScore(stus,5);
//    AllStudent(stus,5);
//    printf("按年龄排序 ");
//    sortStudentByAge(stus,5);
//    AllStudent(stus,5);
//
    printf("新玩法 ");
//    printf("按姓名排序 ");
//    sortStudent(stus,5,compartStudentByName);
//    AllStudent(stus,5);
//    printf("按分数排序 ");
//    sortStudent(stus,5,compartStudentByScore);
//    AllStudent(stus,5);
//    printf("按年龄排序 ");
//    sortStudent(stus,5,compartStudentByNAge);
//    AllStudent(stus,5);
    
#pragma mark ------------函数的返回值是函数的指针--------------
 

m.h

typedef struct
{
    char name[30];
    char sex;
    int age;
    float score;
}Student;

void findstudentScorechengji(Student *p);
// 根据名字大小排序
void sortStudentByName(Student *stus,int count);
//根据分数排
void sortStudentByScore(Student *stus,int count);
//根据年龄排
void sortStudentByAge(Student *stus,int count);
//遍历输出
void AllStudent(Student *stus,int count);

// 新玩法
typedef BOOL (*SORT)(Student,Student);
//SORT代表的是一个类型函数指针,指向的函数的返回值是BOOL类型,参数是俩个Student变量
//实现排序函数
void sortStudent(Student *stus,int count,SORT p_sort);
BOOL compartStudentByName(Student stu1,Student stu2);
BOOL compartStudentByScore(Student stu1,Student stu2);
BOOL compartStudentByNAge(Student stu1,Student stu2);

m.m

/*
void findstudentScorechengji(Student *p)
{
    for (int i = 0; i < 3; i ++)
    {
        if ((p + i) -> score > 90)
        {
            strcat((p + i) -> name, "高富帅");
            printf("%d %s %.1f ",(p + i) -> num,(p+i) -> name,(p + i) -> score);
        }
    }
}
*/
/*
void findStudentByScore(Student *stus,int count,void (*p)(char *)){
    for (int i = 0; i < count;i++) {
        if (stus[i].score > 90) {
            p(stus[i].name);//函数回调
        }
    }
}
void printAllStudent(Student *stus,int count){
    for (int i = 0; i < count; i++) {
        printf("name = %s, sex = %c, age = %d, score = %.2f ",stus[i].name,stus[i].sex,stus[i].age,stus[i].score);
    }
    
}
*/

// 根据名字大小排序
void sortStudentByName(Student *stus,int count)
{
    for (int i = 0; i < count - 1; i ++)
    {
        for (int j = 0; j < count - 1 - i; j ++)
        {
            if (strcmp(stus[j].name,stus[j+1].name ))
            {
                Student temp = stus[j];
                stus[j] = stus[j + 1];
                stus[j + 1] = temp;
                
            }
        }
    }
}
//根据分数排
void sortStudentByScore(Student *stus,int count)
{
    for (int i = 0; i < count - 1; i ++)
    {
        for (int j = 0; j < count - 1 - i; j ++)
        {
            if (stus[j].score > stus[j+1].score)
            {
                Student temp = stus[j];
                stus[j] = stus[j + 1];
                stus[j + 1] = temp;
                
            }
        }
    }
}
//根据年龄排
void sortStudentByAge(Student *stus,int count)
{
    for (int i = 0; i < count - 1; i ++)
    {
        for (int j = 0; j < count - 1 - i; j ++)
        {
            if (stus[j].age > stus[j+1].age)
            {
                Student temp = stus[j];
                stus[j] = stus[j + 1];
                stus[j + 1] = temp;
            }
        }
    }

}

//遍历输出
void AllStudent(Student *stus,int count)
{
    for (int i = 0; i < count; i++) {
        printf("name:%s sex:%c age:%d score:%.1f ",stus[i].name,stus[i].sex,stus[i].age,stus[i].score);
    }
    
}

//实现排序函数
void sortStudent(Student *stus,int count,SORT p_sort)
{
    for (int i = 0; i < count - 1; i ++)
    {
        for (int j = 0; j < count -i - 1; j ++)
        {
            if (p_sort(stus[j],stus[j+1]))
            {                                                                                                                                                                                                                   
                Student temp = stus[j];
                stus[j] = stus[j+1];
                stus[j+1] = temp;
            }
        }
    }
}
BOOL compartStudentByName(Student stu1,Student stu2)
{
     return strcmp(stu1.name, stu2.name) > 0;
}
BOOL compartStudentByScore(Student stu1,Student stu2)

    return stu1.score > stu2.score;   
}

BOOL compartStudentByNAge(Student stu1,Student stu2)
{
     return stu1.age > stu2.age;
}
 

原文地址:https://www.cnblogs.com/wangshuai-1129/p/5079096.html