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

来源:互联网 发布:问财神营销软件 编辑:程序博客网 时间:2024/06/06 03:18

问题及代码:

/**Copyright (c)2014,烟台大学计算机与控制工程学院*All   rights  reserved.*文件名称:main.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);  //调用run函数,通过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();}

运行结果:


总结:通过指针函数来调用函数。

0 0