# 20231106 課堂作業(CH6函式) ## 隨堂練習 ### Ch06-21 以時間亂數種子產生5個500~1000的亂數 程式碼: ``` //訊一乙 19號 康軒勻 #include<iostream> #include<stdlib.h> #include<time.h> using namespace std; int main() { srand(time(0)); for (int i = 0; i < 5; i++) cout << (rand() %501) + 500 << endl; } ``` 程式實作畫面: ![image.png](https://hackmd.io/_uploads/HklMnCH7T.png) 執行結果: ![image.png](https://hackmd.io/_uploads/H1GH3CSX6.png) ## 應用實例練習 ### ch06-24 查詢語音費率程式 程式碼: ``` #include<iostream> using namespace std; void bill(int a){ switch(a){ case 196: cout << "語音費率每秒:0.0869元"; break; case 396: cout << "語音費率每秒:0.0782元"; break; case 796: cout << "語音費率每秒:0.0696元"; break; default: cout << "無此費率,請重新查詢!"; } } int main() { int a; cout << "請輸入要查詢的月租方案(196、396、796):"; cin >> a; bill(a); } ``` 程式實作畫面: ![image](https://hackmd.io/_uploads/ry_qdrdNT.png) 執行結果: ![image](https://hackmd.io/_uploads/SJun_SuVT.png) ### Ch06-25 猜數字遊戲 程式碼: ``` //訊一乙 19號 康軒勻 #include<iostream> #include<time.h> #include<stdlib.h> using namespace std; int number(){ int num = (rand()%10)+1; return num; } int main() { srand(time(0)); int n, ans; ans = number(); while (n !=ans){ cout << "猜猜看數字是多少?(1~10):"; cin >> n; if(n > ans) cout << "猜錯了,再小一點\n"; else if(n < ans) cout << "猜錯了,再大一點\n"; } cout << "猜對了!YA!"; } ``` 程式實作畫面: ![image.png](https://hackmd.io/_uploads/H1zyCAB7a.png) 執行結果: ![image.png](https://hackmd.io/_uploads/rk_NCCBmT.png) ### Ch06-26 猜拳遊戲 程式碼: ``` //訊一乙 19號 康軒勻 #include <iostream> #include <time.h> #include <stdlib.h> using namespace std; void game(int user){ int com = (rand() % 3)+1; if(user == com){ cout << "平手"; }else if(user == 1){ if(com ==2) cout << "你出剪刀,電腦出石頭,你輸了"; else cout << "你出剪刀,電腦出布,你贏了"; }else if(user == 2){ if(com == 3) cout << "你出石頭,電腦出布,你輸了"; else cout << "你出石頭,電腦出剪刀,你贏了"; }else if(user == 3){ if(com == 1) cout << "你出布,電腦出剪刀,你輸了"; else cout << "你出布,電腦出石頭,你贏了"; } } int main() { int user; srand(time(0)); cout << "請輸入你要出的拳(1.剪刀 2.石頭 3.布):"; cin >> user; game(user); } ``` 程式實作畫面: ![image.png](https://hackmd.io/_uploads/B1vQmyUXT.png) ![image.png](https://hackmd.io/_uploads/BkQNXkIXp.png) 執行畫面: ![image.png](https://hackmd.io/_uploads/r1nUXkImT.png) ### ch06-27 階乘函式 程式碼: ``` #include<iostream> using namespace std; double factorial(int); int main() { while(true){ cout << "請輸入 1-170間的整數(輸入0即結束程式):"; int num = 0; cin >> num; if (num == 0) break; cout << num << "! 等於 " << factorial(num) << endl; } cout << endl << "謝謝您使用階乘計算機。"; } double factorial(int n) { double fact; for(fact=1 ; n>0 ; n--) fact = fact * n; return fact; } ``` 實作畫面: ![image](https://hackmd.io/_uploads/Syzt6SOVa.png) 執行畫面: ![image](https://hackmd.io/_uploads/SyTcTSdET.png) ### Ch06-28 猜點數遊戲 程式碼: ``` //訊一乙 19號 康軒勻 #include<iostream> #include<time.h> #include<stdlib.h> using namespace std; int dice(); int main() { srand(time(0)); int num1, num2; cout << "輸入1~6其中一個數字,猜骰子點數:"; cin >> num1; while(num1!=0){ num2 = dice(); cout << "骰子的點數是:"<< num2 << "\n"; if (num2 == num1) cout << "猜中了"; else cout << "沒猜中"; cout << ",再試一次吧!\n" << "輸入1~6其中一個數字,猜骰子點數(或輸入0已結束遊戲):"; cin >> num1; } } int dice() { int a; a = (rand() %6)+1; return a; } ``` 程式實作畫面: ![image.png](https://hackmd.io/_uploads/HJUo8y8mp.png) 執行畫面: ![image.png](https://hackmd.io/_uploads/Hkmu8kU7p.png) ## 課後練習 ### 1.組成三角形 程式碼: ``` #include<iostream> #include<cmath> using namespace std; int triangle (double,double,double); int main() { double a,b,c,t; cout << "請輸入三角形的三個邊長:\n"; cin >> a; cin >> b; cin >> c; t = triangle(a,b,c); if(t == 1){ cout << a << "、" << b << " 、 " << c << "可組成三角形\n"; }else{ cout << a << "、" << b << " 、 " << c << "無法組成三角形\n"; } } int triangle (double a, double b, double c){ if ((a+b) > c &&(b+c)>a&&(a+c)>b){ return 1; }else{ return 0; } } ``` 實作畫面: ![image](https://hackmd.io/_uploads/Sy4638uVp.png) 執行畫面: ![image](https://hackmd.io/_uploads/B1By6UOVp.png) ### 2.遞迴 程式碼: ``` //資訊一乙 康軒勻 #include<iostream> using namespace std; int main() { void func(); func(); } void func() { cout << "這是一種遞回函式\n"; func(); } ``` 實作畫面: ![image](https://hackmd.io/_uploads/SJqKvn9BT.png) 執行畫面: ![image](https://hackmd.io/_uploads/BJyiwnqBp.png) ### 3.專案練習 程式碼: ``` #include <iostream> #include <cstdlib> #include <ctime> using namespace std; void game(int user); int dice(); void func(); void p01(); void p02(); void p03(); void p04(); void p05(); void p06(); void p07(); int main() { string menuItem[] = { "[1]猜數字遊戲", "[2]猜拳遊戲", "[3]猜點數遊戲", "[4]查詢語音費率函式", "[5]階乘函式", "[6]組成三角形", "[7]遞迴", "[0]離開" }; int i, num; int selMenu = 99; while (selMenu != 0) { system("chcp 950"); // 編碼 system("cls"); // 清除畫面 cout << "程式設計實習 資訊一乙 19 康軒勻\n"; cout << "-----------------------------" << endl; int arrLength = sizeof(menuItem) / sizeof(menuItem[0]); // 讀取幾個Item for (i = 0; i < arrLength; i++) { cout << menuItem[i] << endl; } cout << "請輸入選項:"; cin >> selMenu; switch (selMenu) { case 1: p01(); break; case 2: p02(); break; case 3: p03(); break; case 4: p04(); break; case 5: p05(); break; case 6: p06(); break; case 7: p07(); break; } system("PAUSE"); cout << "\n"; } } void p01() { srand(time(0)); int n, ans; ans = (rand() % 10) + 1; while (n != ans) { cout << "猜猜看數字是多少?(1~10):"; cin >> n; if (n > ans) cout << "猜錯了,再小一點\n"; else if (n < ans) cout << "猜錯了,再大一點\n"; } cout << "猜對了!YA!\n"; } void p02() { int user; srand(time(0)); cout << "請輸入你要出的拳(1.剪刀 2.石頭 3.布):"; cin >> user; game(user); } void game(int user) { int com = (rand() % 3) + 1; if (user == com) { cout << "平手"; } else if (user == 1) { if (com == 2) cout << "你出剪刀, 電腦出石頭, 你輸了"; else cout << "你出剪刀, 電腦出布, 你贏了"; } else if (user == 2) { if (com == 3) cout << "你出石頭, 電腦出布, 你輸了"; else cout << "你出石頭, 電腦出剪刀, 你贏了"; } else if (user == 3) { if (com == 1) cout << "你出布, 電腦出剪刀, 你輸了"; else cout << "你出布, 電腦出石頭, 你贏了"; } } void p03() { srand(time(0)); int num1, num2; cout << "輸入1~6其中一個數字,猜骰子點數:"; cin >> num1; while (num1 != 0) { num2 = dice(); cout << "骰子的點數是:" << num2 << "\n"; if (num2 == num1) cout << "猜中了"; else cout << "沒猜中"; cout << ",再試一次吧!\n" << "輸入1~6其中一個數字,猜骰子點數(或輸入0已結束遊戲):"; cin >> num1; } } int dice() { return (rand() % 6) + 1; } void bill(int a) { switch (a) { case 196: cout << "語音費率每秒:0.0869元"; break; case 396: cout << "語音費率每秒:0.0782元"; break; case 796: cout << "語音費率每秒:0.0696元"; break; default: cout << "無此費率,請重新查詢!"; } } void p04() { int a; cout << "請輸入要查詢的月租方案(196、396、796):"; cin >> a; bill(a); } double factorial(int n) { double fact; for (fact = 1; n > 0; n--) fact = fact * n; return fact; } void p05() { while (true) { cout << "請輸入 1-170間的整數(輸入0即結束程式):"; int num = 0; cin >> num; if (num == 0) break; cout << num << "! 等於 " << factorial(num) << endl; } cout << endl << "謝謝您使用階乘計算機。"; } int triangle(double a, double b, double c) { if ((a + b) > c && (b + c) > a && (a + c) > b) { return 1; } else { return 0; } } void p06() { double a, b, c; cout << "請輸入三角形的三個邊長:\n"; cin >> a; cin >> b; cin >> c; if (triangle(a, b, c) == 1) { cout << a << "、" << b << " 、 " << c << "可組成三角形\n"; } else { cout << a << "、" << b << " 、 " << c << "無法組成三角形\n"; } } void p07() { cout << "這是一種遞回函式\n"; func(); } void func() { cout << "這是一種遞回函式\n"; p07(); } ``` 實作畫面: ![image](https://hackmd.io/_uploads/Hk7vq2awT.png) ![image](https://hackmd.io/_uploads/SJVuch6Da.png) ![image](https://hackmd.io/_uploads/B1gFq2avT.png) ![image](https://hackmd.io/_uploads/HkCt92Tw6.png) ![image](https://hackmd.io/_uploads/Sk5q93pv6.png) ![image](https://hackmd.io/_uploads/Hkrj5hpDT.png) ![image](https://hackmd.io/_uploads/SyW393pv6.png) ![image](https://hackmd.io/_uploads/BysAq2pD6.png) 執行畫面: ![image](https://hackmd.io/_uploads/S19u2npwa.png) ## 心得感想(11/06) 在本次的程式設計實習中,我認為難度頗高的,因為這章節不僅僅加了許多複雜的東西,更要背許多的函式及公式,前面的程式基礎更是要好,然而,到後面只會越來越難而已,所以我們上課更該認真專心,回家後也要花些許時間複習才能跟得上. ## 心得感想(11/20) 這是老師第一次要我們做專案練習,說實話這個東西我一開始完全不會也不懂,尤其是除錯時它顯示的錯誤訊息我也根本沒看過,導致我不知道如何修改我的程式,好在老師最後有教我們怎麼做,我們才順利把它做出來.