C----------------LessonDynamicSort

来源:互联网 发布:说明书排版设计软件 编辑:程序博客网 时间:2024/06/14 13:26

#import <Foundation/Foundation.h>

typedef struct student {

    char name[10];

    int age;

    float score;

}Student;

typedef BOOL (*SORT) (Student,Student);

struct nameFunctionPair{

    char name[10]; //名字

    SORT function; //对应的函数地址

};

typedef structnameFunctionPair Pair;



/*

//年龄升序

void sortStudentAgeAsc(Student stu[], int count);

void sortStudentAgeAsc(Student stu[], int count)

{

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

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

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

                Student temp = stu[j];

                stu[j] = stu[j + 1];

                stu[j + 1] = temp;

            }

        }

    }

}


//成绩升序

void sortSudentScoreAsc(Student stu[], int count);

void sortSudentScoreAsc(Student stu[], int count)

{

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

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

            if (stu[j].score > stu[j + 1].score) {

                Student temp = stu[j];

                stu[j] = stu[j + 1];

                stu[j + 1] = temp;

            }

        }

    }

}

//姓名升序

void sortStudentNameAsc(Student stu[], int count);

void sortStudentNameAsc(Student stu[], int count)

{

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

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

            if (strcmp(stu[j].name, stu[j + 1].name) > 0) {

                Student temp = stu[j];

                stu[j] = stu[j + 1];

                stu[j + 1] = temp;

            }

        }

    }

}

 */

//排序方式

//年龄

BOOL compareByAge(Student stu1,Student stu2);

BOOL compareByAge(Student stu1,Student stu2)

{

    return stu1.age > stu2.age;

}

//成绩

BOOL compareByScore(Student stu1,Student stu2);

BOOL compareByScore(Student stu1,Student stu2)

{

    return stu1.score > stu2.score;

}

//姓名

BOOL compareByName(Student stu1,Student stu2);

BOOL compareByName(Student stu1,Student stu2)

{

    return strcmp(stu1.name, stu2.name) >0;

}

//从匹配表中匹配到对应的函数地址

//pair---用来接收匹配表数组.

//count---接收匹配表数组的元素个数.

//name---接收用户输入的内容.

SORT matchFunctionByName(Pair pair[],int count, char *name);

SORT matchFunctionByName(Pair pair[],int count, char *name)

{

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

        if (strcmp(pair[i].name, name) ==0) {

            return pair[i].function;//匹配成功之后将函数地址返回.

        }

    }

    returnNULL;//如果没有匹配成功,返回空.

}

//排序

void sortStudent(Student *p,int count, SORT q);

void sortStudent(Student *p,int count, SORT q)

{

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

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

            if (q(*(p + j), *(p + j + 1))) {

                Student temp = *(p + j);

                *(p + j) = *(p + j + 1);

                *(p + j + 1) = temp;

            }

        }

    }

}

//输出学生信息

void outputStudentInfo(Student stu[],int count);

void outputStudentInfo(Student stu[],int count)

{

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

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

    }

}

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

{

    Student stu[5] = {

        {"atao", 18,59.9},

        {"laowang", 38,100},

        {"duck", 213,90},

        {"kris", 20,60},

        {"frank", 17,150}

    };

    //建立匹配表

    Pair pair[3] = {

        {"age", compareByAge},

        {"score",compareByScore},

        {"name",compareByName}

    };

    char temp[10] = {0};

    printf("输入排序方式:age-年龄,score-成绩,name-姓名\n");

    scanf("%s", temp);

    //根据用户输入内容匹配到对应的函数,p存储函数地址

    SORT p =  matchFunctionByName(pair,3, temp);

    while (p == NULL) {

        printf("输入错误,重新输入\n");

        scanf("%s", temp);

        //重新匹配

        p = matchFunctionByName(pair, 3, temp);

    }

    sortStudent(stu, 5, p);

    outputStudentInfo(stu,5);


    

    //年龄升序排序

//    sortStudent(stu, 5, compareByScore);

//    outputStudentInfo(stu, 5);



    return 0;

}


0 0
原创粉丝点击