16周项目3--用函数指针调用函数

来源:互联网 发布:德比软件 地址 编辑:程序博客网 时间:2024/05/13 17:38

问题及代码:

/* *Copyright(c) 2014 烟台大学计算机学院 *All rights reserved. *文件名称:test.cpp *作者:尚 月 *完成日期:2014年 12 月 15 日 *版本号: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            return 0;    }    while (true);    return 0;}void eat(){    cout<<"我吃吃吃。。。。"<<endl;    cout<<endl;}void sleep(){    cout<<"我睡睡睡。。。"<<endl;    cout<<endl;}void hitdoudou(){    cout<<"我不打还能干什么。。。"<<endl;    cout<<endl;}void run(void (*f)()){    f();}

运行结果:

总结:

   用函数指针调用函数其实很简单.


0 0