zoukankan      html  css  js  c++  java
  • 第十六周项目4-用函数指针调用函数

    将下面的程序补充完整(包括定义函数),使其能够完成图示的功能。请使用已有程序的风格。

    /*
    * Copyright (c) 2014,烟台大学计算机学院
    * All right reserved.
    * 作者:邵帅
    * 文件:demo.cpp
    * 完成时间:2014年12月13日
    * 版本号: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;
    }
    void sleep()
    {
        cout<<"我睡睡..."<<endl;
    }
    void  hitdoudou()
    {
        cout<<"我不打还能干什么..."<<endl;
    }
    void run(void (*f)())
    {
        f();
    }
    
    运行结果:


    @ Mayuko



  • 相关阅读:
    Single Number II
    Best Time to Buy and Sell Stock
    Linked List Cycle
    Single Number
    Max Points on a Line
    Strategy
    LRU Cache
    Word Break II
    Text Justification
    Median of Two Sorted Arrays
  • 原文地址:https://www.cnblogs.com/mayuko/p/4567592.html
Copyright © 2011-2022 走看看