#import <Foundation/Foundation.h>
// 声明Student结构体类型
typedef struct {
char name[20]; // 姓名
char gender; // 性别
int age; // 年龄
int number; // 编号
float score; // 评分
} Student;
// 1. 声明打印一个结构体变量,使用指针
void printStudent(Student *stu);
// 2. 声明打印数组内的全部结构体变量,使用指针
void printStudents(Student *stus, int count);
// 3.声明函数指针,使用typedef
typedef BOOL (*CompareFunctionPointer) (Student *, Student *);
// 4.声明排序函数
void sortStudents(Student *stus, int count, CompareFunctionPointer cfp);
// 5.声明比较姓名函数
BOOL compareNameByAsceding(Student *stu1, Student *stu2);
BOOL compareNameByDesceding(Student *stu1, Student *stu2);
// 6.声明比较编号函数
BOOL compareNumberByAsceding(Student *stu1, Student *stu2);
BOOL compareNumberByDesceding(Student *stu1, Student *stu2);
// 7.声明比较评分的函数
BOOL compareScoreByAsceding(Student *stu1, Student *stu2);
BOOL compareScoreByDesceding(Student *stu1, Student *stu2);
// 8.声明打印所有男/女同学
void printMaleStudent(Student *stus, int count);
void printFemaleStudent(Student *stus, int count);
enum FunctionName
{
CompareNameByAsceding = 1,
CompareNameByDesceding,
CompareNumberByAsceding,
CompareNumberByDesceding,
CompareScoreByAsceding,
CompareScoreByDesceding,
PrintFemaleStudent,
PrintMaleStudent
};
#import "Function.h"
// 1. 定义打印一个结构体变量,使用指针
void printStudent(Student *stu)
{
printf("%-5s %c %-3d %-3d %.2f\n", stu->name, stu->gender, stu->age, stu->number, stu->score);
}
// 2. 定义打印数组内的全部结构体变量,使用指针
void printStudents(Student *stus, int count)
{
for (int i = 0; i < count; i++) {
// .m文件中,函数可以互相调用
printStudent(stus + i);
}
printf("\n");
}
// 3.定义排序函数
void sortStudents(Student *stus, int count, CompareFunctionPointer cfp)
{
for (int i = 0; i < count - 1; i++) {
for (int j = 0; j < count - 1 - i; j++) {
if (cfp((stus + j), (stus + j + 1))) {
Student temp = *(stus + j);
*(stus + j) = *(stus + j + 1);
*(stus + j + 1) = temp;
}
}
}
}
// 4.定义姓名排序函数
BOOL compareNameByAsceding(Student *stu1, Student *stu2)
{
return strcmp(stu1->name, stu2->name) > 0;
}
BOOL compareNameByDesceding(Student *stu1, Student *stu2)
{
return strcmp(stu1->name, stu2->name) < 0;
}
// 5.定义编号排序函数
BOOL compareNumberByAsceding(Student *stu1, Student *stu2)
{
return stu1->number > stu2->number;
}
BOOL compareNumberByDesceding(Student *stu1, Student *stu2)
{
return stu1->number < stu2->number;
}
// 6.定义评分排序
BOOL compareScoreByAsceding(Student *stu1, Student *stu2)
{
return stu1->score > stu2->score;
}
BOOL compareScoreByDesceding(Student *stu1, Student *stu2)
{
return stu1->score < stu2->score;
}
// 7.定义打印男/女排序
void printMaleStudent(Student *stus, int count)
{
for (int i = 0; i < count; i++) {
if ((stus + i)->gender == 'f') {
printStudent(stus + i);
}
}
}
void printFemaleStudent(Student *stus, int count)
{
for (int i = 0; i < count; i++) {
if ((stus + i)->gender == 'm') {
printStudent(stus + i);
}
}
}
#import <Foundation/Foundation.h>
#import "Function.h"
void menu();
void menu()
{
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("0. 退出程序\n");
printf("--------------------\n");
printf("请输入序号:");
}
int main(int argc, const char * argv[])
{
Student stus[] = {
{"刘备", 'f', 28, 1001, 45.4},
{"赵婵", 'm', 39, 1002, 67.3},
{"张飞", 'f', 22, 1003, 54.7},
{"曹操", 'f', 45, 1004, 76.5},
{"大乔", 'm', 24, 1005, 75.2}
};
int count = sizeof(stus) / sizeof(Student);
while (YES) {
menu();
int index = 0;
scanf("%d", &index);
if (index == 0) {
printf("程序结束");
break;
}
switch (index) {
// 1. 姓名排序
case CompareNameByAsceding: {
sortStudents(stus, count, compareNameByAsceding);
printStudents(stus, count);
break;
}
case CompareNameByDesceding: {
sortStudents(stus, count, compareNameByDesceding);
printStudents(stus, count);
break;
}
// 2. 编号排序
case CompareNumberByAsceding: {
sortStudents(stus, count, compareNumberByAsceding);
printStudents(stus, count);
break;
}
case CompareNumberByDesceding: {
sortStudents(stus, count, compareNumberByDesceding);
printStudents(stus, count);
break;
}
// 3.评分排序
case CompareScoreByAsceding: {
sortStudents(stus, count, compareScoreByAsceding);
printStudents(stus, count);
break;
}
case CompareScoreByDesceding: {
sortStudents(stus, count, compareScoreByDesceding);
printStudents(stus, count);
break;
}
// 4.男 / 女
case PrintFemaleStudent: {
printFemaleStudent(stus, count);
break;
}
case PrintMaleStudent: {
printMaleStudent(stus, count);
break;
}
default: {
printf("您输入有误,请重新输入数字: \n");
}
break;
}
}
return 0;
}