C++中的控制流

来源:互联网 发布:行知中学西安老师 编辑:程序博客网 时间:2024/06/06 02:24

/*
 * break、return、continue、exit
 * break     -- 跳出switch语句或提前从循环中退出                   // 语句级
 * continue  -- 开始一下次循环的执行                                    // 语句级
 * return    -- 将对CPU的控制权交给调用它所在函数的上一级    // 函数级
 * exit()    -- 终止程序                                                        // 文件级
 * OS: Windows XP
 * Programming Language: C++
 * Compiler: Dev-C++ 4.9.9.2
 * Date: 14:28 2009-12-7
 */

#include <iostream>
using namespace std;

#define INIT_VALUE 0
#define FALSE 0
#define TRUE 1

unsigned int GetFlag(unsigned char c_level);

int main(void)
{
      unsigned int ia = INIT_VALUE, ib = INIT_VALUE;
      unsigned char c_level = INIT_VALUE;

      unsigned int flag = FALSE;

      cout << "Input a level(A, B, C, D of E, input q or Q to stop)" << endl;
      cin >> c_level;
      for (ia = 0; ia < 3; ia++)
      {
            if ('q' == c_level)
            {
                  cout << "You have selected to stop." << endl;
                  exit(1);                    // 终止程序的运行,一下的所有指令不再执行。
            }
            else if ((c_level >= 'A') && (c_level <= 'E'))
            {
                  break;            // 程序在此处推出for循环,继续执行for循环以下的语句
            }
            else
            {
                  cout << "Invalid character, try again" << endl;
                  cin >> c_level;
                  if (ia >= 2)
                  {
                        cout << "Sorry, you have inputed three time, and all of them were invalid character, the program will stop." << endl;
                        exit(1);      // 终止程序的运行,一下的所有指令不再执行。
                  }
            }
      }
   
      flag = GetFlag(c_level);
   
      if (flag)
      {
            switch (c_level)
            {
                  case 'A':
                        cout << c_level << " means the score is [90,100]" << endl;
                        break;      /* 跳出switch语句,不再执行switch语句中剩余的部分,而是执行main()函数中switch语句之后的指令 */
                  case 'B':
                        cout << c_level << " means the score is [80,90)" << endl;
                        break;
                  case 'C':
                        cout << c_level << " means the score is [70,80)" << endl;
                        break;  
                  case 'D':
                        cout << c_level << " means the score is [60,70)" << endl;
                        break;  
                  case 'E':
                        cout << c_level << " means the score is [0,60)" << endl;
                        break;  
                  default:
                        break;                                                                             
            }
      }
   
      cout << "Done!" << endl;
   
      system("pause");
      return 0;      // 将控制权返还给操作系统,到此,程序运行结束。
}

unsigned int GetFlag(unsigned char c_level)
{
       unsigned char loc_flag;
       cout << "Do you want to kown what does " << c_level << "means " <<"('y' or 'n')" << endl;
       cin >> loc_flag;
       while ((loc_flag != 'y') && (loc_flag != 'n') && (loc_flag != 'Y') && (loc_flag != 'N'))
       {
            cout << "Invalid input try again: ";
            cin >> loc_flag;
       }
       if (('y' == loc_flag) || 'Y' == loc_flag)
       {
            return TRUE; /* 执行此语句后GetFlag()把对CPU的控制权返还给调用它的main()函数, 此后GetFlag()中的代码不再执行,而是去执行main()      函数中GetFlag()调用处之后的语句。*/
       }
       else
       {
            return FALSE;
       }
}