1. 程式人生 > >ch6-控制程序流程

ch6-控制程序流程

reading support == 條件運算 numbers n) pac not HR

#include <QCoreApplication>
#include <iostream>
#include <string>

using namespace std;

const qreal C_PRECISION = 1e-6;

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    //1.if-else
#if 0
    cout << "enter two numbers:" << endl;
    qreal num1 = 0, num2 = 0;
    cin >> num1 >> num2;

    cout << "enter ‘d‘ to divide, ‘m‘ to multiply: ";
    uchar userOp = 0;
    cin >> userOp;

    if(‘m‘ == userOp)
    {
        cout << "you wish to multiply..." << endl;
        cout << num1 << " x " << num2 << " = " << num1 * num2 << endl;
    }
    else if(‘d‘ == userOp)
    {
        cout << "you wish to divide..." << endl;
        if(num2 - 0 < C_PRECISION)
        {
            cout << "division by zero is not allowed.." << endl;
        }
        else
        {
            cout << num1 << " / " << num2 << " = " << num1 / num2 << endl;
        }
    }
    else
    {
        cout << "unsupported input" << endl;
    }
#endif

    //2.switch-case
    //務必不要使用沒有 break 的 case 語句,
    //也不要依賴於 case 語句的順序,這會讓 swtich-case 結構過於復雜。
    //另外,如果以後不小心調整了 case 語句的順序,代碼可能不再可行。
#if 0
    enum DaysInWeek
    {
        Sunday = 0,
        Monday,
        Tuesday,
        Wednesday,
        Thursday,
        Friday,
        Saturday,
    };

    cout << "enter a number for a day(Sunday = 0): ";
    int day;
    cin >> day;

    switch (day) {
    case DaysInWeek::Sunday:
        cout << "Sunday was named after the Sun" << endl;
        break;
    default:
        cout << "Wrong input, execute again" << endl;
        break;
    }

#endif

    //3.?:
    //使用條件運算符(?:)時,不要使用復雜的條件和表達式
#if 0
    cout << "enter two numbers: " << endl;
    int number1 = 0, number2 = 0;
    cin >> number1 >> number2;

    int maxOne = number1 > number2 ? number1 : number2;
    cout << "its " << maxOne << endl;
#endif

    //4.loop
#if 0
    //while
 char userOp = ‘\0‘;

    do 
    {
  cout << "input two numbers: ";
  int num1 = 0, num2 = 0;
  cin >> num1 >> num2;

  cout << num1 << " * " << num2 << " = " << num1 * num2 << endl;

  cout << "press x to exit...";
  cin >> userOp;
    } while (userOp != ‘x‘);
#endif

    // for
    // 在 for 循環的初始化表達式中,可初始化多個變量
    // 有趣的是,還可使用循環表達式在每次循環時都將其遞減。
    // 略

    //rang based for
#if 0
 const uint ARRAY_CAP = 5;
 int someNum[ARRAY_CAP] = { 1, 2, -3, 4, 5 };

 // Yes. The same reason if you only ever read an argument you make the parameter const&.
 //
 // T // I‘m copying this
 // T& // I‘m modifying this
 // const T& // I‘m reading this
 // Those are your "defaults".
 // When T is a fundamental type(built - in), though, you generally just revert to const T(no reference) for reading, 
 // because a copy is cheaper than aliasing.
 for (const int i : someNum)
 {
  cout << i << endl;
 }

 for (const auto i : someNum)
 {
  cout << i << endl;
 }

 //string str = "Hello World!";
 //string str{ "Hello World!" };
 string str("Hello World!");
 for (const auto cc : str)
 {
  cout << cc << " ";
 }
 cout << endl;
#endif

 // continue,break
 // 在 while、 do…while 和 for 循環中, continue 導致重新評估循環條件,如果為 true,則重新進入循環塊;而 break 退出循環塊,即結束當前循環。
 // 在 for 循環中遇到 continue 時,將在評估條件前執行循環表達式(for 語句中的第三個表達式,通常用於遞增計數器)

#if 0
 const uint NUM_ROWS = 3;
 const uint NUM_COLUMNS = 2;

 int myInt[NUM_ROWS][NUM_COLUMNS] = { {1, 2},
           {2, 3},
           {3, 4} };

 for (int i = 0; i < NUM_ROWS; ++i)
 {
  for (int j = 0; j < NUM_COLUMNS; ++j)
  {
   printf("myInt[%u][%u]=%d\n", i, j, myInt[i][j]);
  }
 }
#endif

    return a.exec();
}


來自為知筆記(Wiz)

ch6-控制程序流程