1. 歡迎來到C++編程世界!
1.1 什么是編程?
編程就像是給計算機寫一份詳細的"說明書",告訴它該做什么、怎么做。C++是一種強大的編程語言,可以用來創建游戲、應用程序和各種有趣的軟件!
1.2 第一個C++程序:Hello World!
讓我們從一個簡單的程序開始,它會在屏幕上顯示"Hello World!":
#include <iostream> // 這行代碼告訴計算機我們要使用輸入輸出功能
using namespace std; // 這行代碼讓我們可以使用簡寫方式(如cout而不是std::cout)int main() { // 每個C++程序都必須有一個main函數,程序從這里開始執行cout << "Hello World!" << endl; // 在屏幕上顯示文字return 0; // 告訴計算機程序成功結束
}
試一試:
- 打開代碼編輯器(如Code::Blocks、Dev-C++或Visual Studio)
- 輸入上面的代碼
- 點擊"運行"或"編譯并運行"按鈕
- 看看屏幕上顯示了什么!
2. 變量:計算機的"記憶盒子"
2.1 什么是變量?
變量就像是計算機中的"記憶盒子",我們可以把數據放進去,需要的時候再取出來。每個盒子都有一個名字(變量名)和類型(能放什么樣的數據)。
2.2 基本數據類型
數據類型 | 用途 | 示例 |
---|---|---|
int | 存儲整數 | int age = 15; |
float | 存儲小數(單精度) | float height = 1.75f; |
double | 存儲小數(雙精度,更精確) | double pi = 3.14159; |
char | 存儲單個字符 | char grade = 'A'; |
string | 存儲文本(多個字符) | string name = "小明"; |
bool | 存儲真/假值 | bool isRaining = true; |
注意: 使用string
類型需要包含<string>
頭文件。
2.3 聲明和使用變量
#include <iostream>
#include <string> // 使用string類型需要這個
using namespace std;int main() {// 聲明變量并賦值int age = 15;float height = 1.75f;string name = "小明";char initial = 'X';bool likesPizza = true;// 使用變量cout << "姓名: " << name << endl;cout << "年齡: " << age << endl;cout << "身高: " << height << "米" << endl;cout << "名字首字母: " << initial << endl;if (likesPizza) {cout << name << "喜歡披薩!" << endl;} else {cout << name << "不喜歡披薩!" << endl;}return 0;
}
小練習: 創建一個程序,聲明幾個變量存儲你的個人信息并顯示出來。
3. 與計算機"對話":輸入和輸出
3.1 輸出信息 (cout)
我們已經使用了cout
來輸出信息。cout
就像是計算機的"嘴巴",可以說出我們想說的話。
#include <iostream>
using namespace std;int main() {// 輸出文字cout << "歡迎來到C++世界!" << endl;// 輸出變量的值int score = 95;cout << "你的分數是: " << score << endl;// 一次輸出多個內容string player = "小明";int level = 5;cout << "玩家:" << player << " 等級:" << level << endl;return 0;
}
3.2 獲取輸入 (cin)
cin
就像是計算機的"耳朵",可以聽到我們說的話(輸入)。
#include <iostream>
#include <string>
using namespace std;int main() {string name;int age;cout << "請輸入你的名字: ";cin >> name; // 獲取輸入并存儲到name變量中cout << "請輸入你的年齡: ";cin >> age; // 獲取輸入并存儲到age變量中cout << "你好," << name << "! 你今年" << age << "歲了。" << endl;return 0;
}
注意: 當使用cin >>
讀取字符串時,它會在遇到空格時停止。如果要讀取一整行文字,可以使用getline
函數:
#include <iostream>
#include <string>
using namespace std;int main() {string fullName;cout << "請輸入你的全名: ";cin.ignore(); // 清除之前的輸入緩沖區getline(cin, fullName); // 讀取一整行cout << "你的全名是: " << fullName << endl;return 0;
}
3.3 格式化輸出
有時候我們希望輸出的數據看起來更整齊,可以使用一些格式化方法:
#include <iostream>
#include <iomanip> // 用于格式化輸出
using namespace std;int main() {double pi = 3.1415926535;// 設置小數點后保留2位cout << fixed << setprecision(2);cout << "π的值是: " << pi << endl;// 設置輸出寬度cout << setw(10) << "姓名" << setw(5) << "年齡" << endl;cout << setw(10) << "小明" << setw(5) << 15 << endl;cout << setw(10) << "小紅" << setw(5) << 16 << endl;return 0;
}
小練習: 創建一個程序,詢問用戶三個科目的分數,然后計算并顯示平均分(保留2位小數)。
4. 數學運算:計算機的"計算器"
4.1 基本算術運算符
C++支持所有基本的數學運算:
#include <iostream>
using namespace std;int main() {int a = 10, b = 3;cout << "a + b = " << a + b << endl; // 加法: 13cout << "a - b = " << a - b << endl; // 減法: 7cout << "a * b = " << a * b << endl; // 乘法: 30cout << "a / b = " << a / b << endl; // 除法: 3 (整數除法)cout << "a % b = " << a % b << endl; // 取余: 1 (10除以3余1)// 浮點數除法float result = static_cast<float>(a) / b;cout << "浮點數除法: " << result << endl; // 3.33333return 0;
}
4.2 復合賦值運算符
這些運算符可以簡化代碼:
#include <iostream>
using namespace std;int main() {int x = 5;x += 3; // 等同于 x = x + 3cout << "x += 3: " << x << endl; // 8x -= 2; // 等同于 x = x - 2cout << "x -= 2: " << x << endl; // 6x *= 4; // 等同于 x = x * 4cout << "x *= 4: " << x << endl; // 24x /= 3; // 等同于 x = x / 3cout << "x /= 3: " << x << endl; // 8return 0;
}
4.3 自增和自減運算符
#include <iostream>
using namespace std;int main() {int count = 5;cout << "初始值: " << count << endl;count++; // 后置自增:先使用值,再增加1cout << "count++后: " << count << endl; // 6++count; // 前置自增:先增加1,再使用值cout << "++count后: " << count << endl; // 7count--; // 后置自減:先使用值,再減少1cout << "count--后: " << count << endl; // 6--count; // 前置自減:先減少1,再使用值cout << "--count后: " << count << endl; // 5return 0;
}
小練習: 編寫一個程序,將攝氏溫度轉換為華氏溫度。公式:F = C × 9/5 + 32
5. 做出決定:條件語句
5.1 if 語句
if
語句讓程序能夠根據條件做出決定:
#include <iostream>
using namespace std;int main() {int score;cout << "請輸入你的分數: ";cin >> score;if (score >= 60) {cout << "恭喜你及格了!" << endl;}return 0;
}
5.2 if-else 語句
當條件不滿足時,可以使用else
執行其他操作:
#include <iostream>
using namespace std;int main() {int number;cout << "請輸入一個數字: ";cin >> number;if (number % 2 == 0) {cout << "這是一個偶數" << endl;} else {cout << "這是一個奇數" << endl;}return 0;
}
5.3 else-if 語句(多條件判斷)
當有多個條件需要檢查時,可以使用else if
:
#include <iostream>
using namespace std;int main() {int score;cout << "請輸入你的分數: ";cin >> score;if (score >= 90) {cout << "成績等級: A" << endl;} else if (score >= 80) {cout << "成績等級: B" << endl;} else if (score >= 70) {cout << "成績等級: C" << endl;} else if (score >= 60) {cout << "成績等級: D" << endl;} else {cout << "成績等級: F" << endl;}return 0;
}
5.4 購買賀卡題的實現
現在讓我們解決購買賀卡的問題:
#include <iostream>
using namespace std;int main() {int quantity;cout << "請輸入購買賀卡的數量: ";cin >> quantity;int totalCost;if (quantity <= 10) {totalCost = quantity * 8;} else if (quantity <= 20) {totalCost = quantity * 6;} else {totalCost = quantity * 4;}cout << "總費用: " << totalCost << "元" << endl;return 0;
}
小練習: 編寫一個程序,根據用戶年齡判斷是否可以觀看PG-13電影(13歲以上可以觀看)。
6. 重復執行:循環語句
6.1 for 循環
for
循環當我們知道要重復多少次時使用:
#include <iostream>
using namespace std;int main() {// 打印1到10的數字for (int i = 1; i <= 10; i++) {cout << i << " ";}cout << endl;// 計算1到100的和int sum = 0;for (int i = 1; i <= 100; i++) {sum += i;}cout << "1到100的和是: " << sum << endl;return 0;
}
for循環的結構:
int i = 1
:初始化計數器i <= 10
:循環條件i++
:每次循環后更新計數器
6.2 while 循環
while
循環在不確定要循環多少次,但知道循環條件時使用:
#include <iostream>
using namespace std;int main() {// 打印1到10的數字int i = 1;while (i <= 10) {cout << i << " ";i++;}cout << endl;// 猜數字游戲int secretNumber = 42;int guess;cout << "猜一個1到100之間的數字: ";cin >> guess;while (guess != secretNumber) {if (guess < secretNumber) {cout << "太小了! 再試一次: ";} else {cout << "太大了! 再試一次: ";}cin >> guess;}cout << "恭喜你猜對了!" << endl;return 0;
}
6.3 do-while 循環
do-while
循環至少執行一次,然后再檢查條件:
#include <iostream>
using namespace std;int main() {int number;do {cout << "請輸入一個正數: ";cin >> number;} while (number <= 0);cout << "你輸入的是: " << number << endl;return 0;
}
6.4 循環控制語句
break
:立即退出循環continue
:跳過當前循環的剩余部分,直接開始下一次循環
#include <iostream>
using namespace std;int main() {// break示例:當i等于5時退出循環for (int i = 1; i <= 10; i++) {if (i == 5) {break;}cout << i << " ";}cout << endl; // 輸出: 1 2 3 4// continue示例:跳過偶數for (int i = 1; i <= 10; i++) {if (i % 2 == 0) {continue;}cout << i << " ";}cout << endl; // 輸出: 1 3 5 7 9return 0;
}
小練習: 編寫一個程序,使用循環打印出乘法表(如1×1=1, 1×2=2, …, 9×9=81)。
7. 綜合練習
7.1 計算階乘
#include <iostream>
using namespace std;int main() {int n;cout << "請輸入一個正整數: ";cin >> n;long long factorial = 1;for (int i = 1; i <= n; i++) {factorial *= i;}cout << n << "! = " << factorial << endl;return 0;
}
7.2 判斷素數
#include <iostream>
using namespace std;int main() {int num;cout << "請輸入一個正整數: ";cin >> num;bool isPrime = true;if (num <= 1) {isPrime = false;} else {for (int i = 2; i * i <= num; i++) {if (num % i == 0) {isPrime = false;break;}}}if (isPrime) {cout << num << "是素數" << endl;} else {cout << num << "不是素數" << endl;}return 0;
}
7.3 斐波那契數列
#include <iostream>
using namespace std;int main() {int n;cout << "請輸入斐波那契數列的項數: ";cin >> n;long long a = 0, b = 1;cout << "斐波那契數列前" << n << "項: ";for (int i = 0; i < n; i++) {cout << a << " ";long long next = a + b;a = b;b = next;}cout << endl;return 0;
}
8. 下一步學習建議
恭喜你完成了C++基礎語法的學習!接下來你可以:
- 多練習:編寫更多小程序來鞏固知識
- 嘗試小項目:如計算器、猜數字游戲、簡單的文本冒險游戲
- 學習下一部分:字符串處理、數組和函數
- 參加編程競賽:如NOIP(全國青少年信息學奧林匹克競賽)
記住,編程就像學習一門新語言,需要不斷練習才能熟練掌握。不要害怕犯錯,每個錯誤都是學習的機會!
祝你編程愉快!