返回值是函数指针

来源:互联网 发布:淘宝直通车与淘宝联盟 编辑:程序博客网 时间:2024/05/22 05:06


需求:若干学生,用户自由输入排序方法,达到排序目的

解题步骤:

1、创建学生结构体

 2、创建学生结果体数组并赋值;

3、分别定义按“姓名”、“成绩”、“年龄”等途径排序的函数;

4、定义一个输出函数,用来输出结构体数组;

5、定义一个“排序途径”类型的函数

6、定义一个“选择排序途径”的函数,该函数返回值为指向选定函数的指针;

7、定义一个“排序,并且交换结构体元素位置”函数,该函数无返回值,参数为“学生结构体数组”,“指向选定排序途径的指针”




main函数中:

1、定义一个字符串数组,用来存放用户输入;

2、将用户输入的字符放在“选择排序途径”的函数里,进行比对,并返会相应的函数名;

3、定义一个“排序途径”类型的函数指针,指向上一步返回的函数名;

4、利用“排序函数”将“学生结构体书组名”、“只想函数的指针”作为实参传递




////  main.m//  729_classexam////  Created by Quan.Zh. on 14-7-29.//  Copyright (c) 2014年 蓝鸥科技. All rights reserved.//#import <Foundation/Foundation.h>//创建学生结构体typedef struct{    char name[20];    int age;    float score;} Stu;//打印所有数组元素的函数void print(Stu *a, int count);void print(Stu *a, int count){    for (int i = 0; i < count; i++) {        printf("name = %s , age = %d , score = %.2f \n",(a+i)->name, (a+i)->age, (a+i)->score);    }}//4.1.1按照年龄排序BOOL ag(Stu *student1, Stu *student2);BOOL ag(Stu *student1, Stu *student2){    return (student1->age > student2->age);}//4.1.2按照姓名排序BOOL na(Stu *student1, Stu *student2);BOOL na(Stu *student1, Stu *student2){    return (strcmp(student1->name, student2->name) > 0);}//4.1.3按照年龄排序BOOL score(Stu *student1, Stu *student2);BOOL score(Stu *student1, Stu *student2){    return (student1->score > student2->score);}//给“排序类别”的函数类型起别名typedef BOOL (*useFunc)(Stu *, Stu *);//创建判断方法结构体typedef struct{    char name[10];    useFunc Func;} Name;Name list[3] = {    {"ag", ag},    {"score", score},    {"name", na}};//选择排序方法,并返回useFunc changeWay(char name[]);useFunc changeWay(char name[]){    for (int i = 0; i < 3; i++) {        if (strcmp((list+i)->name, name) == 0) {            return (list+i)->Func;        }        }    return list->Func;    }//4.根据##排序void age(Stu *a, int count, useFunc p);void age(Stu *a, int count, useFunc p){    for (int i = 0; i < count-1; i++) {        for (int j = 0; j < count - i - 1; j++) {            if (p((a+j),(a+j+1))) {                Stu b = *(a+j);                *(a+j) = *(a+j+1);                *(a+j+1) = b;            }        }    }    print(a, count);}int main(int argc, const char * argv[]){   //2.创建结构体数组    Stu student[]={        {"aa",34,98},        {"bb",26,57},        {"cc",23,78}    };    int n = sizeof(student)/sizeof(Stu);            //3.使用函数打印所有数组元素    printf("~~~~~~~~~~~~~~~打印数组元素~~~~~~~~~~~~~~~~~~~~~~\n");    print(student,n);                printf("============================\n");    printf("1. 根据姓名排序\n");    printf("2. 根据年龄排序\n");    printf("3. 根据分数排序\n");    printf("============================\n");           while (YES) {        char name[10]= {""};        scanf("%s",name);        useFunc p = changeWay(name);        age(student, n, p);    }        /*    int i = 0;    while (i < 3) {                int a = 0;        printf("please input a number:");        scanf("%d", &a);        useFunc p = name;                switch (a) {            case 1:            {printf("根据姓名排序\n");                p = name;                break;            }            case 2:            {printf("根据年龄排序\n");                p = ag;                break;            }            case 3:            {printf("根据分数排序\n");                p = score;                break;            }                            default:            {printf("输入3次有误,将退出程序\n");                ++i;                break;            }                        }            }    */    return 0;}


0 0
原创粉丝点击