<font size=7>**C++筆記**</font> --- # 輸出 ```cpp= #include <iostream> using namespace std; int main() { cout << "Hello world!" << endl; return 0; } ``` **iostream 是 C++ 中用於資料輸入與輸出的標頭文件,屬於 C++ 標準程式庫的一部分。 iostream 為 Input/Output Stream 的縮寫,即是輸入/輸出流。** **using namespace std 簡單來說就是呼叫一個名為 std 的class。namespace 的功能簡單來說就是class。** **endl 代表 end line,簡單說就是換行,換成"/n"是一樣的效果。** # 變數 <font size=5>**變數型態**</font> | 型態 | 中文意思 | 英文字義 | 可儲存的資料 | | -------- | -------- | -------- | -------- | | int | 整數 | Integer | 100、-5、1246 ... | | float | 浮點數(小數) | floating point | 3.14159、4.3、-1.1 ... | | char | 字元(半形字) | Character | 'a'、'R'、'1'、'@' ... | | string | 字串(文句) | String | "Hello"、"Q_Q"、"Rock!" ... | | bool | 布林(是非) | boolean | true、false | <br> <font size=5>**變數宣告與輸出**</font> <font size=4>**Code**</font> ```cpp= #include<iostream> using namespace std; int main() { int HP; int MP = 20; cont << MP return 0; } ``` <font size=4>**output**</font> ``` 20 ``` **宣告變數寫法基本與C相同。** <br> <font size=5> **加減乘除輸出**</font> <font size=4>**Code 1**</font> ```cpp= #include<iostream> using namespace std; int main() { int c = 20; c = c + 1; // <------ here cout << c << endl; return 0; } ``` <font size=4>**output**</font> ``` 21 ``` <font size=4>**Code 2**</font> ```cpp= #include<iostream> using namespace std; int main() { int c = 20; cout << c*c << endl; // <------ here return 0; } ``` <font size=4>**output** </font> ``` 400 ``` # 輸入 <font size=5>**語法**</font> ```cpp= cin >> 變數名稱; ``` OR ```cpp= cin >> 變數名稱1 >> 變數名稱2; ``` * 注意輸出 << 和輸入 >> 的方向不一樣。 * C++會略過輸入資料之間的空白、換行。 <br> <font size=5>**範例:計算商品價格總和。**</font> <font size=4>**Code**</font> ```cpp= #include<iostream> using namespace std; int main() { int priceA , priceB; cin >> priceA >> priceB; cout << priceA + priceB << endl; return 0; } ``` <font size=4>**input**</font> ``` 100 150 ``` <font size=4>**output**</font> ``` 250 ``` <BR> # 重複輸入 * 將輸入句 cin >> 放入 while中 ```cpp= while( cin >> 變數 ) { 每次輸入變數時,要做什麼事... } ``` OR ```cpp= while( cin >> 變數1 >> 變數2 ) { 每次輸入變數1、變數2時,要做什麼事... } ``` <font size=5>**範例:計算商品價格總和。**</font> <font size=4>**Code**</font> ```cpp= #include<iostream> using namespace std; int main() { int priceA , priceB; while( cin >> priceA >> priceB ) { cout << priceA + priceB << endl; } return 0; } ``` <font size=4>**input**</font> ``` 100 25 100 150 311246 25452 1246 6421 ``` <font size=4>**output**</font> ``` 125 250 336698 7667 ``` # if條件式 <font size=5>**語法**</font> ```cpp= if( 條件 ) { 如果條件成立時做什麼... } ``` * 寫法與C相同。 # while迴圈 <font size=5>**語法**</font> ```cpp= while( A.條件式 ) { B.當條件成立時,就重覆做的事... } ``` **執行起來流程如下:** **檢查條件A,成立就做B** **==>檢查條件A,成立就做B** **==>檢查條件A,成立就做B** **...** **...** **==>檢查條件A,成立就做B** **==>檢查條件A,不成立離開。** <br> <font size=5>**範例:班級名條**</font> <font size=4>**Code**</font> ```cpp= #include<iostream> using namespace std; int main() { int n; int i; cin >> n; i = 1; while( i <= n ) { cout << "No." << i << endl; i++; } return 0; } ``` <font size=4>**input**</font> ``` 10 ``` <font size=4>**output**</font> ``` No.1 No.2 No.3 No.4 No.5 No.6 No.7 No.8 No.9 No.10 ``` <br> # for迴圈 <font size=5>**語法**</font> ```cpp= for( [A.]一開始先做什麼事 ; [B.]條件式 ; [D.]等C每作完一次,就做什麼事 ) { [C.]當B條件成立時,就重覆做的事... } ``` **執行起來流程如下:** **先做A ==> 檢查B條件,成立就做C,接著做D** **==> 檢查B條件,成立就做C,接著做D** **==> 檢查B條件,成立就做C,接著做D** **...** **...** **==> 檢查B條件,成立就做C,接著做D** **==> 檢查B條件,不成立離開。** <br> <font size=5>**範例:班級名條**</font> <font size=4>**Code**</font> ```cpp= #include<iostream> using namespace std; int main() { int n; int i; cin >> n; for( i=1 ; i<=n ; i++ ) { cout << "No." << i << endl; } return 0; } ``` <font size=4>**input**</font> ``` 10 ``` <font size=4>**output**</font> ``` No.1 No.2 No.3 No.4 No.5 No.6 No.7 No.8 No.9 No.10 ``` <br> # 巢狀迴圈 **指 while/for 裡面還有 while/for 的情況。** <br> <font size=5>**範例一:長方形製造機**</font> 輸入X、Y,並輸出 **寬為X 、 高為Y** 的方形。 <br> <font size=4>**Code - for 迴圈**</font> ```cpp= #include<iostream> using namespace std; int main() { int X, Y; int i, j; cin >> X >> Y; for( i=1 ; i<=Y ; i=i+1 ) { for( j=1 ; j<=X ; j=j+1 ) { cout << "*"; } cout << endl; } return 0; } ``` <font size=4>**Code - while 迴圈**</font> ```cpp= #include<iostream> using namespace std; int main() { int X, Y; int x; cin >> X >> Y; while( Y > 0 ) { x = X; while( x > 0 ) { cout << '*'; x--; } cout << endl; Y--; } return 0; } ``` <font size=4>**input**</font> ``` 6 3 ``` <font size=4>**output**</font> ``` ****** ****** ****** ``` <br> <font size=5>**範例二:直角三角形**</font> 輸入整數N,輸出一個對邊、鄰邊邊長為N,以*拼成的直角三角形。 <br> <font size=4>**Code**</font> ```cpp= #include<iostream> using namespace std; int main() { int N; int i, j; cin >> N; for( i=1 ; i<=N ; i++ ) { for( j=1 ; j<=i ; j++ ) { cout << "*"; } cout << endl; } return 0; } ``` <font size=4>**input**</font> ``` 4 ``` <font size=4>**output**</font> ``` * ** *** **** ``` <br> <font size=5>**範例三:金字塔製造機**</font> 輸入整數N,輸出一個高為N,以*組成的金字塔。 <br> <font size=4>**Code**</font> ```cpp= #include<iostream> using namespace std; int main() { int N; int i, j; cin >> N; for( i=1 ; i<=N ; i++ ) { for( j=1 ; j<=N-i ; j++ ) { cout << " "; } for( j=1 ; j<=2*i-1 ; j++ ) { cout << "*"; } cout << endl; } return 0; } ``` <font size=4>**input**</font> ``` 3 ``` <font size=4>**output**</font> ``` * *** ***** ``` <br> # 選擇排序法 <font size=5>**概念:**</font> **將數字們分成2類,未排序 和 已排序** **一開始所有數字都是未排序** **重複 N 次:** **從未排序的數字中挑出最小的數字,放入已排序的最尾端。** **依照上述** **第1次可以挑到所有數字中第1小的數字 (最小的數字)** **第2次可以挑到所有數字中第2小的數字** **...** **第N次可以挑到所有數字中第N小的數字 (最大的數字)** **最後就由小到大排完了** <font size=5>**實際操作**</font> |已排序 | 未排序 | | -------- | -------- | | 空 | 3、5、1、7 | | **1** | 3、5、7 | | 1、**3** | 5、7 | | 1、3、**5** | 7 | | 1、3、5、**7** | 空 | <font size=4>**Code**</font> ```cpp= #include<iostream> using namespace std; int main() { int num[100]; int N; int i, j, tmp; //[input] cin >> N; for( i=0 ; i<N ; i=i+1 ) { cin >> num[i]; } for( i=0 ; i<N ; i=i+1 ) { for( j=i+1 ; j<N ; j=j+1 ) { if( num[i] > num[j] ) { //變數交換 tmp = num[i]; num[i] = num[j]; num[j] = tmp; } } } //[output] for( i=0 ; i<N ; i=i+1 ) { cout << num[i] << " "; } cout << endl; return 0; } ``` <font size=4>**類題練習**</font> * Zerojudge a104 排序 http://zerojudge.tw/ShowProblem?problemid=a104 # 運算式簡寫 | 原始寫法 | 簡寫 | | -------- | -------- | | i=i+1 |i++、++i| |i=i-1 |i - - 、- - i| |a=a+b |a+=b | |a=a-b |a-=b | |a=a*b |a*=b | |a=a/b |a/=b | |a=a%b |a%=b | <br> * a=i++意思是a先等於i,i再加一。 * a=++i則是i會先加一,a再等於i。 <br> # 內建函式 ```cpp= #include<函式庫名稱> ``` <font size='5'>**常見函式庫:**</font> | 函式庫 | 函式 | 功能 |回傳值型態 | | -------- | -------- | -------- | -------- | | math.h | sqrt( float x ) | 回傳 x 的開根號值 |float | | | pow( float x, float y ) | 回傳 x 的 y 次方 | float | | ctype.h | isalpha( char c ) | 回傳 c 是不是英文字母 | bool | | | isdigit( char c ) | 回傳 c 是不是數字 | bool | |string.h | isdigit( char c ) | 回傳 s 的長度 | int | 可以用利用cplusplus.com查詢各個函式庫、函式的使用方法 http://www.cplusplus.com/ # 自訂函式 <font size=5>寫法:</font> ```cpp= 回傳值型態 函式名稱(參數1型態 參數名稱1, 參數2型態 參數名稱2, ... ) { Do anything you want... return 回傳值; } ``` <font size='5'>**範例1 - 網咖算帳 (有回傳值函式)**</font> <font size='3'>洛亞網咖收費標準如下: 前三小時,每小時30元 第四小時起,每小時20元 </font> <br> <font size='4'>**輸入說明**</font> <font size='3'>一個整數 n 代表打咖 n 小時</font> <font size='4'>**輸出說明**</font> <font size='3'>打咖 n 小時共要付多少錢</font> <br> <font size=4>**Code:**</font> ```cpp= #include<iostream> using namespace std; int pay( int hour ) { if( hour <= 3 ) { return hour*30; } else { return 3*30 + (hour-3)*20; } } int main() { int n; while( cin >> n ) { cout << pay(n) << endl; } return 0; } ``` # 遞迴 遞迴(Recursion),是指在函式中使用函式自身的方法。 遞迴函式必須有終止條件,才能被計算。 <br> <font size=5>**範例1:階乘**</font> ![](https://i.imgur.com/XhA2uzJ.png) ![](https://i.imgur.com/OTvpt3j.png) <font size=4>**Code**</font> ```cpp= #include<iostream> using namespace std; int f(int n) { if( n == 0 ) return 1; if( n >= 1 ) return n*f(n-1); } int main() { int n; while( cin >> n ) { cout << f(n) << endl; } return 0; } ``` <font size=4>**input**</font> ``` 1 3 5 ``` <font size=4>**output**</font> ``` 1 6 120 ``` <font size=5>**範例2:次方**</font> ![](https://i.imgur.com/q6HIr2H.png) ![](https://i.imgur.com/XWtEfWz.png) <font size=4>**Code**</font> ```cpp= #include<iostream> using namespace std; int f(int a, int b) { if( b == 0 ) return 1; if( b >= 1 ) return a * f(a,b-1); } int main() { int a, b; while( cin >> a >> b ) { cout << f(a,b) << endl; } return 0; } ``` <font size=4>**input**</font> ``` 2 4 3 3 5 2 ``` <font size=4>**output**</font> ``` 16 27 25 ``` # 大小寫轉換 <font size=5>**寫法:**</font> ```cpp= tolower(轉換的變數);//大寫轉小寫 toupper(轉換的變數);//小寫轉大寫 ``` <br> <font size=5>**範例一:轉小寫**</font> <font size=4>**Code:**</font> ```cpp= #include<iostream> using namespace std; int main() { char ch = 'G'; printf("%c in lowercase is represented as = %c", ch, tolower(ch)); return 0; } ``` <font size=4>**Output:**</font> ``` G in lowercase is represented as = g ``` <font size=5>**範例二:轉大寫**</font> <font size=4>**Code:**</font> ```cpp= #include<iostream> using namespace std; int main() { int i = 0; char c; char str[] = "Yabai Yiibai"; while(str[i]) { c = toupper(str[i]); cout << c; i++; } return(0); } ``` <font size=4>**Output:**</font> ``` YABAI POINT ``` <br> # 一維陣列 <font size=5>**寫法:**</font> ```cpp= 資料型態 陣列名稱[個數]; 資料型態 陣列名稱[個數]={初值0,初值1,初值2,......初值n-1}; //定義初值 資料型態 陣列名稱[]={初值0,初值1,初值2,......初值n-1}; //同上 資料型態 陣列名稱[個數]={初值}; //將陣列全元素設為初值 ``` * **作業系統會將陣列每一個值放在連續的記憶體上** <Br> # sizeof()、.size()、.length()、strlen()用法 <font size=5>**寫法:**</font> ```cpp= sizeof(變數); //求記憶體空間 字串.size(); //求字串長度 字串.length(); //求字串長度 strlen(字串); //求字串到字串結尾的長度 ``` <br> | | 作用 | 對象 | | -------- | -------- | -------- | | length()、size() | 求長度 | 陣列、字串 | | strlen() | 求包括空格的長度 | 字串 | | sizeof() | 求記憶體大小 | all | # get和getline <font size=5>**寫法:**</font> ```cpp= cin.getline(字串名稱,max字串長度,字串結束字元); //可讀取空白,不能讀取string cin.get(字元變數); getline(cin,字串物件); //可讀取空白,不能讀取char陣列 ``` <br> # find語法 <font size=5>**寫法:**</font> ```cpp= 字串名稱.find('找啥'); 字串名稱.find('找啥', 第幾個位置); ``` <br> # string # auto # map # substr字串分割 # setw()用法 # 指標 <font size=5>**寫法:**</font> ```cpp= 型態 *ptr; ptr=&指向變數名稱 ``` * **指標型態需與所指向變數型態<font color=red>一致</font>** * **<font color=red>&</font>為位址運算元** <BR> <font size=5>**範例一**</font> <font size=4>**Code:**</font> ```cpp= #include<iostream> using namespace std; int main() { int a[3]={5,7,9}; int sum=0; int *ptr=a; for(int i=0;i<3;i++) sum+=*(ptr++); cout << sum << endl; return(0); } ``` <font size=4>**Output:**</font> ``` 21 ``` <br> * **<font color=red>a</font>本身為a[0]的位址** * **<font color=red>\*ptr</font>為所指向之變數之值** # class # 建構元、解構元、拷貝建構元 ## Constructor(建構元) ## Destructor(解構元) ## Copy Constructor(拷貝建構元) **Copy Constructor允許你在創建一個object時,以另一個object作為其初始值。** <font size=5>**寫法:**</font> ```cpp= class MyClass { private: int x; public: MyClass(int i) : x(i) { } // Constructor MyClass(const MyClass& obj) : x(obj.x) { } // Copy Constructor ~MyClass() { } // Destructor }; ``` ## Copy Constructor與Dynamic Memory Allocation # Dynamic Memory Allocation(動態記憶體配置) <font size=5>**寫法:**</font> ```cpp= 指標變數 = new 指標的變數型別[記憶體大小]; delete 指標變數; //釋放記憶體 ```