采用封装的思想对学生姓名成绩年龄排序

<span style="font-size:18px;">//
//  main.m
//  LessonFunctionPointer2
//
//  Created by lanouhn on 14-7-30.
//  Copyright (c) 2014年 Summer. All rights reserved.
//

#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 nameFunctionPair{
    char name[20];       //存储函数对应的字符串
    Pstudent function;   //存储字符串对应函数的地址
}NameFunctionPair;
//根据给定的字符串查找匹配表,找出对应的函数
//name 用来接收匹配的字符串
//p 用来接收匹配表
//count 用来接收匹配表中元素的个数
Pstudent getFunctionByName(char *name, NameFunctionPair *p, int count)
{
    //根据输入的内容查匹配表找到对应的函数
    for (int i = 0; i < count; i++) {
        if (strcmp(name, (p + i)->name) == 0) {
            //如果匹配到对应的函数,将函数地址返回
            return (p + i)->function;
        }
    }
    //如果没有匹配到对应的函数,就返回NULL.
    return NULL;
}
//三个函数之间唯一的不同就在于 冒泡排序中的判断条件不同
void sortStudent(Student *p, int count, char *name, NameFunctionPair *pair, int number)
{
    //接收一下匹配到的函数的地址
    Pstudent function = getFunctionByName(name, pair, number);
    for (int i = 0; i < count - 1; i++) {
        for (int j = 0; j < count - 1 - i; j++) {
            if (function(*(p + j), *(p + j + 1))) {
                Student temp = {0};
                temp = *(p + j);
                *(p + j) = *(p + j + 1);
                *(p + j + 1) = temp;
            }
        }
    }
}
//输出函数
void outputStudentInfo(Student *p, int count)
{
    for (int i = 0; i < count; i++) {
        printf("name:%s, age:%d, score:%.2f \n", (p + i)->name, (p + i)->age, (p + i)->score);
    }
}
int main(int argc, const char * argv[])
{
    //方法一
    Student stu[5] = {
        {"summer", 19, 20.1},
        {"mahaitao", 20, 90.0},
        {"xiaoyu", 21, 12.3},
        {"dream", 22, 23.3},
        {"rain", 21, 25.3}
    };
    //创建匹配表
    NameFunctionPair pair[3] = {
        {"name", sortStudentByName},
        {"age", sortStudentByAge},
        {"score", sortStudentByScore}
    };
    char tempName[20] = {0}; //用来接收从控制台输入的字符串
    printf("请输入排序的方式(姓名:name,年龄:age,成绩:score):\n");
    scanf("%s", tempName);
    //对学生排序
    sortStudent(stu, 5, tempName, pair, 3);
    outputStudentInfo(stu, 5);
    
    
    
    //方法二
//    Student stu[5] = {
//        {"summer", 19, 20.1},
//        {"mahaitao", 20, 90.0},
//        {"xiaoyu", 21, 12.3},
//        {"dream", 22, 23.3},
//        {"rain", 21, 25.3}
//    };
//    //1.按姓名升序排序
//    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) {
//                    Student temp = {0};
//                    temp = *(p + j);
//                    *(p + j) = *(p + j + 1);
//                    *(p + j + 1) = temp;
//                }
//            }
//        }
//    }
//    //2.按年龄升序排序
//    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 temp = {0};
//                    temp = *(p + j);
//                    *(p + j) = *(p + j + 1);
//                    *(p + j + 1) = temp;
//                }
//            }
//        }
//    }
//    //3.按成绩升序排列
//    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 temp = {0};
//                    temp = *(p + j);
//                    *(p + j) = *(p + j + 1);
//                    *(p + j + 1) = temp;
//                }
//            }
//        }
//    }

//    sortStudentByName(stu, 5);
//    outputStudentInfo(stu, 5);
//    printf("\n");
//    printf("\n");
//    sortStudentByAge(stu, 5);
//    outputStudentInfo(stu, 5);
//    printf("\n");
//    printf("\n");
//    sortStudentByScore(stu, 5);
    
    
    
    return 0;
}
</span>

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值