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

来源:互联网 发布:mac默认的管理员密码 编辑:程序博客网 时间:2024/04/29 21:32
<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