• ios函数指针


    //
    //  main.m
    //  LessionFunctionPointer
    //
    //  Created by laouhn on 15/7/29.
    //  Copyright (c) 2015年 池海涛. All rights reserved.
    //
    
    #import <Foundation/Foundation.h>
    #import "Function.h"
    int main(int argc, const char * argv[]) {
    //
    //    int a = 10;
    //    int *p = &a;
    //    printf("%d
    ", *p);
        
        //定义结构体指针,指向结构体变量
        //结构体中包含,姓名,性别,分数,学号,新建文件实现结构体申明
    //    
    //    Student stu1 = {"KongKong",'w',19,99,"1234322"};
    //    Student *sp = &stu1;
    //    printf("name = %s,gender = %c,age = %d,score = %f,num = %s",sp->name,sp->Sex,sp->age,sp->score,sp->studentNum);
        
        
        
        
        
        
    //   int b = maxCommon(6, 9);
    //    printf("%d",b);
    //    printf("%d",minCommon(6, 9));
        /**
         *  函数指针
         //函数指针
         //函数在代码区
         */
        //int (*p)(int a, int b) = maxCommon; //赋初值 maxCommon 注意,不加();
        
        //使用函数指针调用函数
        //int value = p(6,8);
       // printf("value = %d", value);
        
      //  typedef int (*PFUN)(int a, int b);
        
        
        //PFUN q = maxCommon;
        
        
        //printf("value = %d", q(3,5));
        
        
        
        //重定义一个无参函数
    //    typedef void (*PPRINT)();
    //    
    //    PPRINT q = printHollo;
    //
    //    q();
    
         
         typedef int (*PFUN)(int a, int b);
         printf("请输入数字 : 1---maxValue,2-----minValue,3---sumValue,4----subValue");
         
         int a = 0;
         scanf("%d",&a);
         PFUN p = NULL;
         switch (a) {
         case 1:
         p = bigNum;
         break;
         case 2:
         p = littleNum;
         break;
         case 3:
         p = sumNum;
         break;
         case 4:
         p = cutNum;
         break;
         default:
         break;
         }
         int result = p(3,5);
         printf("result == %d 
    ",result);
        //定义结构体数组,包含 5 个元素
        Student stu[] = {
            {"kongKong",'w',27,99,"1234321"},
            {"liming",'m',14,88,"432142314"},
            {"zhangxin",'w',19,85,"223442113"},
            {"goushen",'m',21,93,"343214"},
            {"doubi",'w',45,44,"64214321"}
        };
        printStudentArray(stu,5);
        
        //sortByNum(stu,5);
       // sortByAge1(stu,5);
        printf("-------------------
    ");
       // findStudentByScore(stu,5,addStr);
       // printStudentArray(stu,5);
        //函数回调
        //函数指针作为函数参数
        //int value = getValue(3, 5, maxCommon);
       // printf("%d",value);
        //findStudentByScore(stu,5,addStr);
        sortStudentArray(stu,5,compareByName);
        printStudentArray(stu,5);
        
    //        printf("-------------------
    ");
    //    sortStudentArray(stu,5,compareByNum);
    //    printStudentArray(stu,5);
        return 0;
    }

    //Function.h

    //
    //  Function.h
    //  LessionFunctionPointer
    //
    //  Created by laouhn on 15/7/29.
    //  Copyright (c) 2015年 池海涛. All rights reserved.
    //
    
    #import <Foundation/Foundation.h>
    
    
    struct student{
        char name[20];
        char Sex;
        int age;
        float score;
        char *studentNum;
    
    };
    typedef struct student Student;
    
    //函数描述:返回值为整形,有两个整形参数的函数 ,名字是max
    //函数类型:int(int a,int b)---可简写为int(int,int)
    //函数指针类型:int (*)(int a, int b)
    //求两个数最大值
    int bigNum(int a, int b);
    //求两个数的最小值
    int littleNum(int, int);
    //求两个数的和
    int sumNum(int, int);
    //求两个数的差
    int cutNum(int, int);
    //求两个数的乘积
    int  productNum(int, int);
    //求两个数的商
    int divide(int, int);
    //求两个数的最大公约数
    int maxCommon(int, int);
    //求两个数的最小公倍数
    int minCommon(int, int);
    
    void printHollo();
    
    void printStudentArray(Student *,int);
    //姓名升序
    void sortByName(Student *,int);
    //年龄降序
    void sortByAge(Student *,int);
    //分数升序
    void sortByScore(Student *,int);
    //学号降序
    void sortByNum(Student *,int);
    
    
    void sortByAge1(Student[], int);
    typedef int (*PFUN)(int a, int b);
    int getValue(int a,int b, PFUN p);
    
    
    
    void addStr(char *);
    typedef void (*FADD)(char *);
    void findStudentByScore(Student stu[],int count,void (*p)(char *));
    
    typedef BOOL (*SORT)(Student,Student);
    
    //姓名比较,升序
    BOOL compareByName(Student stu1,Student stu2);
    //年龄比较,降序
    BOOL compareByAge(Student stu1,Student stu2);
    //分数比较,升序
    BOOL compareByScore(Student stu1,Student stu2);
    //学号比较,降序
    BOOL compareByNum(Student stu1,Student stu2);
    
    //结构体数组排序
    
    void sortStudentArray(Student stu[],int count,SORT sort);

    //Function.m

    //
    //  Function.m
    //  LessionFunctionPointer
    //
    //  Created by laouhn on 15/7/29.
    //  Copyright (c) 2015年 池海涛. All rights reserved.
    //
    
    #import "Function.h"
    
    //求两个数最大值
    int bigNum(int a, int b)
    {
        return a > b ? a : b;
    }
    //求两个数的最小值
    int littleNum(int a, int b)
    {
        return a > b ? b : a;
    }
    //求两个数的和
    int sumNum(int a, int b)
    {
        return a + b;
    }
    //求两个数的差
    int cutNum(int a, int b)
    {
        return a - b;
    }
    //求两个数的乘积
    int  productNum(int a, int b)
    {
        return a * b;
    }
    //求两个数的商
    int divide(int a, int b)
    {
        return a / b;
    }
    //求两个数的最大公约数
    int maxCommon(int a, int b)
    {
        // 9 6
        int temp = a % b ;
        while (temp != 0) {
            
            a = b;
            b = temp;
            temp = a % b;
        }
        return b;
    }
    //求两个数的最小公倍数
    int minCommon(int a, int b)
    {
       int temp = maxCommon(a,b);
        return (a * b) / temp;
    }
    
    
    void printHollo()
    {
        printf("这是弄啥呢");
    }
    
    
    
    void printStudentArray(Student *sp,int count)
    {
        for (int i = 0; i < count; i++) {
            printf("name = %s,gender = %c,age = %d,score = %f,num = %s 
    ",(sp + i)->name,(sp + i)->Sex,(sp + i)->age,(sp + i)->score,(sp + i)->studentNum);
        }
    
    }
    
    
    void sortByName(Student *a,int b)
    {
        for (int i = 0; i < b - 1; i++) {
            for (int j = 0; j < b - 1 - i; j++) {
                if (strcmp((a + j)->name, (a + j + 1)->name) > 0) {
                    Student temp = *(a + j);
                    *(a + j) = *(a + j + 1);
                    *(a + j + 1) = temp;
                }
            }
        }
    }
    //年龄降序
    void sortByAge(Student *a,int b)
    {
        for (int i = 0; i < b - 1; i++) {
            for (int j = 0; j < b - 1 - i; j++) {
                if ((a + j)->age < (a + j+ 1)->age) {
                    Student temp = *(a + j);
                    *(a + j) = *(a + j + 1);
                    *(a + j + 1) = temp;
                }
    
            }
        }
    }
    void sortByAge1(Student str[], int a){
        for (int i = 0; i < a - 1; i++) {
            for (int j = 0; j < a - 1 - i; j++) {
                if (str[j].age < str[j + 1].age) {
                    Student temp = str[j];
                    str[j] = str[j + 1];
                    str[j + 1] = temp;
                }
            }
        }
    }
    //分数升序
    void sortByScore(Student *a,int b)
    {
        for (int i = 0; i < b - 1; i++) {
            for (int j = 0; j < b - 1 - i; j++) {
                if ((a + j)->name > (a + j + 1)->name) {
                    Student temp = *(a + j);
                    *(a + j) = *(a + j + 1);
                    *(a + j + 1) = temp;
                }
    
            }
        }
    }
    //学号降序
    void sortByNum(Student *a,int b)
    {
        for (int i = 0; i < b - 1; i++) {
            for (int j = 0; j < b - 1 - i; j++) {
                if (strcmp((a + j)->studentNum, (a + j + 1)->studentNum) < 0) {
                    Student temp = *(a + j);
                    *(a + j) = *(a + j + 1);
                    *(a + j + 1) = temp;
                }
    
            }
        }
    }
    
    
    
    int getValue(int a,int b, PFUN p)
    {
        return p(a,b);
    }
    void addStr(char *name)
    {
        //*b = *b + "高富帅";
        //strcat(b,"高富帅");
        strcat(name, "高富帅");
    }
    void findStudentByScore(Student stu[],int count,void (*p)(char *))
    {
        for (int i = 0; i < count; i++) {
            if (stu[i].score >= 90) {
                p(stu[i].name);
            }
        }
    }
    
    
    
    //姓名比较,升序
    BOOL compareByName(Student stu1,Student stu2)
    {
        return  strcmp(stu1.name, stu2.name) > 0 ? YES : NO;
    }
    //年龄比较,降序
    BOOL compareByAge(Student stu1,Student stu2)
    {
        return stu1.age < stu2.age ? YES : NO;
    }
    //分数比较,升序
    BOOL compareByScore(Student stu1,Student stu2)
    {
        return stu1.score > stu2.score ? YES : NO;
    }
    //学号比较,降序
    BOOL compareByNum(Student stu1,Student stu2)
    {
    return  strcmp(stu1.studentNum, stu2.studentNum) < 0 ? YES : NO;
    }
    
    //结构体数组排序
    
    void sortStudentArray(Student stu[],int count,SORT sort)
    {
        for (int i = 0; i < count - 1; i++) {
            for (int j = 0; j < count - 1 - i; j++) {
                if (sort(stu[j], stu[j + 1])) {
                    Student temp = stu[j];
                    stu[j] = stu[j + 1];
                    stu[j + 1] = temp;
                }
            }
        }
    }
  • 相关阅读:
    Splay专题总结
    UVa12657
    ZOJ3772
    POJ1743
    高斯消元模板
    python使用chrom登陆微博
    mysql常用数据库(表)命令
    mysql索引
    mysql建表的时候,时间戳的选用
    php 金额每三位添加一个逗号
  • 原文地址:https://www.cnblogs.com/wohaoxue/p/4688114.html
Copyright © 2020-2023  润新知