main.m:
#import
<Foundation/Foundation.h>
#import
"Function.h"
int main( int argc, const char * argv[]) {
// 函数指针:指向函数的指针
// 函数的名字 是一个地址 是一个常量地址
// 定义函数指针步骤 :
// 1 、把要指向的函数声明复制过来 把函数名删了
// int num(int a, int b) ;
// 2 、把原来函数名的位置替换成 (*) (此时该式子就是函数指针的类型)
// int (*)(int a, int b);
// 3 、给函数指针起一个名字
// int (* 函数指针名 )(int a, int b) = NUL;
int (*p)( int a, int b) = NULL ;
// 注意: 函数指针只能指向同类型的函数
//
定义函数指针
指向
求最大值函数
// 定义函数指针的时候 参数的名字可以省略
int (*p1)( int a, int b) = NULL ;
p1 = maxValue ;
// 可以用函数指针的名字 代替函数名来调用函数
printf ( "max = %d\n" ,p1( 3 , 5 ));
void (*ppH)() = NULL ;
ppH = printHello ;
// 无参数调用时 也要加括号
// 定义函数指针的时候 参数的名字可以省略
int (*p1)( int a, int b) = NULL ;
p1 = maxValue ;
// 可以用函数指针的名字 代替函数名来调用函数
printf ( "max = %d\n" ,p1( 3 , 5 ));
void (*ppH)() = NULL ;
ppH = printHello ;
// 无参数调用时 也要加括号
ppH();
while
(p1 ==
NULL
) {
printf ( " 输入 max 或者 sum : \n" );
char str[ 20 ] = { 0 };
scanf ( "%s" ,str);
if ( strcmp (str, "max" ) == 0 ) {
p1 = maxValue ;
printf ( "max = %d\n" ,p1( 3 , 5 ));
getValue ( 3 , 5 , p1);
} else if ( strcmp (str, "sum" ) == 0 ){
p1 = sumValue ;
printf ( "sum = %d\n" ,p1( 3 , 5 ));
} else {
printf ( " 输入错误 \n" );
}
printf ( " 输入 max 或者 sum : \n" );
char str[ 20 ] = { 0 };
scanf ( "%s" ,str);
if ( strcmp (str, "max" ) == 0 ) {
p1 = maxValue ;
printf ( "max = %d\n" ,p1( 3 , 5 ));
getValue ( 3 , 5 , p1);
} else if ( strcmp (str, "sum" ) == 0 ){
p1 = sumValue ;
printf ( "sum = %d\n" ,p1( 3 , 5 ));
} else {
printf ( " 输入错误 \n" );
}
}
Student stu1 = { "dj" , 23 , 90 , 1111 };
Student stu2 = { "mtt" , 24 , 84 , 3333 };
Student stu3 = { "cqq" , 22 , 56 , 2222 };
Student stu4 = { "zp" , 25 , 97 , 5555 };
Student stu5 = { "xm" , 24 , 66 , 4444 };
Student stu[5] = {stu1, stu2, stu3, stu4, stu5};
Student *st =stu;
// sortByAge(st, 5);
// sortByScore(st, 5);
// sortByNumber(st, 5);
//
按年龄排序
// SORT p = NULL;
// p = compareAge;
//
调用排序
// sortStudent(st, 5, p);
// ADDSTR a = addStr;
// findStudentByScore( st, 5, a);
//
最终版
while ( 1 ) {
printf ( " 输入排序条件 age/score/name/number : \n" );
char str[ 20 ] = { 0 };
scanf ( "%s" ,str);
// 根据输入查找对应 函数地址
SORT p = getScanf (str);
if (p != NULL ){
sortStudent (st, 5 , p);
} else {
printf ( " 你傻爆了 " );
}
}
return 0 ;
while ( 1 ) {
printf ( " 输入排序条件 age/score/name/number : \n" );
char str[ 20 ] = { 0 };
scanf ( "%s" ,str);
// 根据输入查找对应 函数地址
SORT p = getScanf (str);
if (p != NULL ){
sortStudent (st, 5 , p);
} else {
printf ( " 你傻爆了 " );
}
}
return 0 ;
}
Function.h :
#import <Foundation/Foundation.h>
// 两个整型数和的函数
int sumValue(int a, int b);
// 两个整型数比较大小 返回大的
int maxValue(int a, int b);
void printHello();
int sumValue(int a, int b);
// 两个整型数比较大小 返回大的
int maxValue(int a, int b);
void printHello();
// 指针函数 作为参数的 函数
int getValue(int a,int b, int (*p)(int,int));
struct Student{
char name[20];
int age;
float score;
int number;
};
typedef struct Student Student;
int getValue(int a,int b, int (*p)(int,int));
struct Student{
char name[20];
int age;
float score;
int number;
};
typedef struct Student Student;
void printStu(Student stu);
void printStudents(Student *p, int count);
void sortByAge(Student *p, int count);
void sortByScore(Student *p, int count);
void sortByNumber(Student *p, int count);
void printStudents(Student *p, int count);
void sortByAge(Student *p, int count);
void sortByScore(Student *p, int count);
void sortByNumber(Student *p, int count);
// 比较两个学生的分数
BOOL compareScore(Student stu1,Student stu2);
BOOL compareAge(Student stu1,Student stu2);
BOOL compareNumber(Student stu1,Student stu2);
BOOL compareName(Student stu1,Student stu2);
// 给函数指针的类型起别名
// 新的名称起在 * 的后面
typedef BOOL (*SORT)(Student stu1,Student stu2) ;
// 新的排序方法 参数是函数指针
void sortStudent(Student *p,int count,SORT s);
// 添加 “高富帅”
void addStr(char *name);
typedef void (*ADDSTR)(char *name);
// 查找90分以上的
void findStudentByScore(Student *stu, int count, ADDSTR add);
// 声明结构体 构建对应关系
struct Kind{
// 查找时输入的名字
char fName[20];
// 对应的函数的地址
SORT s;
};
typedef struct Kind Kind;
BOOL compareScore(Student stu1,Student stu2);
BOOL compareAge(Student stu1,Student stu2);
BOOL compareNumber(Student stu1,Student stu2);
BOOL compareName(Student stu1,Student stu2);
// 给函数指针的类型起别名
// 新的名称起在 * 的后面
typedef BOOL (*SORT)(Student stu1,Student stu2) ;
// 新的排序方法 参数是函数指针
void sortStudent(Student *p,int count,SORT s);
// 添加 “高富帅”
void addStr(char *name);
typedef void (*ADDSTR)(char *name);
// 查找90分以上的
void findStudentByScore(Student *stu, int count, ADDSTR add);
// 声明结构体 构建对应关系
struct Kind{
// 查找时输入的名字
char fName[20];
// 对应的函数的地址
SORT s;
};
typedef struct Kind Kind;
// 根据用户输入的排序方式查找对应的函数地址, 并返回
SORT
getScanf(
char
*name);
Function.m :
#import "Function.h"
int sumValue(int a, int b){
return a + b;
}
int maxValue(int a, int b){
return a > b ? a : b;
}
void printHello(){
printf("hello world!\n");
}
int getValue(int a,int b, int (*p)(int,int)){
return p( a, b);
}
void printStu(Student stu){
printf("%s %d %.2f %d\n", stu.name, stu.age, stu.score, stu.number);
}
void printStudents(Student *p, int count){
for (int i = 0; i < count; i++) {
printStu(*(p + i));
}
printf("\n");
}
void sortByAge(Student *p, int count){
for (int i = 0; i < count - 1; i++) {
for (int j = 0; j < count - 1 -i; j++) {
if ((p + j)->age > (p + j + 1)->age) {
Student stu = *(p + j);
*(p + j) = *(p + j + 1);
*(p + j + 1) = stu;
}
}
}
printStudents(p, count);
}
void sortByScore(Student *p, int count){
for (int i = 0; i < count - 1; i++) {
for (int j = 0; j < count - 1 -i; j++) {
if ((p + j)->score > (p + j + 1)->score) {
Student stu = *(p + j);
*(p + j) = *(p + j + 1);
*(p + j + 1) = stu;
}
}
}
printStudents(p, count);
}
void sortByNumber(Student *p, int count){
for (int i = 0; i < count - 1; i++) {
for (int j = 0; j < count - 1 -i; j++) {
if ((p + j)->number > (p + j + 1)->number) {
Student stu = *(p + j);
*(p + j) = *(p + j + 1);
*(p + j + 1) = stu;
}
}
}
printStudents(p, count);
}
BOOL compareScore(Student stu1,Student stu2){
return stu1.score > stu2.score;
}
BOOL compareAge(Student stu1,Student stu2){
return stu1.age > stu2.age;
}
BOOL compareNumber(Student stu1,Student stu2){
return stu1.number > stu2.number;
}
BOOL compareName(Student stu1,Student stu2){
return strcmp(stu1.name, stu2.name) > 0;
}
void sortStudent(Student *p,int count,SORT s){
for (int i = 0; i < count - 1; i++) {
for (int j = 0; j < count - 1 -i; j++) {
// 条件选择器
if ( s(*(p + j),*(p + j + 1)) ){
Student stu = *(p + j);
*(p + j) = *(p + j + 1);
*(p + j + 1) = stu;
}
}
}
printStudents(p, count);
}
void addStr(char *name){
printf("%s\n", strcat(name, " 高富帅"));
}
void findStudentByScore(Student *stu, int count, ADDSTR addS){
for (int i = 0; i < count; i++) {
if (stu[i].score >= 90) {
addS(stu[i].name);
}
}
}
SORT getScanf(char *name){
Kind k1 = {"name", compareName};
Kind k2 = {"age", compareAge};
Kind k3 = {"score", compareScore};
Kind k4 = {"number", compareNumber};
Kind list[4]= {k1, k2, k3, k4};
// 遍历数组 查找名字一样的返回地址
for (int i = 0; i < 4; i++) {
if (strcmp(list[i].fName, name) == 0) {
return list[i].s;
}
}
return NULL;
int sumValue(int a, int b){
return a + b;
}
int maxValue(int a, int b){
return a > b ? a : b;
}
void printHello(){
printf("hello world!\n");
}
int getValue(int a,int b, int (*p)(int,int)){
return p( a, b);
}
void printStu(Student stu){
printf("%s %d %.2f %d\n", stu.name, stu.age, stu.score, stu.number);
}
void printStudents(Student *p, int count){
for (int i = 0; i < count; i++) {
printStu(*(p + i));
}
printf("\n");
}
void sortByAge(Student *p, int count){
for (int i = 0; i < count - 1; i++) {
for (int j = 0; j < count - 1 -i; j++) {
if ((p + j)->age > (p + j + 1)->age) {
Student stu = *(p + j);
*(p + j) = *(p + j + 1);
*(p + j + 1) = stu;
}
}
}
printStudents(p, count);
}
void sortByScore(Student *p, int count){
for (int i = 0; i < count - 1; i++) {
for (int j = 0; j < count - 1 -i; j++) {
if ((p + j)->score > (p + j + 1)->score) {
Student stu = *(p + j);
*(p + j) = *(p + j + 1);
*(p + j + 1) = stu;
}
}
}
printStudents(p, count);
}
void sortByNumber(Student *p, int count){
for (int i = 0; i < count - 1; i++) {
for (int j = 0; j < count - 1 -i; j++) {
if ((p + j)->number > (p + j + 1)->number) {
Student stu = *(p + j);
*(p + j) = *(p + j + 1);
*(p + j + 1) = stu;
}
}
}
printStudents(p, count);
}
BOOL compareScore(Student stu1,Student stu2){
return stu1.score > stu2.score;
}
BOOL compareAge(Student stu1,Student stu2){
return stu1.age > stu2.age;
}
BOOL compareNumber(Student stu1,Student stu2){
return stu1.number > stu2.number;
}
BOOL compareName(Student stu1,Student stu2){
return strcmp(stu1.name, stu2.name) > 0;
}
void sortStudent(Student *p,int count,SORT s){
for (int i = 0; i < count - 1; i++) {
for (int j = 0; j < count - 1 -i; j++) {
// 条件选择器
if ( s(*(p + j),*(p + j + 1)) ){
Student stu = *(p + j);
*(p + j) = *(p + j + 1);
*(p + j + 1) = stu;
}
}
}
printStudents(p, count);
}
void addStr(char *name){
printf("%s\n", strcat(name, " 高富帅"));
}
void findStudentByScore(Student *stu, int count, ADDSTR addS){
for (int i = 0; i < count; i++) {
if (stu[i].score >= 90) {
addS(stu[i].name);
}
}
}
SORT getScanf(char *name){
Kind k1 = {"name", compareName};
Kind k2 = {"age", compareAge};
Kind k3 = {"score", compareScore};
Kind k4 = {"number", compareNumber};
Kind list[4]= {k1, k2, k3, k4};
// 遍历数组 查找名字一样的返回地址
for (int i = 0; i < 4; i++) {
if (strcmp(list[i].fName, name) == 0) {
return list[i].s;
}
}
return NULL;
}