比较两个数的大小,并返回结果
//int maxValue(int a , int b){
// return a>b?a:b;
//}
//
//int sumNum(int x,int y){
// return x + y;
//}
//
//void test(int a, int b){
//
//}
1- n 的和,在函数内打印
//void sum(int n){
// int count =0;
// for (int i =1; i<n+1; i++) {
// count +=i;
// }
// printf("%d\n",count);
//}
int main(int argc, const char * argv[]) {
//
// Student stu1 = {20,15 ,'w', "zhangsan"};
// Student stu2 = {21,26, 'm', "lisi"};
// Student stu3 = {22,17, 'w', "wangwu"};
// Student *stu =calloc(3, sizeof(Student));
// stu[0]=stu1;
// stu[1]=stu2;
// stu[2]=stu3;
// printf("%p\n",maxValue);
// 定义一个函数指针
// int (*p)(int ,int ) = NULL;// 原函数:int maxValue(int a, int b);
// int *p = NULL;
//对函数指针进行赋值
// p = maxValue;
//
// // 打印地址所保存的地址
// printf("%p\n",p);
//
void (*p1)(int , int) = NULL;
//
// // 通过函数指针进行函数的调用
maxValue(10,20);
// printf("%d\n", p(10,20));
//
// p =sumNum;
// printf("%d\n", p(10,20));
//
// void (*p)(int ) = sum ;
//
// p(8);
// 键盘输入数字,要求实现功能,1是求最大数值,2是求和,用函数指针来完成调用
// int enterNum = 0;
// scanf("%d",&enterNum);
//
// int (*p)(int, int) = NULL;
// switch (enterNum) {
// case 1:
// p = maxValue;
// printf("%d\n",p(29, 10));
// break;
// case 2:
// p = sumNum;
// printf("%d\n",p(29,10));
// break;
// default:
// break;
// }
// 回调函数
// int result = maxValue3(3,6,1 ,maxValue);
// printf("%d\n",result);
// char str[] ="帅";
// printf("%ld",sizeof(str));
// Student stu1 ={18 , 70.5 , 'w',"liushanshan"};
// Student stu2 ={20 , 60 ,'w', "yanglin"};
// Student stu3 ={21 , 95 ,'m',"zhangsan" };
// Student stu4 ={19 ,91 ,'w', "lisi"};
// Student stu[4] ={stu1,stu2,stu3,stu4};
//
//
// Student newStu = changeName(stu1);
// printf("%s\n",newStu.stuName);
// checkStudent(stu, changeName);
// printf("请输入排序方式,1是年龄,2是成绩,3是姓名\n");
// int enterNum = 0;
// scanf("%d",&enterNum);
// // 定义函数指针的变量
// FUN p =NULL;
// switch (enterNum) {
// case 1:
// p =sortByAge;
//
// break;
// case 2:
// p = sortByScore;
// case 3:
// p = sortByName;
// break;
// default:
// printf("输入错误\n");
// break;
// }
// bubbleSort(stu, 4, p);
// for (int i =0; i <4 ; i++) {
// printf("%s\n",stu[i].stuName);
// }
//
// NameFunction name1 ={"sum", sumNum};
// NameFunction name2 ={"mul", mulNum};
// NameFunction name3 ={"min", minNum};
// NameFunction name[3]={name1,name2,name3};
//
// // 调用一下下
// PFUN p = checkFunctionName(name,"sum");
// if (p == NULL) {
// printf("功能不对\n");
// }else{
// printf("%d\n",p(3,5));
// }
return 0;
}
. h函数
// Myfunction.h
// C12_函数指针
//
// Created by dllo on 15/7/14.
// Copyright (c) 2015年 cml. All rights reserved.
//
#import <Foundation/Foundation.h>
// 找到两个数中最大值
//int maxValue(int a,int b );
//
三个数最大值
函数调回时候,需要把调用的函数作为第四个参数,然后针对地址进行调用
//int maxValue3(int a,int b,int c ,int (*p)(int,int));
四个数中最大值
//
声明学生类型的结构体
//
//struct student{
// int stuAge;
// float stuScore;
//
// char stuSex;
// char stuName[50];
//};
//typedef struct student Student;
//
回调部分的函数
//
//Student changeName(Student stu);
//
用来查找90分以上的学生,满足条件的调用改名的函数进行名的拼接
//
//void checkStudent(Student stu[],Student (*p)(Student ));
//
动态排序
//
先写三个排序条件
//
按照年龄排序
//BOOL sortByAge(Student stu1,Student stu2);
分数
//BOOL sortByScore(Student stu1,Student stu2);
姓名
//BOOL sortByName(Student stu1,Student stu2);
//
//
//typedef BOOL(*FUN)(Student,Student) ;
//void bubbleSort(Student stu[],int count ,FUN p);
// 函数的返回值是函数指针
int sumNum(int a, int b);
int mulNum(int a, int b);
int minNum(int a, int b);
// PFUN是新类型
typedef int(*PFUN) (int ,int );
// 让功能名和地址能关联起来,为他们下一个结构体,一个存名,一个存对应功能的地址
struct nameFunction{
char name[20]; // 功能名
PFUN p; // 对应的功能名地址
};
typedef struct nameFunction NameFunction;
// 通过功能的名,找到对应功能的函数地址,返回函数地址
PFUN checkFunctionName(NameFunction name[],char str[]);
.m函数
// Myfunction.m
// C12_函数指针
//
// Created by dllo on 15/7/14.
// Copyright (c) 2015年 cml. All rights reserved.
//
#import "Myfunction.h"
找到两个数中最大值
//int maxValue(int a,int b ){
// return a>b?a:b;
//}
//
//
//
//int maxValue3(int a,int b,int c ,int (*p)(int,int)){
//
//
函数的嵌套调用
//
int max =maxValue(a, b);
回调函数
// int max = p(a,b);
// max =p(max ,c);
// return max;
//
//}
//
//Student changeName(Student stu){
// // 字符串的拼接
// strcat(stu.stuName, "高富帅");
// return stu;
//
//}
//
//void checkStudent(Student stu[],Student (*p)(Student )){
//
// // 进行for循环的遍历
// for (int i =0; i<4; i++) {
// if (stu[i].stuScore>=90) {
// p(stu[i]);
// stu[i ] = p(stu[i]);
// // 打印一下新的学生姓名
// printf("%s\n",stu[i].stuName);
// }
// }
//
//
//}
按照年龄排序
//BOOL sortByAge(Student stu1,Student stu2){
//
//
//
// return stu1.stuAge>stu2.stuAge?YES:NO;
//}
//
//
分数
//BOOL sortByScore(Student stu1,Student stu2){
// return stu1.stuScore>stu2.stuScore?YES:NO;
//
//}
//
//
姓名
//BOOL sortByName(Student stu1,Student stu2){
// return strcmp(stu2.stuName, stu2.stuName)>0?YES:NO;
//}
//
//
//void bubbleSort(Student stu[],int count ,FUN p){
// for (int i =0; i<count -1; i++) {
// for (int j =0; j< count -1-i; j++) {
// // 指定排序依据
// if (p(stu[j],stu[j + 1])) {
// // 交换
// Student temp =stu[j];
// stu[j]=stu[j+1];
// stu[j+1]=temp;
//
// }
// }
// }
//}
int sumNum(int a, int b){
return a+b;
}
int mulNum(int a, int b){
return a*b;
}
int minNum(int a, int b){
return a-b;
}
PFUN checkFunctionName(NameFunction name[],char str[]){
for (int i=0; i<3; i++ ) {
if (strcmp(name[i].name,str)==0) {
return name[i].p;
}
}
return NULL;
}