打豆豆--char3

来源:互联网 发布:淘宝上买的弹弓枪拒收 编辑:程序博客网 时间:2024/05/17 09:30

           阅读并运行下面的程序“吃饭、睡觉、打豆豆”,体会形式上死循环,实际不“死”的技巧。在此基础上,改写程序并提交报告。程序中用了自定义函数
           改写任务1:去除有关使用自定义函数的一切痕迹,根据用户的选择,用你已经会的cout直接输出有关内容

           改写任务2:将程序中的if改为switch。
           改写前的程序:

          
#include <iostream>   using namespace std;  int main()  {     char cChioce;     void eat();     void sleep();     void hitdoudou();     do      {              cout<<"*  1. 吃饭"<<endl;        cout<<"*  2. 睡觉"<<endl;        cout<<"*  3. 打豆豆"<<endl;        cout<<"*  0. 退出"<<endl;              cout<<"*  请选择(0-3):";        cin>>cChioce;        if (cChioce=='1')             eat();        else if (cChioce=='2')           sleep();        else if (cChioce=='3')           hitdoudou();        else if (cChioce=='0')           break;        else         {           cout<<"\007选择错误!"<<endl<<endl;           continue;        }        cout<<"恭喜你完成了一项工作!"<<endl<<endl;     }while(1);     return 0;  }  void eat()  {     cout<<"我吃吃吃... ..."<<endl;  }  void sleep()  {     cout<<"我睡觉觉... ..."<<endl;  }  void hitdoudou()  {     cout<<"我打打打... ..."<<endl;  } 


改写后的程序:

#include <iostream>   using namespace std;  int main()  {      char cChioce;      do       {                cout<<endl;          cout<<"*  1. 吃饭"<<endl;          cout<<"*  2. 睡觉"<<endl;          cout<<"*  3. 打豆豆"<<endl;          cout<<"*  0. 退出"<<endl;                cout<<"*  请选择(0-3):";          cin>>cChioce;          if (cChioce=='0')          {              cout<<"撤退!"<<endl;               break;  //此break的跳转点有别于下面switch中的break.           }          else           {              switch (cChioce)              {              case '1':                    cout<<"我吃吃吃... ..."<<endl;                   break;              case '2':                  cout<<"我睡觉觉... ..."<<endl;                  break;              case '3':                  cout<<"我打打打... ..."<<endl;                  break;              default:                  cout<<"\007选择错误!"<<endl;                  //这儿可以不写continue;               }          }      }while(1);      return 0;  }