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

来源:互联网 发布:淘宝秒杀怎么玩 编辑:程序博客网 时间:2024/05/24 03:19

代码:

/* * Copyright (c) 2014, 烟台大学计算机学院 * All rights reserved. * 文件名称:test.cpp * 作    者:阿里木·阿布都热合曼 * 完成日期:2014年 10 月 x 日 * 版 本 号:v1.0 * * 问题描述:将下面的程序补充完整(包括定义函数),使其能够完成图示的功能。请使用已有程序的风格。void eat(); * 输入描述: * 程序输出: */#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        {            cout<<"我要学习C++啦"<<endl;            break;        }    }    while(true);    return 0;}void run(void(*f)()){    (*f)();}void eat(){    cout<<"我吃吃......."<<endl;}void sleep(){    cout<<"我睡睡......."<<endl;}void hitdoudou(){    cout<<"我不打还能干什么......"<<endl;}

运行结果:


0 0
原创粉丝点击