C语言回调函数实例
来源:互联网 发布:期货看哪个网站的数据 编辑:程序博客网 时间:2024/05/17 03:35
今天刚学习了C语言回调函数,现炒现卖,写一个实例供参考
在主函数调用SearchStudents函数的时候给它传递了SearchByNumber这个函数,而SearchStudents函数内部又返回调用了SearchByNumber,故为回调
#include<stdio.h>#include<stdlib.h>#definenull((void*)0)//定义一个学生结构体typedef struct{char *Name;long Number;unsigned char ChinesSocre;unsigned char MathSocre;unsigned char EnglishSocre;long sumSocre;float averageSocre;} STU,*PSTU;//定义回调函数指针typedef int (*SearchFunCallBack)(void const *, void const *);//查找学生并计算总成绩和平均成绩。PSTU SearchStudents(PSTU pStuList,SearchFunCallBack searchFun,void const * searchValue){while(pStuList->Name != null){if(searchFun(pStuList,searchValue) == 1){pStuList->sumSocre=pStuList->ChinesSocre+pStuList->MathSocre+pStuList->EnglishSocre;pStuList->averageSocre=pStuList->sumSocre/3.0f;break;}pStuList++;}return pStuList;}//通过学号查找学生int SearchByNumber(void const * ListNode,void const * value){PSTU pStu;pStu = (PSTU)ListNode;if(pStu->Number == *(long *)value){return 1;}else{return 0;}}//通过姓名查找学生int SearchByName(void const * ListNode,void const * value){PSTU pStu;pStu = (PSTU)ListNode;if(pStu->Name == (char *)value){return 1;}else{return 0;}}void main(){PSTU pStu;STU students[3];students[0].Name="小张";students[0].Number=1001;students[0].ChinesSocre=78;students[0].MathSocre=86;students[0].EnglishSocre=85;students[1].Name="小明";students[1].Number=1002;students[1].ChinesSocre=96;students[1].MathSocre=67;students[1].EnglishSocre=90;students[2].Name="小李";students[2].Number=1003;students[2].ChinesSocre=87;students[2].MathSocre=75;students[2].EnglishSocre=95;while(1){long value = 1003;//pStu = SearchStudents(students,SearchByNumber,&value);// 通过学号查找pStu = SearchStudents(students,SearchByName,"小明");// 通过姓名查找if(pStu->Name == null){printf("没有找到该学生。\n");}else{printf("查找到相应的学生:\n姓 名:%s\n学 号:%d\n语文成绩:%d\n数学成绩:%d\n英语成绩:%d\n总 成 绩:%d\n平均成绩:%.2f",pStu->Name,pStu->Number,pStu->ChinesSocre,pStu->MathSocre,pStu->EnglishSocre,pStu->sumSocre,pStu->averageSocre);}while(1);}}
- C语言回调函数实例
- C语言回调函数实例
- C语言回调函数实例
- C语言中的回调函数实例
- C语言中的回调函数实例
- C语言中的回调函数实例
- C语言中的回调函数实例
- C语言中的回调函数实例
- C语言回调函数(实例)
- C语言 回调函数的使用:(实例对比)
- C语言---回调函数
- c语言回调函数
- c语言回调函数
- C语言回调函数
- C语言回调函数
- C语言回调函数
- C语言回调函数
- 回调函数(c语言)
- Tomcat 配置jndi 通过spring调用
- 程序设计中的计算复用(Computational Reuse)
- mysql数据类型 int、bigint、smallint、tinyint
- 如何在 c++builder中有顺序地启动多个外部exe
- 在Tomcat中部署web应用
- C语言回调函数实例
- android 之 AIDL
- linux 中挂载,格式化NTFS分区
- Vi编辑器的基本使用方法!
- int main(int argc,char* argv[])详解
- 动态生成表格示例
- 实时操作系统概念
- typeid使用记录
- SwingWorker,多线程开发swing必备类,很多时候不用它界面会挂掉