回调函数
1、 函数指针 做参数
2、 回调过程
例代码
//
// main.m
// C_Project_12
//
// Created by on 15/3/26.
// Copyright (c) 2015年 . All rights reserved.
//
#import <Foundation/Foundation.h>
//课堂练习题:写一函数查找成绩90分以上的学员,使?用回调函数在姓名后加”?高富 帅”。
//1.定义结构体类型
typedef struct student {
char name[20];
float score;
} Student;
Student *generateStudentsInfo(int count);
Student *generateStudentsInfo(int count) {
Student *stus = malloc(sizeof(Student) * count);
for (int i = 0; i < count; i++) {
printf("请输入第%d个学生的信息\n", i + 1);
printf("姓名:");
scanf("%s", (stus + i)->name);
printf("成绩:");
scanf("%f", &(stus + i)->score);
}
return stus;
}
void printStudentsInfo(Student *stus, int count);
void printStudentsInfo(Student *stus, int count) {
printf("\n---------------------------\n");
for (int i = 0; i < count; i++) {
printf("姓名:%s\t\t\t\t成绩:%.2f\n", (stus + i)->name, (stus + i)->score);
}
printf("\n---------------------------\n");
}
void modifyName(char *name);
void modifyName(char *name) {
strcat(name, "-高富帅");
}
void searchStudentInfo(Student *stus, int count, float score, void (*point)(char *));
void searchStudentInfo(Student *stus, int count, float score, void (*point)(char *)) {
for (int i = 0; i < count; i++) {
if ((stus + i)->score > score) {
point((stus + i)->name);
}
}
}
int main(int argc, const char * argv[]) {
Student *stus = generateStudentsInfo(2);
printStudentsInfo(stus, 2);
searchStudentInfo(stus, 2, 90, modifyName);
printStudentsInfo(stus, 2);
return 0;
}
时间: 2024-10-04 22:51:54