首页 > 编程语言 > 详细

C语言基础_Teacher

时间:2015-08-11 22:43:10      阅读:177      评论:0      收藏:0      [点我收藏+]

1) .h中代码

#import <Foundation/Foundation.h>

typedef struct teacher{
    char name[20];
    char gender;
    int age;
    int number;
    float score;
}Teacher;

void printTeacher(Teacher *teacher);

typedef BOOL(*CompareFunctionPointer)(Teacher teacher1,Teacher teacher2);

void sortTeachers(Teacher teacher[],int count,CompareFunctionPointer cfp);

void printTeachers(Teacher *teacher,int count);

BOOL compareNameByAscending(Teacher teacher1,Teacher teacher2);

BOOL compareNameByDescending(Teacher teacher1,Teacher teacher2);

BOOL compareNumberByAscending(Teacher teacher1,Teacher teacher2);

BOOL compareNumberByDescending(Teacher teacher1,Teacher teacher2);

BOOL compareScoreByAscending(Teacher teacher1,Teacher teacher2);

BOOL compareScoreByDescending(Teacher teacher1,Teacher teacher2);

void printMaleTeacher(Teacher *teachers,int count);

void printFemaleTeacher(Teacher *teacher,int count);

enum FunctionName{
    CompareNameByAscending = 1,
    CompareNameByDescending,
    CompareNumberByAscending,
    CompareNumberByDescending,
    CompareScoreByAscending,
    CompareScoreByDescending,
    PrintFemaleTeacher,
    PrintMaleTeacher,
};

 2).m中代码

#import "Teacher.h"

void printTeacher(Teacher *teacher)
{
        printf("%s %c %d %d %.2f\n",teacher->name,teacher->gender,teacher->age,teacher->number,teacher->score);
}

typedef BOOL(*CompareFunctionPointer)(Teacher teacher1,Teacher teacher2);

void sortTeachers(Teacher teacher[],int count,CompareFunctionPointer cfp)
{
    for (int i = 0; i < count - 1; i++) {
        for (int j = 0; j < count - 1 -i; j++) {
            if (cfp(teacher[j],teacher[j+1])) {
                
                Teacher temp = teacher[j];
                teacher[j] = teacher[j+1];
                teacher[j+1] = temp;
            }
        }
    }
    printTeachers(teacher,5);
}
void printTeachers(Teacher *teacher,int count)
{
    for (int i = 0; i < count; i++) {
        printf("%s %c %d %d %.2f\n",teacher[i].name,teacher[i].gender,teacher[i].age,teacher[i].number,teacher[i].score);
    }
}

BOOL compareNameByAscending(Teacher teacher1,Teacher teacher2)
{
    return strcmp(teacher1.name, teacher2.name) > 0;
}

BOOL compareNameByDescending(Teacher teacher1,Teacher teacher2)
{
    return strcmp(teacher1.name, teacher2.name) < 0;
}

BOOL compareNumberByAscending(Teacher teacher1,Teacher teacher2)
{
    return teacher1.number > teacher2.number;
}

BOOL compareNumberByDescending(Teacher teacher1,Teacher teacher2)
{
    return teacher1.number < teacher2.number;
}

BOOL compareScoreByAscending(Teacher teacher1,Teacher teacher2)
{
    return teacher1.score > teacher2.score;
}

BOOL compareScoreByDescending(Teacher teacher1,Teacher teacher2)
{
    return teacher1.score < teacher2.score;
}

void printMaleTeacher(Teacher *teachers,int count)
{
     for (int i = 0; i < count; i++) {
        if (teachers[i].gender == f){
            printTeacher(&teachers[i]);
        }
     }
}

void printFemaleTeacher(Teacher *teacher,int count)
{
    for (int i = 0; i < count; i++) {
        if (teacher[i].gender == m) {
            printTeacher(&teacher[i]);
        }
    }
}

3).main中的代码

#import <Foundation/Foundation.h>
#import "Teacher.h"
int main(int argc, const char * argv[]) {
    
     Teacher teacher[5] = {{"abc",m,28,2012,90},
                           {"def",f,35,2013,80},
                           {"hji",m,25,2014,78},
                           {"asd",m,31,2010,88},
                           {"xyz",f,28,2015,90}};
    
    
    while (true) {
        
        printf("输入1: 实现按照姓名进行升序排列\n");
        printf("输入2: 实现按照姓名进行降序排列\n");
        printf("输入3: 实现按照员工编号进行升序排列\n");
        printf("输入4: 实现按照员工编号进行降序排列\n");
        printf("输入5: 实现按照评分进行升序排列\n");
        printf("输入6: 实现按照评分进行降序排列\n");
        printf("输入7: 实现输出所有的女老师\n");
        printf("输入8: 实现输出所有的男老师\n");
        printf("==============================\n");
        printf("请输入实现功能对应的数字:\n");
    
        int a;
        scanf("%d",&a);
        switch (a) {
            case CompareNameByAscending:
            {
                sortTeachers(teacher, 5, compareNameByAscending);
            }
                break;
            case CompareNameByDescending:
            {
                sortTeachers(teacher, 5, compareNameByDescending);
            }
                break;
            case CompareNumberByAscending:
            {
                sortTeachers(teacher, 5, compareNumberByAscending);
            }
                break;
            case CompareNumberByDescending:
            {
                sortTeachers(teacher, 5, compareNumberByDescending);
            }
                break;
            case CompareScoreByAscending:
            {
                sortTeachers(teacher, 5, compareScoreByAscending);
            }
                break;
            case CompareScoreByDescending:
            {
                sortTeachers(teacher, 5, compareScoreByDescending);
            }
                break;
            case PrintFemaleTeacher:
            {
                printMaleTeacher(teacher,5);
            }
                break;
            case PrintMaleTeacher:
            {
                printFemaleTeacher(teacher,5);
            }
                break;
            default:
            {
                printf("对不起,你所输入的数字没有对应的函数,请重新输入\n");
            }
                break;
        }
    }
    
    return 0;
}

 

C语言基础_Teacher

原文:http://www.cnblogs.com/ChinaTiger/p/4722259.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!