iOS C语言11_函数指针
来源:互联网 发布:java字符串转日期类型 编辑:程序博客网 时间:2024/06/06 18:38
.h
//
// MyFunction.h
// C12_函数指针
//
// Created by dllo on 15/7/14.
// Copyright (c) 2015年 zhozhicheng. 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);
typedef int (*PFUN) (int,int);
// 让功能名和地址能关联起来,为他们两个写一个结构体,一个存名,一个存对应功能的地址
struct nameFunction{
char name[20]; //功能名
PFUN p; //对应功能地址
};
typedefstructnameFunction NameFunction;
//通过功能名,找到相应功能的函数地址,返回函数地址
//PFUN checkFunctionName (NameFunction name[].char str[]);
//
// MyFunction.m
// C12_函数指针
//
// Created by dllo on 15/7/14.
// Copyright (c) 2015年 zhozhicheng. 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 max =maxValue(a, b);
// int max=p(a,b);
// max=p(max,c);
// return max;
//}
//int maxValue3(int a ,int b,int c,int (*p)(int,int))
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) {
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(stu1.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;
}
//
// main.m
// C12_函数指针
//
// Created by dllo on 15/7/14.
// Copyright (c) 2015年 zhozhicheng. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "MyFunction.h"
// 比较两个数的大小,并且返回结果
//int maxValue(int a,int b){
// return a > b ? a : b;
//}
//void test1(int b,int c){
//
//}
//
//int sumNum(int x,int y){
// return x+y;
//}
//// 1-n 的和,在函数内打印结果
//
//void sum(int n){
// int count=0;
// for (int i=1;i<=n;i++ ){
// count=count+i;
// }printf("%d",count );
//}
int main(int argc,constchar * argv[]) {
// printf("%p\n",maxValue);
// // 定义一个函数指针变量
// int (*p)(int ,int ) = NULL;
// p = maxValue;
// printf("%p\n",p);
//
// // int *p = NULL;
//
//
// void(*p1)(int ,int) = NULL;
//
// // 通过函数指针进行函数的调用
// maxValue(10, 20);
//
// 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(20,10));
// break;
// case 2:
// p=sumNum;
// printf("%d\n",p(20,10));
// break;
//
//
// default:
// break;
// }
//
// 回调函数
//// int result=maxValue3(3, 6, 1, maxValue3);
//// printf("%d\n",result);
//
// Student stu1={18,70.5,'w',"zhangsan"};
// Student stu2={20,60,'m',"lisi"};
// Student stu3={19,95,'w',"wangwu"};
// Student stu4={21,85,'w',"shenliu"};
// 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 = sortByName;
// break;
// case 3:
// p = sortByScore;
// 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",sumNum};
// NameFunction name3={"min",sumNum};
// NameFunction name[3]={name1,name2,name3};
return 0;
}
- iOS C语言11_函数指针
- C语言_指针函数
- C语言_函数指针
- C语言_函数指针数组指针
- iOS C语言8_指针
- iOS C语言6_函数
- C语言_指针
- C语言_指针
- C语言_指针
- C语言_指针
- IOS--C 语言 Lesson Seven 指针函数
- c语言_函数指针作为函数参数的使用
- iOS C语言9_结构体指针
- C语言11 -- 函数指针
- 20170215C语言提升08_指针_02数组指针及指针数组及函数指针
- c语言_函数指针的调用方法
- C语言-指针_笔记
- C语言_高级指针
- POJ 1276 多重背包
- poj1925--Spiderman(dp)
- javascript获取文件后缀名
- docker 简单笔记
- Happy Great BG 精度控制
- iOS C语言11_函数指针
- linux中安装python科学计算环境-numpy、scipy、matplotlib、OpenCV...
- 队列类 - 链式存储
- sql左连接、右链接、内连接
- Algorithm(I)-week1
- break、return与continue的区别.
- 菜鸟自己也可以会写查询功能
- HDOJ 1097 A hard puzzle
- 关于Python在函数中接收元组和列表的理解