赞
踩
问题及代码:
- /*
- *Copyright (c) 2014,烟台大学计算机学院
- *All rights reserved.
- *文件名称:莉莉.cpp
- *作者:李莉
- *完成日期:2014年12月01日
- *版本号:v1.0
- *
- *问题描述:利用指针,来完成指针变量做函数参数,完成吃饭睡觉打豆豆
- *程序输入:根据运行界面提示输入
- *程序输出:根据不同的输入输出不同的结果
- */
- #include <iostream>
- using namespace std;
- void eat();
- void sleep();
- void hitdoudou();
- void run(void (*f)());//*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) ())
- {
- f();
- }

运行结果:
心得体会:
指针变量做函数参数。。好桑心。。慢慢理解吧
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。