# 20231016 課堂作業(CH5流程控制) ## 本章應用實例 (HW) ### 05-24電影分級制度 執行畫面: ![](https://hackmd.io/_uploads/BJ-OTPmfa.png) 程式碼: ``` #include<iostream> using namespace std; int main() { int age_range; cout << "請輸入年齡 (0~120):"; cin >> age_range; switch(age_range){ case 0 ... 5: cout << "可看普遍級"; break; case 6 ... 11: cout << "可看普遍級及保護級"; break; case 12 ... 17: cout << "可看限制級以外的影片"; break; case 18 ... 120: cout << "可看各級影片"; break; defauit: cout << "您並未輸入正確的年齡!"; } } ``` ### 05-27 猜數字遊戲 程式碼: ``` #include <iostream> using namespace std; int main() { int n, ans=7; while (n !=ans){ cout << "猜猜看數字是多少?(1~10):"; cin >> n; if (n < ans){ cout << "再大一點"; } else if(n > ans){ cout << "再小一點"; } } cout << "猜對了! YA!"; } ``` 實作畫面: ![image](https://hackmd.io/_uploads/H1ZtxJk_T.png) 執行畫面: ![image](https://hackmd.io/_uploads/Hy9rWkku6.png) ### 05-28 2的N次方 執行畫面: ![](https://hackmd.io/_uploads/Hy4v1OQG6.png) 程式碼: ``` #include <iostream> using namespace std; int main() { int n, p = 1, i = 1; cout << "請輸入n值以求得2的n次方值:"; cin >> n; while (i <= n){ p = p * 2; i++; } cout << "2的"<< n << "次方為:" << p; } ``` ### 05-29 成績加總 執行畫面: ![](https://hackmd.io/_uploads/ryhewwBza.png) 程式碼: ``` #include<iostream> using namespace std; int main() { int sum = 0, n = 0; do{ sum = sum + n; cout << "請輸入成績:"; cin >> n; }while(n !=-1); cout << "總分為:"<<sum; } ``` ### 5-31 顯示1~50的質數 程式碼: ``` #include <iostream> using namespace std; int main() { bool a = 1; for(int i = 2;i <= 50; i++){ for(int j=2;j <=i - 1; j++){ if(i % j==0){ a = 0; } } if(a == 1){ cout << i << " "; } a = 1; } } ``` 實作畫面: ![image](https://hackmd.io/_uploads/r1Y84Jyup.png) 執行畫面: ![image](https://hackmd.io/_uploads/Sk3PV1kd6.png) ### 05-32 階層計算機 程式碼: ``` #include <iostream> using namespace std; int main() { while(true){ cout << "請輸入 1-170 間的整數(輸入0即結束程式):"; int num = 0; cin >> num; if (num == 0) break; cout << num << "! 等於"; double fact; for (fact = 1; num > 0;num--) fact = fact *num; cout << fact << "\n\n"; } cout << "謝謝您使用階承計算機。"; } ``` 實作練習: ![image](https://hackmd.io/_uploads/B1aKeg1OT.png) 執行畫面: ![image](https://hackmd.io/_uploads/H1YRllyOT.png) ## 課後練習 ### 1.心理測驗(if...else if應用) 執行畫面: ![](https://hackmd.io/_uploads/SksNqQ9Zp.png) 程式碼: ``` #include<iostream> using namespace std; int main() { int chose; cout<<"發生船難,你選擇留下什麼?\n"; cout<<"選擇(1)留下老人和孕婦\n"; cout<<"選擇(2)留下老人和嬰兒\n"; cout<<"選擇(3)留下老人或金銀財寶\n"; cout<<"選擇(4)留下孕婦和嬰兒\n"; cout<<"選澤(5)留下孕婦和金銀財寶\n"; cout<<"選擇(6)留下嬰兒和金銀財寶\n"; cin >> chose; if (chose==1) cout <<"你的感情很細膩,善於觀察細節,在感情中,\n相比於戀人的甜言蜜語,你更在乎對方實際的行動。\n"; else if (chose==2) cout <<"在感情中,你不僅洞察力超好,第六感也很強,\n越是親近的人這種直覺越敏銳,所以另一半對你很難有所隱藏,因為你可以憑借著蛛絲馬跡得到你想要知道的消息。\n"; else if (chose==3) cout <<"你是個典型的顏控,在擇偶時很注重另一半的外表和身高。\n"; else if (chose==4) cout <<"面對感情你很承盾,一方面你很感性,渴望浪漫熱烈的愛情;\n另一方面你又很理性,明白現實的殘酷和金錢的重要性\n"; else if (chose==5) cout <<"在感情方面你很挑剔,所以很難遇到心動的對象。不過在戀愛時你卻很專一\n,一旦喜歡上某個人,你就會全心全意的對他好,所以和你談戀愛是一件很幸福的事。\n"; else if (chose==6) cout <<"在感情中你很缺乏安全感,有時候會忍不住通過試探或考驗的方式去確認對方是否愛你。\n"; else cout <<"亂輸入,活該一輩子舔狗命!!!"; } ``` ### 2. 判斷座標(x,y)在第幾象限。 (巢狀if…else 應用) 執行畫面: ![](https://hackmd.io/_uploads/HJlMzV9WT.png) 程式碼: ``` #include <iostream> using namespace std; int main() { int x, y; cout << "請輸入x值(不可輸入0):"; cin >> x; cout << "請輸入y值(不可輸入0):"; cin >> y; if(x > 0){ if(y > 0){ cout << "("<< x << "," << y << ")在第一象限"; }else{ cout << "(" << x << "," << y << ")在第四象限"; } }else { if(y>0){ cout <<"(" << x << ", " << y << ")在第二象限"; }else { cout << "(" << x << "," << y << ")在第三象限"; } } } ``` ### 3.用*組成三角形(for迴圈的應用) 執行畫面: ![](https://hackmd.io/_uploads/HyhvWIXGp.png) 程式碼: ``` #include<iostream> using namespace std; int main() { int s,t; cout << "請輸入層數:"; cin >> s; //正直角三角形 for(int i=1; i<=s; i++) { for(int k=1; k<=i; k++) { cout<<"*"; } cout<<endl; } cout<<endl; //倒直角三角形 for(int i=1; i<=s; i++) { for(int k=1; k<=s-i+1; k++) { cout<<"*"; } cout<<endl; } cout<<endl; //空白正直角三角形 for(int i=1; i<=s; i++) { for(int j=1; j<=s-i; j++) { cout<<" "; } for(int k=1; k<=i; k++) { cout<<"*"; } cout<<endl; } cout<<endl; //空白倒直角三角形 for(int i=1; i<=s; i++) { for(int j=1; j<=i-1; j++) { cout<<" "; } for(int k=1; k<=s-i+1; k++) { cout<<"*"; } cout<<endl; } } ``` ### 4.十二生肖查詢(switch…case的應用) 執行畫面: ![](https://hackmd.io/_uploads/BypVwV9Za.png) 程式碼: ``` #include <iostream> using namespace std; int main() { int year; cout << "請輸入民國幾年:"; cin >> year ; switch (year%12) { case 1 : cout << "鼠\n"; break; case 2: cout << "牛\n"; break; case 3 : cout << "虎\n"; break; case 4 : cout << "兔\n"; break; case 5 : cout << "龍\n"; break; case 6 : cout << "蛇\n"; break; case 7 : cout << "馬\n"; break; case 8 : cout << "羊\n"; break; case 9 : cout << "猴\n"; break; case 10 : cout << "雞\n"; break; case 11 : cout << "狗\n"; break; case 12 : cout << "豬\n"; break; } } ``` ### 5.閏年 執行畫面: ![](https://hackmd.io/_uploads/SyG1WU7Mp.png) 程式碼: ``` #include<iostream> using namespace std; int main() { int year; cout << "請輸入一個西元年"; cin >> year; if((year%4)==0) cout << year << "年是閏年" <<endl; else{ cout << year << "年非閏年" <<endl; } } ``` ### 6重複計算BMI(while 迴圈) 執行畫面: ![](https://hackmd.io/_uploads/r1WktP7Mp.png) 程式碼: ``` #include<iostream> using namespace std; int main() { char go_again = 'y'; float height, weight; while(go_again == 'Y' || go_again == 'y') { cout << "請輸入身高(公分):"; cin >> height; cout << "請輸入體重(公斤):"; cin >> weight; cout << "您的BMI為:" << weight / (height * height)*10000 << endl; cout << "要繼續計算另一位嗎?" << "(要請輸入Y或y,若不要請輸入非Y或y的字元):"; cin >> go_again; } } ``` ### 7計算兩整數的最大公因數(while 迴圈) 執行畫面: ![](https://hackmd.io/_uploads/SJoqFP7MT.png) 程式碼: ``` #include<iostream> using namespace std; int main() { int num1,num2,temp; cout << "計算兩整數的最大公因數\n"; cout << "請輸入第1個數字: "; cin >> num1; cout << "請輸入第2個數字: "; cin >> num2; while (num2!=0) { temp = num1%num2; num1 = num2; num2 = temp; } cout << "最大公因數是:" << num1; } ``` ### 8密碼檢驗程式 執行畫面: ![](https://hackmd.io/_uploads/S1naWd7GT.png) 程式碼: ``` #include<iostream> using namespace std; int main() { int pwd; int i=1; do{ if (i<=3){ cout << "請輸入第" << i <<"次ATM密碼:"; cin >> pwd;} if (i>3) { cout << "密碼輸入錯誤3次,無法登入。"; break;} if (pwd!=123) cout << "密碼錯誤,請再輸入一次...\n"; i++; }while (pwd!=123); if (pwd==123) cout << "歡迎進入本系統"; } ``` ## ch05心得感想:這一次的程式設計實習中,while跟for是我覺得最難又最重要的,首先,它們的使用方式就有一定的難度,需要複習好幾次才能懂,但我覺得它其中的邏輯才是最困難的,許多時候課本的範例我甚至不知道為什麼會那樣,然而,這部分又很重要,也就是說,我需要多複習,否則基礎不好,之後就麻煩了。