天天看点

C语言 回调函数的使用:(实例对比)

题目:定义一个结构体数组,包含学生的姓名,年龄,成绩.分别按姓名,年龄,成绩的从小到大排序输出:  

一般做法:

typedef struct student{
    char name[20]; //存储姓名
    int age ;  //存储年龄
    float score; //存储成绩
}Student;//重命名为:Student
//输出所有学生信息
void printAllStudentInfo(Student *p, int count){
    for (int i = 0; i < count; i++) {
        printf("name = %s age = %d  score = %.2f", (p + i)->name, (p + i)->age, (p + i)->score);
        printf("\n");
    }
}
//按姓名
void sortStudentByName(Student *p, int count){
    for (int i = 0; i < count - 1; i++) {
        for (int j = 0; j < count -1 -i; j++) {
            if (strcmp((p + j)->name, ( p + j + 1)->name) > 0) { //用strcmp比较姓名
                Student tempArr = *(p + j);
                *(p + j) = *(p + j + 1);
                *(p + j + 1) = tempArr;
            }
        }
    }
}
//按年龄
void sortStudentByAge(Student *p, int count){
    for (int i = 0; i < count - 1; i++) {
        for (int j = 0; j < count - 1- i; j++) {
            if ((p + j)->age > (p + j + 1)->age) { //比较年龄
                Student tempArr = *(p + j); //临时结构体数组
                *(p + j) = *(p + j + 1);
                *(p + j + 1) = tempArr;
            }
        }
    }
}
//按成绩
void sortStudentByScore(Student *p, int count){
    for (int i = 0; i < count - 1; i++) {
        for (int j = 0; j < count - 1- i; j++) {
            if ((p + j)->score > (p + j + 1)->score) { //比较成绩
                Student tempArr = *(p + j);
                *(p + j) = *(p + j + 1);
                *(p + j + 1) = tempArr;
            }
        }
    }
}

int main(int argc, const char * argv[])
{
    Student stu[5] = {
        {"zhangSan", 10, 99.99},
        {"liSi", 50, 88.8},
        {"wangWu",20, 77.7},
        {"maLiu",30, 66.6},
        {"gaZi",38, 87.9}
    };
    printf("按年龄:\n");
    sortStudentByAge(stu, 5);
    printAllStudentInfo(stu, 5);
    printf("按姓名:\n");
    sortStudentByName(stu, 5);
    printAllStudentInfo(stu, 5);
    printf("按成绩:\n");
    sortStudentByScore(stu, 5);
    printAllStudentInfo(stu, 5);
           

优化后

//分别按升序输出学生的姓名,年龄,成绩
    typedef struct student{
        char name[20];//存储姓名
        int age;//存储年龄
        float score;//存储成绩
    }Student;//重命名
    //按年龄
    //按年龄
    BOOL sortStudentByAge(Student stu1, Student stu2){
        return stu1.age > stu2.age;
    }
    //按成绩
    BOOL sortStudentByScore(Student stu1, Student stu2){
        return stu1.score > stu2.score;
    }
    //按姓名
    BOOL sortStudentByName(Student stu1, Student stu2){
        return strcmp(stu1.name, stu2.name) > 0;
    }
    typedef BOOL (*PStudent)(Student, Student);//对函数重命名
    
    void sortStudent(Student *p,int count, PStudent pstudent){
        for (int i = 0; i < count - 1; i++) {
            for (int j = 0; j < count - 1- i; j++) {
                if (pstudent(*(p + j), *(p + j +1))) {
                    Student tempArr = *(p + j);
                    *(p + j) = *(p + j + 1);
                    *(p + j + 1) = tempArr;
                }
            }
        }
    }
    //输出所有学生信息
    void printAllStudentInfo(Student *p, int count){
        for (int i = 0; i < count; i++) {
            printf("name = %s age = %d  score = %.2f", (p + i)->name, (p + i)->age, (p + i)->score);
            printf("\n");
        }
    }
    int main(int argc, const char * argv[])
    {
        
        Student stu[5] = {
            {"zhangSan", 10, 99.99},
            {"liSi", 50, 88.8},
            {"wangWu",20, 77.7},
            {"maLiu",30, 66.6},
            {"gaZi",38, 87.9}
        };
        //按年龄
        printf("按年龄:\n");
        sortStudent(stu, 5, sortStudentByAge);
        printAllStudentInfo(stu, 5);
        //按姓名
        printf("按名字:\n");
        sortStudent(stu, 5, sortStudentByName);
        printAllStudentInfo(stu, 5);
        //按成绩
        printf("按成绩:\n");
        sortStudent(stu, 5, sortStudentByScore);
        printAllStudentInfo(stu, 5);
           

继续阅读