int Maxint int double Maxdouble double double Areadouble
函數的重載 int Max(int, int); double Max(double, double); // 參數的資料型態不同 double Area(double x); double Area(double x, double y); // 參數的數目不一樣 3
範例程式 Overload. cpp: 函數 的重載 // Overload. cpp #include <iostream> using namespace std; // 函數的原型 double Max(double, double); // 參數的數目不一樣 4
//------- 主程式 ----------------int main() { double a=5. 0, b=6. 0, c=7. 0; cout << "Max(a, b) 的� 是: " << Max(a, b) << endl; cout << "Max(a, b, c) 的� 是: " << Max(a, b, c) << endl; return 0; } 5
// --- 函數的定義 -------------double Max(double x, double y) { return (x>=y) ? x : y; } double Max(double x, double y, double z) { double Temp = x; if (x<y) Temp = y; if (Temp<z) Temp = z; return Temp; } 程式執行結果 6 Max(a, b) 的� 是: 6 Max(a, b, c) 的� 是: 7
預設引數 (default arguments) 函數原型: double Area(double Width, double Length = 12. 0); n 函數定義(兩個參數的乘積): double Area(double Width, double Length) { return Width*Length; } A = Area(6. 5); // 只用一個引數呼叫 n 7
範例程式 Default. cpp: 預設引數 8 // Default. cpp #include <iostream> using namespace std; // --- 宣告函數Area() ----------double Area(double Width, double Length = 12. 0); // --- 主程式 --------------int main() { double A; A = Area(6. 5); // 只用一個參數呼叫 return 0; } // --- 定義函數Area() ----------double Area(double Width, double Length) { return Width*Length; }
樣版函數可以用來取代函數的重載 template<class T> T Abs(T x) {return (x>0)? x : -x; } n 相當於同時定義了三個函數: int Abs (int x) {return (x>0)? x : -x; } float Abs (float x) {return (x>0)? x : -x; } double Abs (double x) {return (x>0)? x : x; } 12
範例程式 Test. Dice. cpp 以inline 函數 Rand. I() 為基礎,寫成一個函數Test. Dice() 以模擬擲骰子的 現象 (骰子的值為整數 1~6)。 15 // Test. Dice. cpp #include <iostream> #include <iomanip> #include <ctime> using namespace std; inline int Rand. I(int N) // 定義 inline 函數 Rand. I() { return rand()%N+1; } void Test. Dice(); // 宣告函數 Test. Dice() const int Test. Num = 6000;
//------- 主程式 -----------int main() { cout << right << fixed << showpoint << setprecision(4); cout << "RAND_MAX (0 x 7 FFFU) 的值是 : " << setw(7) << RAND_MAX << endl; Test. Dice(); return 0; } 16
17 //------- 定義函數 Test. Dice() ----void Test. Dice() { int Freq[6], Face, i; for (i=0; i<6; i++) Freq[i]=0; // (1) 初始化 srand(int(time(0))); // (2) 連擲 20 次 cout << "連擲 20 次的結果: " << endl; for (i=1; i<=20; i++) { cout << setw(5) << Rand. I(6); if (i%5 == 0) cout << endl; } cout << endl;
18 // (3) 統計連擲 Test. Num 次的結果 for (int Roll=0; Roll< Test. Num; Roll++) { Face = Rand. I(6); Freq[Face-1]++; } cout << " 點數 次數 " << endl; cout << "--------- " << endl; for (i=0; i<6; i++) cout << setw(5) << (i+1) << setw(10) << Freq[i] << endl; cout << "--------- " << endl; }
程式執行結果 19 RAND_MAX (0 x 7 FFFU) 的� 是: 32767 LRAND_MAX (0 x 7 FFFFFFFU) 的� 是: 2147483647 連擲 20次的結果: 1 1 4 3 4 2 6 2 5 1 4 6 1 4 5 2 2 3 1 4 點數 次數 ---------1 1002 2 1027 3 1005 4 980 5 972 6 1014 ---------
隨機獲得一個介於 0到 1之間的 浮點數 inline double Rand() {return double(rand())/RAND_MAX; } 20
#include <iostream> #include <iomanip> #include <stdlib> using namespace std; // 定義 inline 函數 Rand() 產生 0 ~ 1 之間的亂數 inline double Rand() {return double(rand())/RAND_MAX; } //------- 宣告函數 Test. Rand() ----void Test. Rand(); const int Test. Num = 6000; //------- 主程式 -----------int main() { cout << right << fixed << showpoint << setprecision(4); Test. Rand(); return 0; } 22
//------- 定義函數 Test. Rand() ----void Test. Rand() { int Freq[10]; double Sum, Temp; // (1) 初始化 srand(int(time(0))); // 也可寫成 randomize(); for (int i=0; i<10; i++) Freq[i]=0; // (2) 連試 20 次 Rand() cout << "連試 20 次 Rand(): " << endl; for (int i=1; i<=20; i++) { cout << setw(10) << Rand(); if (i%5 == 0) cout << endl; } cout << endl; // (3) 統計連試 Test. Num 次的結果 Sum = 0. 0; 23
24 for (int Roll=0; Roll< Test. Num; Roll++) { Temp = Rand(); for (int i=1; i<=10; i++) if ((Temp<=i*0. 1) && (Temp>(i-1)*0. 1)) Freq[i-1]++; Sum += Temp; } cout << " 範圍 次數 " << endl; cout << "----------------- " << endl; for (int i=1; i<=10; i++) cout << ((i-1)*0. 1) << " < Rand() <= " << (i*0. 1) << setw(6) << Freq[i-1] << endl; cout << "----------------- " << endl; cout << "平均: " << Sum/double(Test. Num) << endl; return; }
程式執行結果 連試 20次Rand(): 0. 1886 0. 3008 0. 1375 0. 7226 0. 7928 0. 5995 0. 9930 0. 6493 25 0. 6612 0. 3215 0. 7400 0. 0373 0. 6320 0. 0080 0. 4369 0. 8551 範圍 次數 ----------------0. 0000 < Rand() <= 0. 1000 605 0. 1000 < Rand() <= 0. 2000 601 0. 2000 < Rand() <= 0. 3000 560 0. 3000 < Rand() <= 0. 4000 600 0. 4000 < Rand() <= 0. 5000 602 0. 5000 < Rand() <= 0. 6000 648 0. 6000 < Rand() <= 0. 7000 550 0. 7000 < Rand() <= 0. 8000 643 0. 8000 < Rand() <= 0. 9000 608 0. 9000 < Rand() <= 1. 0000 583 ----------------平均: 0. 5014 0. 0216 0. 6875 0. 8018 0. 6647
範例程式 Factorial. cpp: 遞迴 函數Factorial() 27 // Factorial. cpp #include <iostream> using namespace std; // --- 宣告遞迴函數 Factorial() ----------int Factorial(int); // ---- 主程式 ------------------int main() { int N; cout << "請輸入一個 12 以下的正整數: "; cin >> N; if ( N < 0 ) cout << "錯誤! 您輸入了負數. " << endl; else cout << N << " ! = " << Factorial( N ) << endl; return 0; }
// --- 定義遞迴函數 Factorial() ----------int Factorial(int N) { if ( N <= 1 ) return 1; else return N * Factorial(N-1); // 呼叫自己! } 程式執行結果 28 請輸入一個 12 以下的正整數: 10 10 ! = 3628800
// --- 定義函數 GCD() ------------int GCD(int M, int N ) { if ( (M%N) == 0 ) return N; else return GCD(N, M%N); } 31
範例程式 Bubble. cpp // Bubble. cpp #include <iostream> #include <iomanip> using namespace std; // --- 定義樣版函數 Exchange() -----------template <class T> void Exchange(T& A, T& B) { T Temp; Temp = A; A=B; B=Temp; } 33 template <class T> // --- 定義樣版函數 Bubble() void Bubble(T *V, int N ) { for (int i=0; i<N; i++) for (int j=N-1; j>i; j--) if (V[j]<V[j-1]) Exchange(V[j], V[j-1]); return; }
inline double Rand() // 定義 inline 函數 Rand() {return double(rand())/RAND_MAX; } // ---- 主程式 ------------------int main() { srand(int(time(0))); ; const int Size = 20; double Data[Size]; for (int i=0; i<20; i++) Data[i]= 10. 0*Rand()-5. 0; // 隨機產生 Data[i] cout << setiosflags(ios: : right) << setiosflags(ios: : fixed) << setiosflags(ios: : showpoint)<< setprecision(4); cout << "n執行 Bubble() 之前, Data 的值是: n"; for (int i=0; i<Size; i++) { cout << setw(10) << Data[i]; if (i%5 == 4) cout << endl; } 34
Bubble(Data, Size); cout << "n執行 Bubble() 之後, Data 的 值是: n" for (int i=0; i<Size; i++) { cout << setw(10) << Data[i]; if (i%5 == 4) cout << endl; } return 0; } 35
程式執行結果 執行Bubble() 之前, Data的� 是: 1. 9149 -2. 1917 -3. 7524 0. 3801 4. 9835 -2. 7267 -0. 1643 0. 2831 1. 3082 2. 4410 執行Bubble() 之後, Data的 4. 0301 � 是: 1. 3158 0. 8391 -2. 1691 -4. 0802 -3. 8464 3. 7524 -2. 7267 2. 6949 -2. 1917 -2. 1691 2. 1551 -1. 3640 0. 1643 -4. 0802 - 2. 1551 -2. 6949 4. 3005 36 -1. 3640 -
範例程式 檔案 Linear. Search. cpp // Linear. Search. cpp #include <iostream> #include <iomanip> #include <stdlib> using namespace std; // --- 定義樣版函數 Lin. Search() -----------template <class T> int Lin. Search(T *V, int N , T Key) { for (int i=0; i<N; i++) { if (V[i] == Key) return i; } return -1; } 38
// -- 定義 inline 函數 Rand. I() 產生 1~N 之間的亂數 -----inline int Rand. I(int N) {return rand()%N+1; } 39
// ---- 主程式 -------------------int main() { int Index; int Key = 32; srand(int(time(0))); const int Size = 20; int Data[Size]; for (int i=0; i<20; i++) Data[i]= Rand. I(85); // 隨機產生 Data[i] cout << "n. Data 的值是: " << endl; 40
for (int i=0; i<Size; i++) { cout << setw(10) << Data[i]; if (i%5 == 4) cout << endl; } Index = Lin. Search(Data, Size, Key); if (Index > -1) cout << Key << " 在第 " << Index+1 << " 個元素的地方. " << endl; else cout << "找不到 " << Key << endl; return 0; } 41
程式執行結果 (第一次執行結果) Data的� 是: 74 72 35 7 46 10 47 49 找不到 32 (第二次執行結果) Data的� 是: 52 31 83 5 58 17 67 80 32在第 18個元素的地方 42 82 9 27 18 16 20 65 58 29 43 7 50 27 33 61 32 30 33 39 79 73 67 84 52
當 (Left <= Right) 時執行下列敘述 { Mid = (int) ((Left+Right)/2); if (Key == Data[Mid]) 傳回 Mid; else if (Key > Data[Mid]) Left = Mid +1; else Right = Mid -1; } 傳回 -1; 44
範例程式 檔案 Binary. Search. cpp // --- 定義樣版函數 Bubble() -----------template <class T> void Bubble(T *V, int N ) { for (int i=0; i<N; i++) for (int j=N; j>i; j--) if (V[j]<V[j-1]) Exchange(V[j], V[j-1]); return; } 45
46 // ---- 主程式 ------------------int main() { int Index, Key = 32; srand(int(time(0))); const int Size = 20; int Data[Size]; for (int i=0; i<20; i++) Data[i]= Rand. I(85); // 隨機產生 Data[i] cout << "n. Data 原來的值是: n"; for (int i=0; i<Size; i++) { cout << setw(10) << Data[i]; if (i%5 == 4) cout << endl; }
47 Bubble(Data, Size); cout << "n執行 Bubble() 之後, Data 的 值是: n"; for (int i=0; i<Size; i++) { cout << setw(10) << Data[i]; if (i%5 == 4) cout << endl; } Index = Bi. Search(Data, Size, Key); if (Index > -1) cout << Key << " 在第 " << Index+1 << " 個元素的地方. n"; else cout << "找不到 " << Key << endl; return 0; }
- Slides: 47