# C++
---
# 基礎架構
----
```cpp=
#include<iostream>
using namespace std;
int main(){
return 0;
}
```
---
# 萬用標頭檔
```cpp=
#include<bits/stdc++.h>
```

---
# 變數
----

----
## 宣告
資料型態 變數名稱;
```cpp=
#include<iostream>
using namespace std;
int main(){
int a;
a = 17;
char b;
b = 'h';
float c;
c = 5.23;
}
```
----
## 資料型態

---
# string
string為字串 但是用前要先include
```cpp=
#include<iostream>
#include<string>
using namespace std;
int main(){
string a;
a="gaybar";
}
```
---
# 語法
----
```cpp=
a = 6 // 設 a 為 6
a == 6 //a 等於 6
a != 6 //a 不等於 6
a + 6 // a + 6 這個數字
a = a + 6 // 將a這個數字 + 6
a += 6 //跟上面一樣
a++ //a = a + 1
++a // a + 1 = a
a % 3 // a / 3 的餘數
a && b // a and b
a || b // a or b
```
---
# 輸入輸出
----
## 輸入
通常是將一個變數賦值
```cpp=
int a;
cin>>a;
```
----
## 輸出
將一些東西顯示在螢幕上面
```cpp=
cout<<7122<<endl;
cout<<"hello"<<endl;
cout<<a<<endl;
```
---
# 例題
----
我們的美工由於是個孤兒,他很想要有人能跟他打招呼。請你寫一個程式,不管怎麼寫,他都會跟你say Hello!
(請注意!是「不管怎麼寫喔!」)
----
範例:

----
# 答案
----
```cpp=
#include <iostream>
#include <string>
using namespace std;
int main(){
string a;
cin>>a;
cout<<"Hello!"<<endl;
}
```
---
# 選擇結構
----
### 選擇結構會根據條件式判斷的結果
### 選擇不同的應對方式 有以下三種語法
1. if
2. if-else
3. if-else if-else
---
# IF
----
### if是如果條件式成立,才執行敘述,否則不執行。

----
## 範例code
```cpp=
if(a == 3){
cout<<"A等於3耶";
}
if(a == 3) cout<<"A等於3耶";
```
---
# if-else
----
### if-else是如果條件式成立,執行敘述1;否則執行敘述2。

----
```cpp=
if(a==3) cout<<"A等於3耶";
else cout<<"A不等於3耶";
```
---
# if-else if-else
----
### if-else if是依序判斷多個條件式,那個條件式成立,就執行此條件式內的敘述,若都不成立,就執行 else

----
```cpp=
if(a == 1) cout<<"A等於1耶";
else if(a == 2) cout<<"A等於2耶";
else if(a == 3) cout<<"A等於3耶";
else cout<<"A不等於1或2或3耶"
```
----
# 以上就是對選擇結構的大概介紹
---
# 進階用法
---
## A?B:C
----
## 語法
A?B:C
A is true -> B
A is false -> C
```cpp=
cout<<((副社==甲) ? "臭甲" : "嗨!朋友")<<endl;
```
---
# 例題
----
這裡有三個評級:
1.大於180mm->得A
2.大於170mm且不超過180mm->得B
3.大於160mm且不超過170mm->得C
----
這裡有一項資料,而這是公關的身高:161.4 mm ,請幫我寫出一個程式,先輸入他的身高後,
再判斷他在哪個等級。
----
範例


----
# 答案
----
```cpp=
#include <iostream>
using namespace std;
int main(){
float a;
cin>>a;
if(a > 180) cout<<"A";
else if(a>170 && a<=180) cout<<"B";
else if(a>160 && a<=170) cout<<"C";
}
```
(如果你是用三個if,也算你對,但這樣程式碼會太多)
----
# 另外一個答案
----
```cpp=
#include <iostream>
using namespace std;
int main(){
float a;
cin>>a;
cout<<(a > 180?"A":"False")<<endl;
cout<<(a>170 && a<=180?"B":"False")<<endl;
cout<<(a>160 && a<=170?"C":"False")<<endl;
}
```
---
# 加減乘除
## 請輸入兩個變數及運算子做出加減乘除
input
```
12
+
3
```
output
```
15
```
input
```
15
-
5
```
output
```
10
```
----
# code
```coo=
#include <iostream>
using namespace std;
int count(int a, int b, char c);
int main(){
int a, b;
char c;
cin >> a >> c >> b;
cout << count(a, b, c) << "\n";
system("pause");
return 0;
}
count (int a, int b, char c){
if(c == 43)
return a+b;
else if(c == 45)
return a-b;
else if(c == 42)
return a*b;
else
return a/b;
}
```
---
迴圈(For and While)
===
## While迴圈
----
### 執行步驟
1. 當條件符合(true),就執行大括號內的程式
2. 回到小括號內再次判斷:
符合(true)-->執行下方指令
不符合(false)-->結束迴圈跳出
### 格式
```cpp
while ( 執行條件 ){
要執行的指令;
}
```
----
## 接下來複習一下IF結構
### If格式
```cpp
if ( 執行條件 ){
要執行的指令;
}
```
----
## If v.s. While
| |**If迴圈** |**While迴圈** |
| ----------- | --------- | ----------------- |
| **條件符合後** | 執行一次 | 一直執行到條件不符合 |

----
### 範例:印出1~10的數字
```cpp=
#include<iostream>
using namespace std;
int main(){
int i = 1;
while (i <= 10){
cout << i << endl;
i++;
}
return 0;
}
```
----
### 範例:印出3X9的「\*」(如下)
```***
***
***
***
***
***
***
***
***
***
```
----
```cpp=
#include <iostream>
using namespace std;
int main() {
int i = 1;
while (i <= 9) {
int j = 1;
while (j <= 3) {
cout << "*";
j++;
}
cout << endl;
i++;
}
return 0;
}
```
----

#### 程式:
```cpp
while ( 等待時間 >= 20分鐘 ) {
跟罹患阿茲海默症的阿嬤要錢;
}
```
---
## For迴圈
----
### 執行步驟
1. 設定初始值,接著進行判斷
2. 當條件符合(true),就執行大括號內的程式
3. 回到小括號執行第二個分號後的程式(運算式)
4. 再次進行判斷:
符合(true)-->回到第二步
不符合(false)-->結束迴圈並跳出
### 格式
```cpp
for( 初始化 ; 執行條件 ; 運算式 ){
要執行的指令;
}
```
----
### 範例:輸出3次「HI」後輸出「BYE」
```cpp=
#include<iostream>
using namespace std;
int main (){
for( int i = 0; i < 3; i++ ){
cout << "HI" << endl;
}
cout<<"BYE"<<endl;
return 0;
]
```
#### ⊳小提醒:宣告的 i 作用範圍只有迴圈內喔!
----
### 範例:印出1~10
```cpp=
#include<iostream>
using namespace std;
int main (){
for(int i = 0; i < 10; i++){
cout << i + 1 << endl;
}
return 0;
}
```
----
### 範例:印出3X9的「\*」
----
```cpp=
#include<iostream>
using namespace std;
int main (){
for(int i = 0 ; i < 9 ; i ++){
for(int j = 0 ; j < 3 ; j++){
cout<<'*';
}
cout<<endl;
}
return 0;
}
```
---
陣列-array
===
----

----

