第16周上机实践项目3——用函数指针调用函数
2014-12-14 20:31
225 查看
问题及代码
运行结果:
学习感悟:函数名就相当于数组名,也是一种地址,可以直接调用
/* * Copyright (c) 2014, 烟台大学计算机学院 * All rights reserved. * 文件名称:test.cpp * 作 者:辛彬 * 完成日期:2014年 12 月 14 日 * 版 本 号:v1.0 * * 问题描述: 补充函数。完成提示功能。 * 输入描述:选择。 * 程序输出:结果; */ #include <iostream> using namespace std; void eat(); void sleep(); void hitdoudou(); void run(void (*f)()); int main() { int iChoice; do { cout<<"请选择(1-吃;2-睡;3-打;其他-退)"; cin>>iChoice; if(iChoice==1) run(eat); else if(iChoice==2) run(sleep); else if(iChoice==3) run(hitdoudou); else break; } while(true); return 0; } void eat() { cout<<"我吃吃吃... ..."<<endl; } void sleep() { cout<<"我睡睡... ..."<<endl; } void hitdoudou() { cout<<"我不打还能干什么... ..."<<endl; } void run(void (*f)()) { if(f==eat) eat(); else if(f==sleep) sleep(); else if(f==hitdoudou) hitdoudou(); }
运行结果:
学习感悟:函数名就相当于数组名,也是一种地址,可以直接调用
相关文章推荐
- 第16周项目3 用函数指针调用函数
- 第16周项目3--用函数指针调用函数(吃饭,睡觉,打豆豆)
- 第16周上机实践项目3--相同的数字!
- 第16周项目3-用函数指针调用函数
- 第16周 项目3-用函数指针调用函数
- 第16周项目3-用函数指针调用函数
- 第16周上机实践项目1--逆序输出数组
- 第16周实践上机项目4--指针引出奇数因子
- 第16周上机实践项目2——用指针玩字符串(7)
- 第16周项目3函数指针调用函数
- 第16周上机实践项目2——用指针玩字符串(2)
- 第16周上机实践项目2——用指针玩字符串(5)
- 第16周项目3—用函数指针调用函数
- 2014秋C++第16周 项目3参考 用函数指针调用函数
- 第16周上机实践项目1——数组的排序
- 第16周项目3-用函数指针调用函数
- 第16周上机实践项目2——用指针玩字符串(3)
- 第16周上机实践项目2——用指针玩字符串(4)
- 第16周项目3 用函数指针调用函数
- 第16周项目3-用函数指针调用函数