main 部分
//
// main.m
// First test
//
// Created by lanou3g on 15/6/26.
// Copyright (c) 2015年 wangjiafu. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "Teacher.h"
int main(int argc, const char * argv[]) {
Teacher tea[] = {
{"tb", 'm', 25, 7, 80.6},
{"ta", 'w', 24, 6, 75.5},
{"tc", 'm', 27, 9, 90.4},
{"td", 'w', 26, 8, 69.3},
{"te", 'm', 22, 5, 96.5}
};
int count = sizeof(tea)/sizeof(tea[0]);
printf("输入1:实现姓名升序排列\n");
printf("输入2:实现姓名降序排列\n");
printf("输入3:实现工作编号升序排列\n");
printf("输入4:实现工作编号降序排列\n");
printf("输入5:实现评分升序排列\n");
printf("输入6:实现评分降序排列\n");
printf("输入7:实现输出所有女教师\n");
printf("输入1:实现所有男教师\n");
printf("=========================\n");
printf("请输入对应功能的数字:");
enum functionName c = 1;
scanf("%d",&c);
while (c < 1 || c > 8) {
printf("请输入有效数字");
scanf("%d",&c);
}
switch (c) {
case 1:
softTeachers(tea, count, CompareName1);
break;
case 2:
softTeachers(tea, count, CompareName2);
break;
case 3:
softTeachers(tea, count, CompareNumber1);
break;
case 4:
softTeachers(tea, count, CompareNumber2);
break;
case 5:
softTeachers(tea, count, ComparScore1);
break;
case 6:
softTeachers(tea, count, CompareScore2);
break;
case 7:
printFemale(tea, count);
break;
case 8:
printMale(tea, count);
break;
default:
break;
}
return 0;
}
.h 部分
#import <Foundation/Foundation.h>
typedef struct teacher{
char name[20];
char gender;
int age;
int number;
float score;
}Teacher;
enum functionName{
CompareNameByAscending = 1,
CompareNameByDesscending,
CompareNumberByAscending,
CompareNumberByDescending,
CompareScoreByScenging,
CompareScoreByDescending,
PrinterFemaleTeacher,
PrinterMaleTeacher,
};
typedef BOOL (*compare)(Teacher teacher1, Teacher teacher2);
void softTeachers(Teacher teacher[],int count,compare cfp);
void printTeachers(Teacher *teacher,int count);
BOOL CompareName1(Teacher teacher1,Teacher teacher2);
BOOL CompareName2(Teacher teacher1,Teacher teacher2);
BOOL CompareNumber1(Teacher teacher1, Teacher teacher2);
BOOL CompareNumber2(Teacher teacher1, Teacher teacher2);
BOOL ComparScore1(Teacher teacher1, Teacher Teacher2);
BOOL CompareScore2(Teacher teacher1, Teacher teacher2);
void printMale(Teacher *teacher,int count);
void printFemale(Teacher *teacher, int count);
.m部分
#import "Teacher.h"
void printTeacher(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);
}
}
void softTeachers(Teacher teacher[],int count,compare 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;
}
}
}
printTeacher(teacher, count);
}
void printTeachers(Teacher *teacher, int count) {
printTeacher(teacher, count);
}
BOOL CompareName1(Teacher teacher1, Teacher teacher2) {
return strcmp(teacher1.name,teacher2.name) > 0 ? YES : NO;
}
BOOL CompareName2(Teacher teacher1, Teacher teacher2) {
return strcmp(teacher1.name, teacher2.name) < 0 ? YES : NO;
}
BOOL CompareNumber1(Teacher teacher1, Teacher teacher2) {
return teacher1.number > teacher2.number ? YES : NO;
}
BOOL CompareNumber2(Teacher teacher1, Teacher teacher2) {
return teacher1.number < teacher2.number ? YES : NO;
}
BOOL ComparScore1(Teacher teacher1, Teacher Teacher2) {
return teacher1.score > Teacher2.score ? YES : NO;
}
BOOL CompareScore2(Teacher teacher1, Teacher teacher2) {
return teacher1.score < teacher2.score ? YES :NO;
}
void printMale(Teacher *teacher,int count){
for (int i = 0; i < count; i++) {
if (teacher[i].gender == 'm') {
printf("%s, %c, %d, %d, %.2f\n",teacher[i].name,teacher[i].gender,teacher[i].age,teacher[i].number,teacher[i].score);
}
}
}
void printFemale(Teacher *teacher, int count) {
for (int i = 0; i < count; i++) {
if (teacher[i].gender == 'w') {
printf("%s, %c, %d, %d, %.2f\n",teacher[i].name,teacher[i].gender,teacher[i].age,teacher[i].number,teacher[i].score);
}
}
}