#import <Foundation/Foundation.h>
//定义一个学生结构体
typedef struct student {
char name[20]; //姓名
int age; //年龄
float score; //成绩
}Student;
//按成绩排序
//如何把他们都变成参数类型相同
//解决方案:他们都属于结构体
BOOL sortStudentByAge(Student stu1, Student stu2)
{
return stu1.age > stu2.age;
}
//按成绩排序
BOOL sortStudentByScore(Student stu1, Student stu2)
{
return stu1.score > stu2.score;
}
//按姓名排序
BOOL sortStudentByName(Student stu1, Student stu2)
{
return strcmp(stu1.name, stu2.name) > 0;
}
//指针函数(回调)
typedef BOOL (*PStudent)(Student, Student);
//建立字符串和函数之间的一一对应关系
typedef struct nameFunctionPair {
char name[20]; //存储函数对应的字符串
PStudent function; //存储字符串对应函数的地址
}NameFunctionPair;
//根据给定的字符串查找匹配表,找出对应的函数
//name 用来接收用来匹配的字符串
//p 用来接受匹配表
//count 用来接受匹配表中元素的个数
PStudent getFunctionByName(char *name, NameFunctionPair *p, int count)
{
for (int i = 0; i < count; i++) {
if (strcmp((p + i)->name, name) == 0) {
//如果匹配到对应的函数,将函数地址返回.
return (p + i)->function;
}
}
//如果没有匹配到对应的函数,就返回NULL
return NULL;
}
//三个函数之间唯一的不同就在于 冒泡排序中的判断条件不同
void sortStudent(Student *stu, int count, char *name, NameFunctionPair *pair, int number)
{
//接受一下匹配到函数的地址
PStudent function = getFunctionByName(name, pair, number);
for (int i = 0; i < count - 1; i++) {
for (int j = 0; j < count - 1 - i; j++) {
if (function(*(stu + j), *(stu + j + 1))) {
Student temp = *(stu + j);
*(stu + j) = *(stu + j + 1);
*(stu + j + 1) =temp;
}
}
}
}
1.按姓名升序排列
//void sortStudentByName(Student *stu, int count);
//void sortStudentByName(Student *stu, int count)
//{
// for (int i = 0; i < count - 1; i++) {
// for (int j = 0; j < count - 1 - i; j++) {
// if (strcmp((stu + j)->name, (stu + j + 1)->name) > 0) {
// Student temp = *(stu + j);
// *(stu + j) = *(stu + j + 1);
// *(stu + j + 1) =temp;
// }
// }
// }
//}
2.按年龄升序排序
//void sortStudentByAge(Student *stu, int count);
//void sortStudentByAge(Student *stu, int count)
//{
// for (int i = 0; i < count - 1; i++) {
// for (int j = 0; j < count - 1 - i; j++) {
// if ((stu + j)->age > (stu + j + 1)->age) {
// Student temp = *(stu + j);
// *(stu + j) = *(stu + j + 1);
// *(stu + j + 1) =temp;
// }
// }
// }
//}
3.按成绩升序排序
//void sortStudentByScore(Student *stu, int count);
//void sortStudentByScore(Student *stu, int count)
//{
// for (int i = 0; i < count - 1; i++) {
// for (int j = 0; j < count - 1 - i; j++) {
// if ((stu + j)->score > (stu + j + 1)->score) {
// Student temp = *(stu + j);
// *(stu + j) = *(stu + j + 1);
// *(stu + j + 1) =temp;
// }
// }
// }
//}
//输出函数
void printStudent(Student *stu, int count)
{
for (int i = 0; i < count; i++) {
printf("name:%s, age:%d, score:%.2f\n", (stu+ i)->name, (stu+ i)->age, (stu+ i)->score);
}
}
int main(int argc, const char * argv[])
{
//学生结构体,姓名,年龄,成绩
Student stu[5] = {
{"xiaoguang", 20, 90},
{"xiaoming", 19, 80},
{"xiaomeng", 22, 89},
{"xiaofang", 20, 90},
{"xiaofei", 21, 88}
};
//sortStudentByName(stu, 5);
// sortStudentByAge(stu, 5);
// sortStudentByScore(stu, 5);
//创建匹配表
NameFunctionPair pair[3] = {
{"name", sortStudentByName},
{"age", sortStudentByAge},
{"score", sortStudentByScore}
};
char tempName[20] = {0}; //用来接受从控制台输入的字符串
printf("请输入排序的方式(name, age, score):\n");
scanf("%s", tempName);
//对学生排序
sortStudent(stu, 5, tempName, pair, 3);
printStudent(stu, 5);
return 0;
}
回调函数(求两个数的最大值,最小值,和)
最新推荐文章于 2022-11-13 20:31:29 发布