按输入的排序方式实现对5个学生进行排序

函数声明,函数实现
<pre name="code" class="cpp"><span style="font-size:24px;">#import <Foundation/Foundation.h>
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);
//建立字符串和函数之间的一一对应关系
typedef struct nameaFunctionPair{
    char name[10];//存储函数对应的字符串
    pStudent function;//存储字符串对应的函数的地址
}NameaFunctionPair;
//三个函数之间唯一的不同就在于 冒泡排序中的判断条件不同
pStudent getFunctionByName(char *name, NameaFunctionPair *p, int count){
    //根据输入的内容查匹配表找到对应的函数
    for (int i = 0; i < count; i++) {
        if (strcmp(name, (p + i)->name) == 0) {
            //如果匹配到对应的函数,将函数地址返回
            //output(stu, 5);
            return (p + i) ->function;
        }
    }
    //如果没有对应的函数,就返回null
    return NULL;
}

void sortStudent(Student *pStu, int count, char *name, NameaFunctionPair *pair, int number){//pstu1用来接收比较函数的地址
    //根据输入的内容查匹配表找到对应的函数
    pStudent function= getFunctionByName(name, pair, number);//p接收地址
    for (int i = 0; i < count - 1; i++) {
        for (int j = 0; j < count - 1 - i; j++) {
            if (function(*(pStu + j), *(pStu + j + 1))) {
                Student temp = *(pStu + j);
                *(pStu + j) = *(pStu + j + 1);
                *(pStu + j + 1) =temp;
            }
        }
    }

}
//
按姓名升序排序
//void sortSudentByName(Student *pStu,int count){
//    for (int i = 0; i < count - 1; i++) {
//        for (int j = 0; j < count - 1 - i; j ++) {
//            if (strcmp((pStu + j) ->name, (pStu + j + 1)->name) > 0) {
//                Student temp = *(pStu + j);
//                *(pStu + j) = *(pStu + j + 1);
//                *(pStu + j + 1) =temp;
//            }
//        }
//    }
//}
按年龄升序排序
//void sortStudentByAge(Student *pStu, int count){
//    for (int i = 0; i < count - 1; i++) {
//        for (int j = 0; j <count - 1 - i; j ++) {
//            if ((pStu + j) ->age > (pStu + j + 1) ->age) {
//                Student temp = *(pStu + j);
//                *(pStu + j) = *(pStu + j + 1);
//                *(pStu + j + 1) =temp;
//
//            }
//        }
//    }
//}
按成绩升序排序
//void sortStudentByScore(Student *pStu, int count){
//    for (int i = 0; i < count - 1; i++) {
//        for (int j = 0; j <count - 1 - i; j++) {
//            if ((pStu + j)->score > (pStu + j + 1)->score) {
//                Student temp = *(pStu + j);
//                *(pStu + j) = *(pStu + j + 1);
//                *(pStu + j + 1) =temp;
//            }
//        }
//    }
//}
void output(Student *pStu, int count){
    for (int i = 0; i < count; i++) {
        printf("%s %d %.2f\n", (pStu + i)->name, (pStu + i) ->age, (pStu + i) ->score);
    }
}
//根据给定的字符串查找匹配表,找出对应的函数,有返回值
//name用来接收匹配的字符串
//p用来接收匹配表
//count 用来接收匹配表中元素的个数
int main(int argc, const char * argv[])
{
    
    Student stu[5] = {
        {"jg", 20, 79},
        {"uhk,g",7, 467},
        {"vcjhf",34,87},
        {"cjfm",356,698},
        {"vjgy",345,693}
    };
    //创建一个匹配表,  结构体
    NameaFunctionPair pair[3] = {
        {"name", sortStudentByName},
        {"age",sortStudentByAge},
        {"score", sortStudentByScore}
    };
    char tempname[20] = {0};//用来接收从控制台输入的字符串
    printf("请输入排序的方式(姓名:name, 年龄:age, 成绩:score):\n");
    scanf("%s", tempname);
//    //根据输入的内容查匹配表找到对应的函数
//    pStudent p= getFunctionByName(tempname, pair, 3);//p接收地址
    
    
    //对学生排序
    sortStudent(stu, 5, tempname, pair, 3);
    output(stu, 5);
</span>



                
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值