# 第二次社課 --- # C++ 基礎語法 --- # C++結構 ---- ```cpp= #include <iostream> using namespace std; int main(){ cout << "hello world"; //記得加分號 return 0; } ``` --- # I/O 輸入輸出 ---- ## 語法 | 名稱 |功用 | | ------ | -------- | | cin | 輸入 | | cout| 輸出 | ---- ## 範例 ```cpp= #include <iostream> using namespace std; int main(){ string name; cin >> name; cout << "hello" << name; return 0; } ``` --- # 變數 ---- ## 語法 ```cpp= 資料型態 名稱; //ex int a; ``` ---- | 資料型態 | | | ------ | -------- | | int | 整數 | | long long| 長整數 | | float | 浮點數 | | double| 雙精度浮點數 | | char | 字元 | | string| 字串 | ---- ## 範例 ```cpp= #include <iostream> using namespace std; int main(){ int a = 1,b = 1; int c = a+b; cout << c; //2 } ``` --- # 判斷 ---- ## 語法 ```cpp= if (條件){ // do something }else{ // do something } ``` ---- ## 範例 ```cpp= #include <iostream> using namespace std; int main(){ int colin = 30; if (colin >= 30){ cout << "Wow"; }else{ cout <<"LOL"; } } ``` --- # 迴圈 ---- ## 語法 ```cpp= for (初始變數; 判斷式; 遞增式){ // do something } //eg for(int i= 0; i < 10 ; i++){ //重複10次的迴圈 A++; } ``` ---- ## 範例 ```cpp= #include <iostream> using namespace std; int main(){ int n = 0; for(int i= 0; i < 10 ; i++){ //重複10次的迴圈 n++; } cout << n; //10 } ``` --- # 陣列 ---- ![baSAdxh](https://hackmd.io/_uploads/H1mGTpaT6.png) ---- ```cpp= 資料型態 名稱[size]; //ex int a[10]; ``` ---- ## 範例 ```cpp= #include <iostream> using namespace std; int main(){ int a[10]; for(int i= 0; i < 10 ; i++){ //重複10次的迴圈 a[i]=0; } //a{0,0,0,0,0,0,0,0,0,0} for(int i= 0; i < 10 ; i++){ //重複10次的迴圈 a[i]=i; } //a{0,1,2,3,4,5,6,7,8,9} } ``` --- # 函式 ---- ---- ```cpp= type name(...) { // do something return something; } ``` ---- ## 範例 ```cpp= #include <iostream> using namespace std; int add(int a, int b) { int c = a + b; return c; } int main(){ int x,y; cin >> x >> y; int ans; ans = add(x,y); cout << ans; // x+y } ``` --- # [STL](https://hackmd.io/@pojinYang/ByKlmDAzp) --- # 附錄 ---- ## 以前社課電神做的詳細簡報 | 主題| 連結| | -------- | -------- | | 基礎架構 | [Portal](https://hackmd.io/9ZRxVZLQRw2gaRK0nlWI7Q ) | |運算子與if/else|[Portal](https://hackmd.io/@terry0711/SywWHeX16#/)| |迴圈 Loop|[Portal](https://hackmd.io/@mwqXlFx-Snm6rWYAV9nuAA/Byo2RdTka#/)| |陣列 Array|[Portal](https://hackmd.io/@yen951013/Array#/)| |字串 String|[Portal](https://hackmd.io/81FE62U2STuNHXEHdgitfA?view#/)| |函式 Function|[Portal](https://hackmd.io/@pojinYang/rJiWbMwyp#/)| |遞迴 Recursion|[Portal](https://hackmd.io/@ColinHung/B1nc9rCxp#/)| |資訊概論|[Portal](https://hackmd.io/GnFwiUwjQCmnxYsYL3DtNg#/)|
{"title":"第二次社課","contributors":"[{\"id\":\"8b61a27e-159d-4386-a2e4-e9ff3c2e6c6e\",\"add\":3517,\"del\":615}]"}
    93 views