题目:
一、项目需求:
- 创建一对文件Teacher.h Teacher.m。1分
- 在Teacher.h中,定义一个Teacher结构体,成员变量包括:姓名、性别、年龄、员工编号、评分。2分
- 声明一个打印Teacher的函数,void printTeacher(Teacher * teacher)。1分 实现部分 4分
- 定义一个函数指针类型,typedef BOOL(*CompareFunctionPointer)(Teacher teacher1, Teacher teacher2);1分
- 声明一个实现老师数组排序的函数,void sortTeachers(Teacher teacher[ ] ,int count, CompareFunctionPointer cfp);1分 实现部分 4分
- 声明一个打印数组中所有老师的函数,void printTeachers(Teacher * teachers, int count);1分 实现部分4分
- 分别声明两个比较Teacher姓名的函数:BOOL compareNameByAscending(Teacher teacher1 , Teacher teacher2);(升序方式)、BOOL compareNameByDescending(Teacher teacher1, Teacher teacher2);(降序方式)。2分 实现部分 8分
- 分别声明两个比较Teacher员工编号的函数:BOOL compareNumberByAscending(Teacher teacher1 , Teacher teacher2);(升序方式)、BOOL compareNumberByDescending(Teacher teacher1, Teacher teacher2);(降序方式)2分 实现部分8分
- 分别声明两个比较Teacher员工评分的函数:BOOL compareScoreByAscending(Teacher teacher1 , Teacher teacher2);(升序方式)、BOOL compareScoreByDescending(Teacher teacher1, Teacher teacher2);(降序方式)2分 实现部分8分
10. 声明一个输出教师数组中全部男老师的函数,void printMaleTeacher(Teacher * teachers, int count);1分实现部分 4分
11. 声明一个输出教师数组中全部女老师的函数,void printFemaleTeacher(Teacher * teacher, int count);1分实现部分 4分
12. 声明一个枚举FunctionName,其中的每个枚举值于用户从控制台输入的功能数字一一对应。例如:当从控制台输入1时,此时实现按照姓名进行升序排列。2分 实现部分 8分(以上声明部分共17分 实现部分是52分)
13. 在Teacher.m中,实现上述的全部函数功能。
上述声明部分和实现部分一共是69分
15. 在main.m文件中要求:
(1)要求在控制台中,输出用户操作说明。如图:3分
(2)创建一个Teacher数组,数组中包含5个Teacher结构体类型的数据。(Teacher的姓名使用拼音,不使用汉字;性别使用‘m’表示男、‘f’表示女,评分使用float类 型。)7分
(3)根据控制台输入的数字,控制输出结果:
1)使用while循环实现重新输入功能。7分
2) 使用switch…case实现:根据控制台输入的1~8之间的数字,对Teacher数组进行不同排序并输出结果。case的数值使用1~8数字对应的枚举值。6分
3)输入数字1~6,对Teacher数组进行排序,使用sortTeachers 函数、比较函数实现排序,排序后使用printTeachers函数打印输出所有老师信息。4分
4)输入数字7~8,根据性别筛选老师,分别使用printMaleTeacher、printFemaleTeacher函数输出结果。2分
5)如果输入1~8以外的数字,则输出“对不起,您所输入的数字没有对应的函数,请重新输入!!!!”。2分
编程实现
main.m文件
int main(int argc, const char * argv[])
{
Teacher teac1 = {"xiaomi",‘m‘,25,15,98.5};
Teacher teac2 = {"yunjie",‘f‘,18,11,97.5};
Teacher teac3 = {"huige",‘m‘,35,1,99};
Teacher teac4 = {"bobo",‘m‘,29,3,95.5};
Teacher teac5 = {"cuige",‘m‘,27,6,96.5};
Teacher teacArray[5] = {teac1,teac2,teac3,teac4,teac5};
// Teacher *p = &teac1; //验证函数pritTeacher
// pritTeacher(p);
// CompareFunctionPointer cfp;
// cfp = CompareFunctionStudent;
// printTeachers(teacArray,5); //验证打印结构体数组的函数
enum FunctionName
{
compareNameByAscending = 1,
compareNameByDescending,
compareNumberByAscending,
compareNumberByDescending,
compareScoreByAscending,
compareScoreByDescending,
printMaleTeacher,
printFemaleTeacher
};
int putChoiceNumber = 0;
do{
printf("输入1:实现按照姓名进行升序排列\n输入2:实现按照姓名进行降序排列\n输入3:实现按照员工编号进行升序排列\n输入4:实现按照员工编号进行降序排列\n输入5:实现按照评分进行升序排列\n输入6:实现按照评分进行降序排列\n输入7:实现输出所有女老师\n输入8:实现输出所有的男老师\n==============================\n请输入实现功能对应的数字:");
scanf("%d",&putChoiceNumber);
// cfp = compareNum(putChoiceNumber);
// sortTeacher(teacArray,4 ,cfp); //使用结构体实现
// printTeachers(teacArray,4); //使用结构体实现
findFunction(putChoiceNumber,teacArray); //使用枚举实现
}while(putChoiceNumber<1||putChoiceNumber>8);
return 0;
}
function.h文件
typedef struct Teacher{
char name[30];
char sex;
int age;
int number;
float score;
}Teacher;
void pritTeacher(Teacher *teacher); //打印个人信息
BOOL CompareFunctionStudent(Teacher teacher1,Teacher teacher2);
typedef BOOL(*CompareFunctionPointer)(Teacher teacher1,Teacher teacher2);
void sortTeacher(Teacher teacher[],int count ,CompareFunctionPointer cfp); //按照评分给两个老师排序
void printTeachers(Teacher *teachers,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 *teachers,int count);//输出全部女教师的函数
typedef struct FunctionNameStruct{
int putNum;
CompareFunctionPointer choiceNum;
}FunctionNameStruct;
CompareFunctionPointer compareNum(int );
void findFunction(int ,Teacher *);
function.m文件
#import "Teacher.h"
void pritTeacher(Teacher *teacher)
{
printf("姓名:%s\t性别:%c\t年龄:%d\t员工编号:%d\t评分:%.2f\n",teacher->name,teacher->sex,teacher->age,teacher->number,teacher->score);
}
//排序函数
void sortTeacher(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+1];
teacher[j+1] = teacher[j];
teacher[j] = temp;
}
}
}
}
BOOL CompareFunctionStudent(Teacher teacher1,Teacher teacher2)
{
return teacher1.score>teacher2.score;
}
//打印数组
void printTeachers(Teacher *teachers,int count)
{
for (int i = 0; i < count; i++) {
printf("姓名:%s\t性别:%c\t年龄:%d\t员工编号:%d\t评分:%.2f\n",teachers[i].name,teachers[i].sex,teachers[i].age,teachers[i].number,teachers[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].sex == ‘m‘) {
pritTeacher(teachers+i);
}
}
}
//输出全部女教师的函数
void printFemaleTeacher(Teacher *teachers,int count)
{
for (int i = 0; i < count; i++) {
if (teachers[i].sex == ‘f‘) {
pritTeacher(teachers+i);
}
}
}
CompareFunctionPointer compareNum(int a)
{
FunctionNameStruct choiceNum1={1,compareNameByAscending};
FunctionNameStruct choiceNum2={2,compareNameByDescending};
FunctionNameStruct choiceNum3={3,compareNumberByAscending};
FunctionNameStruct choiceNum4={4,compareNumberByDescending};
FunctionNameStruct choiceNum5={5,compareScoreByAscending};
FunctionNameStruct choiceNum6={6,compareScoreByDescending};
FunctionNameStruct choiceNum[6] = {choiceNum1,choiceNum2,choiceNum3,choiceNum4,choiceNum5,choiceNum6};
for (int i = 0; i < 6; i++) {
if (choiceNum[i].putNum == a) {
return choiceNum[i].choiceNum;
}
}
return choiceNum[4].choiceNum;
}
void findFunction(int a,Teacher *teacArray)
{
switch (a) {
case 1:
// cfp = compareNameByAscending;
sortTeacher(teacArray,5 ,compareNameByAscending);
printTeachers(teacArray,5);
break;
case 2:
// cfp = compareNameByDescending;
sortTeacher(teacArray,5 ,compareNameByDescending);
printTeachers(teacArray,5);
break;
case 3:
// cfp = compareNumberByAscending;
sortTeacher(teacArray,5 ,compareNumberByAscending);
printTeachers(teacArray,5);
break;
case 4:
// cfp = compareNumberByDescending;
sortTeacher(teacArray,5 ,compareNumberByDescending);
printTeachers(teacArray,5);
break;
case 5:
// cfp = compareScoreByAscending;
sortTeacher(teacArray,5 ,compareScoreByAscending);
printTeachers(teacArray,5);
break;
case 6:
// cfp = compareScoreByDescending;
sortTeacher(teacArray,5 ,compareScoreByDescending);
printTeachers(teacArray,5);
break;
case 7:
printFemaleTeacher(teacArray,5);
break;
case 8:
printMaleTeacher(teacArray,5);
break;
default:
printf("对不起,您所输入的数字没有对应的函数,请重新输入!!!!\n");
break;
}
}