码迷,mamicode.com
首页 > 其他好文 > 详细

ch6-控制程序流程

时间:2018-07-02 14:14:55      阅读:158      评论:0      收藏:0      [点我收藏+]

标签:程序   多个   ace   swt   运算符   ali   loop   hose   sda   

#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();
}




ch6-控制程序流程

标签:程序   多个   ace   swt   运算符   ali   loop   hose   sda   

原文地址:https://www.cnblogs.com/splitfire/p/9253373.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!