首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> C語言 >> C++ >> C++ 入門

C++ 程式設計入門實例精講:從 Hello World 到四則運算迴圈

     編輯:C++ 入門

第一支程式:最小完整結構


// program_1_1.cpp
#include  // 標準輸入輸出串流

int main() {                    // 程式入口,返回 int
    std::cout << "Let's learn to write a C++ Program."; // 輸出到標準輸出
    std::cin.get();             // 等待按鍵,防止控制台瞬間關閉
    return 0;                   // 返回 0 表示成功
}

關鍵點:`#include ` 引入串流庫、`std::cout`/`std::cin` 為物件而非函數、`<<`/`>>` 為運算子多載、`main` 必須返回 `int`。

變數、輸入與運算:求和與圓面積


// program_1_2.cpp : 求兩數之和
#include 
int main() {
    int a = 43, b = 37, sum = a + b;
    std::cout << "The sum is " << sum << std::endl;
    std::cin.get(); return 0;
}

// program_1_3.cpp : 圓面積 (常數、浮點數、鍵盤輸入)
#include 
int main() {
    const float PI = 3.14f;     // 常數,編譯期決定,不可修改
    float radius;
    std::cout << "Enter radius: ";
    std::cin >> radius;         // 從標準輸入讀取到 radius
    float area = PI * radius * radius;
    std::cout << "\nThe area of circle is: " << area << std::endl;
    std::cin.get(); return 0;
}

條件分支:溫度轉換


// program_1_4.cpp : 攝氏/華氏互轉
#include 
int main() {
    float t; char type;
    const float FACTOR = 1.8f, OFFSET = 32.0f;
    std::cout << "Enter temperature: "; std::cin >> t;
    std::cout << "C or F? "; std::cin >> type;

    if (type == 'c' || type == 'C') {
        std::cout << "Fahrenheit = " << t * FACTOR + OFFSET << std::endl;
    } else if (type == 'f' || type == 'F') {
        std::cout << "Celsius = " << (t - OFFSET) / FACTOR << std::endl;
    } else {
        std::cout << "Invalid type." << std::endl;
    }
    std::cin.get(); return 0;
}

迴圈與選單:持續運算計算機


// program_1_5.cpp : 四則運算,直到輸入 'n' 退出
#include 
int main() {
    int x, y; char op, cont = 'y';
    while (cont != 'n' && cont != 'N') {
        std::cout << "First integer: "; std::cin >> x;
        std::cout << "Second integer: "; std::cin >> y;
        std::cout << "Operator (+ - * /): "; std::cin >> op;

        switch (op) {
            case '+': std::cout << x << " + " << y << " = " << x + y; break;
            case '-': std::cout << x << " - " << y << " = " << x - y; break;
            case '*': std::cout << x << " * " << y << " = " << x * y; break;
            case '/':
                if (y != 0) std::cout << x << " / " << y << " = " << x / y;
                else std::cout << "Divide by zero!";
                break;
            default: std::cout << "Invalid operator!";
        }
        std::cout << "\nContinue? (y/n): "; std::cin >> cont;
    }
    std::cin.get(); return 0;
}

關鍵點:`while` 迴圈條件控制流程、`switch` 多分支、`cin` 讀取字元會跳過空白 (含換行),若需讀取空白需用 `cin.get()` 或 `getline`。

常見新手錯誤與避坑指南

錯誤代碼現象原因修正
`void main()`編譯警告/錯誤標準要求 `int main()`改為 `int main()` 並 `return 0;`
缺 `#include ``cout`/`cin` 未宣告未引入串流庫加上頭檔
`std::cin >> ch` 讀不到字元跳過輸入緩衝區殘留換行符前置 `std::cin.ignore()` 或用 `std::getline`
整數除法截斷`5/2 == 2`整數型別除法向零截斷至少一邊轉 `float`/`double`
迴圈無法退出死迴圈條件變數未在迴圈內更新檢查 `cont` 是否正確讀取並更新

結語

這五個範例涵蓋 C++ 核心基礎:主函數、輸入輸出、變數與型別、常數、運算子、條件、迴圈、錯誤處理。建議逐行手敲、編譯、運行、修改、觀察差異——「手感」是程式設計入門最寶貴的資產。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved