1、 函數指針 做參數
2、 回調過程
//
// main.m
// C_Project_12
//
// Created by on 15/3/26.
// Copyright (c) 2015年 . All rights reserved.
//
#import
//課堂練習題:寫一函數查找成績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;
}