---
## 一維陣列
----
## 格式
```cpp=
資料型態 陣列名稱[陣列大小];
int arr[5]; //亂碼
int arr[5] = {}; //[0,0,0,0,0]
int arr[5] = {0, 1, 2, 3, 4}; //[0,1,2,3,4]
int arr[5] = {0, 1, 2} //[0,1,2,0,0]
int arr[5] = {1} //[1,0,0,0,0]
float arr[5] //跟int arr[5]一樣但變成float型態
```
**陣列的區間是arr[0] ~ arr[n-1]**
<font color="red">**不包括arr[n]**</font>
----
## 輸入輸出
```cpp=
for(int i = 0; i < 5 ; i++)
cin >> arr[i];
for(int i = 0; i < 5 ; i++)
cout << arr[i] << ' ';
```
----
## 練習時間!
給定$n$接下來會有$n$個數字
請倒過來輸出
```
input:
5
1 2 3 4 5
output:
5 4 3 2 1
```
----
## CODE
```cpp=
#include<bits/stdc++.h>
using namespace std;
int main(){
int n;
cin >> n;
int arr[n];
for(int i=0; i<n; i++) cin >> arr[i];
for(int i=n-1; i>=0; i--) cout << arr[i] << ' ';
return 0;
}
```
----
---
## 區間合
給定 $n$ 代表接下來會有一個 n 個數字的數列
給定 $l, r$
問數列第 $l$ 項到第 $r$ 項的總和
input:
5
3 1 2 5 4
0 3
output:
11
----
## CODE
```cpp=
int n;
cin >> n;
int arr[n];
for(int i = 0; i < n; i++)
cin >> arr[i];
int l, r, ans = 0;
cin >> l >> r;
for (int i = l; i <= r; i++)
ans += arr[i];
cout << ans << endl;
```
---
## 二維陣列
----
廣義的陣列
| arr[i][j] | 0 | 1 | 2 |
| -- | -- | -- | --|
| 0 | arr[0][0] | arr[0][1]|arr[0][2]|
| 1 | arr[1][0] | arr[1][1]|arr[1][2]|
| 2 | arr[2][0] | arr[2][1]|arr[2][2]|
| 3 | arr[3][0] | arr[3][1]|arr[3][2]|
----
二維陣列輸入輸出
```cpp=
int arr[5][5];
for(int i = 0; i < 5; i++){
for(int j = 0; j < 5; j++){
cin>>arr[i][j];
}
}
for(int i = 0; i < 5; i++){
for(int j = 0; j < 5; j++){
cout << arr[i][j] << ' ';
}
cout << endl;
}
```
----
## 練習時間
輸出三九乘法表(要用陣列先存起來)
| 3\9 | 1 | 2 | 3 |4|5|...9|
| -- | -- | -- | --|--|--|--|
| 1 | 1 | 2|3|4|5|...9|
| 2 | 2 | 4|6|8|10|...18|
| 3 | 3 | 6|9|12|15|...27|
----
```cpp=
int arr[3][9];
for(int i = 1; i <= 3; i++){
for(int j = 1; j <= 9;j++){
arr[i-1][j-1] = i*j;
}
}
for(int i = 0; i < 3; i++){
for(int j = 0; j < 9; j++){
cout << arr[i][j] << ' ';
}
cout<<endl;
}
```
---
## 注意事項
----
- 陣列大小是 0 ~ n-1
- 不要呼叫 arr[-1] 或 arr[n]
- 陣列大小最多通常是$10^8$這麼大
- 不要被二維陣列的行列固定想法 (行列可交換)
- 大小不可之後改變
- 初始值未固定
---
{"metaMigratedAt":"2023-06-17T23:02:47.303Z","metaMigratedFrom":"YAML","title":"C++","breaks":true,"contributors":"[{\"id\":\"7fcc0ba7-7581-49cc-8661-a10bbd3e484f\",\"add\":148,\"del\":27},{\"id\":\"9d72ebb6-005c-4070-b9d6-ab4931f5153c\",\"add\":580,\"del\":93},{\"id\":\"fb5d6386-b221-4316-bb5f-ebdb0890573c\",\"add\":11787,\"del\":79},{\"id\":\"91eb9fab-5ee7-4263-9029-085c01058641\",\"add\":1111,\"del\":108}]"}