Xcode中实现函数指针调用和结构体排序

<pre name="code" class="cpp"><p><span style="font-family:Microsoft YaHei;font-size:18px;">①   创建一个Teacher数组,利用上面的sortTeachers(Teacher  teacher[ ] ,int count,CompareFunctionPointer  cfp)函数,调用函数(使用switch…case,通过控制台输入的数字与枚举值匹配实现调用不同的函数)来分别实现按姓名、员工编号、评分排序,其中每种排序的升、降方式都要实现。比如:分别实现对年龄按照升序、降序排列。</span></p><p><span style="font-family:Microsoft YaHei;font-size:18px;">②   输出教师数组中全部的男老师。</span></p><p><span style="font-family:Microsoft YaHei;font-size:18px;">③   输出教师数组中的所有女老师。 </span></p>

/<span style="font-size:24px;">/  Teacher.h</span>
//  Exame
//
//  Created by lanou3g on 15-3-25.
//  Copyright (c) 2015年 lanou3g. All rights reserved.
//

#import <Foundation/Foundation.h>
// 第二种定义
// 性别
// typedef enum sex
// {
//    Man,
//    Woman
// } Sex;

// typedef struct teacher
// {
//    char name[20];
//     Sex sex;
//    int age;
//    int num;
//    float score;
//    
// } Teacher;

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


enum FunctionName
{
    CompareNameByAscending = 1,
    CompareNameByDescending,
    CompareNumberByAscending,
    CompareNumberByDescending,
    CompareScoreByAscending,
    CompareScoreByDescending,
    PrintFemaleTeacher,
    PrintMaleTeacher
    
    
};
void printTeacher(Teacher *teacher);

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

void sortTeachers(Teacher teacher[],int count,CompareFunctionPointer cfp);//<span style="font-size:24px;"><strong>函数指针的利用</strong></span>

void printTeachers(Teacher *teachers,int count);

BOOL compareNumberAscending(Teacher teacher1,Teacher teacher2);
BOOL compareNumberDescending(Teacher teacher1,Teacher teacher2);

BOOL compareNameAscending(Teacher teacher1,Teacher teacher2);
BOOL compareNameDescending(Teacher teacher1,Teacher teacher2);

BOOL compareScoreDescending(Teacher teacher1,Teacher teacher2);
BOOL compareScoreAscending(Teacher teacher1,Teacher teacher2);


void printMaleTeacher(Teacher *teacher,int count);
void printFemaleTeacher(Teacher *teacher,int count);







<pre name="code" class="cpp">//
// <strong><span style="font-size:18px;"> Teacher.m</span></strong>
//  Exame
//
//  Created by lanou3g on 15-3-25.
//  Copyright (c) 2015年 lanou3g. All rights reserved.
//

#import "Teacher.h"

void printTeacher(Teacher *teacher)
{
    printf("姓名:%s,性别:%c,年龄:%d,编号:%d,评分:%f\n",teacher->name,teacher->sex,teacher->age,teacher->num,teacher->score);
}

typedef BOOL (*CompareFunctionPointer)(Teacher teacher1,Teacher teacher2);//<span style="font-size:24px;">定义函数指针</span>

void sortTeachers(Teacher teacher[],int count,CompareFunctionPointer cfp)//<span style="font-size:24px;">函数回调</span>
{
    for (int i = 0; i < count - 1; i ++) {
        for (int j = 0; j < count - i - 1; j ++) {
            if (cfp(teacher[j],teacher[j+1])) {
                Teacher tea = teacher[j];
                teacher[j] = teacher[j+1];
                teacher[j+1] = tea;
            }
        }
    }
}

void printTeachers(Teacher *teachers,int count)
{
    for (int i = 0; i < count; i ++) {
        printTeacher(&teachers[i]);
    }
}

BOOL compareNumberAscending(Teacher teacher1,Teacher teacher2)
{
    return teacher1.num > teacher2.num;
}
BOOL compareNumberDescending(Teacher teacher1,Teacher teacher2)
{
    return teacher1.num < teacher2.num;
}
BOOL compareNameAscending(Teacher teacher1,Teacher teacher2)
{
    //return strcmp(teacher1.name,teacher2.name);
    if (strcmp(teacher1.name,teacher2.name) > 0) {
        return YES;
    }
    else
        return NO;
}
BOOL compareNameDescending(Teacher teacher1,Teacher teacher2)
{
    //return strcmp(teacher2.name, teacher1.name);
    if (strcmp(teacher1.name,teacher2.name) < 0) {
        return YES;
    }
    else
        return NO;

}

BOOL compareScoreDescending(Teacher teacher1,Teacher teacher2)
{
    return teacher1.score < teacher2.score;
}
BOOL compareScoreAscending(Teacher teacher1,Teacher teacher2)
{
    return teacher1.score > teacher2.score;
}


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

<strong><span style="font-size:24px;">主函数的实现</span></strong>
<pre name="code" class="cpp">//
//  main.m
//  Exame
//
//  Created by lanou3g on 15-3-25.
//  Copyright (c) 2015年 lanou3g. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "Teacher.h"

int main(int argc, const char * argv[])
{
    printf("输入1:实现按照姓名进行升序排列\n输入2:实现按照姓名进行降序排列\n输入3:实现按照员工编号进行升序排列\n输入4:实现按照员工编号进行降序排列\n输入5:实现按照评分进行升序排列\n输入6:实现按照评分进行降序排列\n输入7:实现输出所有的女老师\n输入8:实现输出所有的男老师\n");
    printf("=====================\n");
    printf("请输入实现功能对应的数字:\n");

 
    Teacher teacher[4] = {{"zhangsan",'m',23,34,5.5},{"lishi",'f',45,56,3.5},{"wangwu",'m',22,12,1.3},{"songliu",'f',45,12,3.2}};
    
    int n = 0;
    while (scanf("%d",&n)!=EOF) {
       
        if (n >= 1 && n <= 8) {
            switch (n) {
                case CompareNameByAscending:
                    sortTeachers(teacher,4,compareNameAscending);
                    printTeachers(teacher,4);
                    break;
                case CompareNameByDescending:
                    sortTeachers(teacher,4,compareNameDescending);
                    printTeachers(teacher,4);
                    break;
                case CompareNumberByAscending:
                    sortTeachers(teacher,4,compareNumberAscending);
                    printTeachers(teacher,4);
                    break;
                case CompareNumberByDescending:
                    sortTeachers(teacher,4, compareNumberDescending);
                    printTeachers(teacher,4);
                    break;
                case CompareScoreByAscending:
                    sortTeachers(teacher,4, compareScoreAscending);
                    printTeachers(teacher,4);
                     break;
                case CompareScoreByDescending:
                    sortTeachers(teacher,4, compareScoreDescending);
                    printTeachers(teacher,4);
                     break;
                case PrintFemaleTeacher:
                    printFemaleTeacher(teacher,4);
                    break;
                case PrintMaleTeacher :
                    printMaleTeacher(teacher,4);
                    break;
               

                default:
                    break;
            }
        }
        
        else
            printf("请重新输入:\n");
    }
        

    return 0;
}





郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。