2020年6月14日 星期日

讀書心得 - C++ Primer (5th Edition) - Chapter 1

簡單的 C++ 程式

    要寫一個 C++ 程式跟 C 一樣,寫好 cpp 檔,交給編譯器 ( g++ ) 編成可執行程式。程式至少包含一個名為 main 的 function,用以當作程式的入口 function。
    int main() {
        return 0; // return 0 的原因為 0 通常代表著程式執行成功。
    }

Input/Output

    C++ 本身並沒有定義 I/O,需要額外的標準函式庫,iostream。
    #include <iostream>
    int main()
    {
        // operator << >> 會回傳左邊的 operand
        // 以 std::cin >> v1 >> v2; 為例
        // 執行結果 =
        //     std::cin >> v1;
        //     std::cin >> v2;
        int v1, v2;
        std::cout << "Enter two numbers:" << std::endl;
        std::cin >> v1 >> v2;
        std::cout << "The sum of " << v1 << " and " << v2 << " is " << v1 + v2 << std::endl;
        return 0;
    }

Note

常用詞彙

  • prompt 提示 驅使

    We can extend our main program to prompt the user to give us two numbers and then print their sum

  • expression (數學中的)式,表達式

    The smallest unit of computation. An expression consists of one or more operands and usually one or more operators. Expressions are evaluated to produce a result. For example, assuming i and j are ints, then i + j is an expression and yields the sum of the two int values.

  • angle brackets : <>

  • quotation marks : ""

  • curly braces : {}

  • operator : 運算子

  • operand : 運算元

  • assignment

    Obliterates an object’s current value and replaces that value by a new one.

  • comments : 註解

  • statement : wiki

    The new part of this program is the while statement.A while has the form

    while (condition)
        statement

    A while executes by(alternately) testing the condition and executing the associated
    statement until the condition is false.A condition is an expression that yields a result
    that is either true or false..So long as condition is true, statement is executed.
下一篇 :

0 意見:

張貼留言

Popular Posts