第十六周项目3-用函数指针调用函数

来源:互联网 发布:上海游族网络 编辑:程序博客网 时间:2024/05/21 15:43

题目:

将下面的程序补充完整(包括定义函数),使其能够完成图示的功能。请使用已有程序的风格。
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(...)
             ...
    }
    while(true);
    return 0;
}

<pre class="html" name="code">#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);    }    while(iChoice>=1&&iChoice<=3);    void run(void (*f)());    return 0;}void eat(){    cout<<"我吃吃吃..."<<endl;}void sleep(){    cout<<"我睡睡..."<<endl;}void  hitdoudou(){    cout<<"我不打还能干什么..."<<endl;}void run(void (*f)()){    f();}


运行结果:

学习心得:

好好学习 天天向上

0 0