# 2023/09/18作業 ## Ch03-18讓使用者從鍵盤輸入資料 ``` #include <iostream> using namespace std; int main() { int grade; int student_ID; cout << "請問你是幾年級的學生 :"; cin >> grade; cout << "請問你的學號是 :"; cin >> student_ID; cout << "你是" << grade << "年級的學生" << endl; cout << "學號是" << student_ID; } ``` ![](https://hackmd.io/_uploads/rkWN7_By6.png) 輸出結果: ![](https://hackmd.io/_uploads/B1bqfurJp.png) ## CH03-19 換匯程式 ``` //訊一乙 36號 潘誠 #include <iostream> using namespace std; #define US 29.5 //定義巨集常數 美金匯率 #define JP 20.277 //定義巨集常數 美金匯率 int main() { double money; cout << "請輸入要兌換的金額:"; cin >> money; cout << "可兌換" << money / US << "美金\n"; cout << "或兌換" << money / JP << "日幣"; } ``` ![](https://hackmd.io/_uploads/rJdMzOHyT.png) 輸出結果: ![](https://hackmd.io/_uploads/S174GuB1a.png) ## Ch03-20 交換兩變數的值 ``` //訊一乙 36號 潘誠 #include <iostream> using namespace std; int main() { int a = 10, b=20; //宣告兩個 int 變數a, b int temp; // 宣告暫存整數變數 temp cout << "交換前 a="<< a << "\t b ="<< b << endl; temp = a; // 將變數的值指定給暫存變數 a = b; // 將暫存變數所存的值指定給 b b = temp; // 把變數b 的值指定給a cout << "交換後a="<< a << "\t b = "<< b << endl; } ``` ![](https://hackmd.io/_uploads/BkqUN_Ska.png) 輸出結果: ![](https://hackmd.io/_uploads/H1gKN_SJ6.png) ## Ch03-21 大小寫轉換 ``` //訊一乙 36號 潘誠 #include <iostream> using namespace std; int main() { char upper = 'S', lower; //宣告兩個字元變數 lower = upper + 32; cout << upper << " 的小寫是 " << lower << endl; } ``` ![](https://hackmd.io/_uploads/B1dqHOS1T.png) 輸出結果: ![](https://hackmd.io/_uploads/S1UsS_BJp.png) # 心得感想 情從複雜變得難以理解,程式越來越多,每個程式的意思也越來越複雜,腦袋快炸了,甚麼define char upper temp 宣告 字面常數 變數 浮點常數 字元 布林型別 唯讀變數 真的是有看但都沒有懂,看來我的C++之路還非常漫長,還有很多要學呢。 # 實作練習 ## P74_1 ``` //訊一乙 36號 潘誠 #include <iostream> using namespace std; #define SIZE 10 int main() { cout << SIZE ; } ``` ![](https://hackmd.io/_uploads/HkQQF_ryT.png) 輸出結果: ![](https://hackmd.io/_uploads/ByqNKdSJp.png) ## P74_2 ``` //訊一乙 36號 潘誠 #include <iostream> using namespace std; #define SIZE 10 int main() { float b; int a = 10; b = 101.7; char c = 'c' ; cout << "a="<<a<<"\t b = "<<b<< "\t c="<< c <<endl; } ``` ![](https://hackmd.io/_uploads/HyDaCur1p.png) 輸出結果: ![](https://hackmd.io/_uploads/H1wXyFrJp.png) ## P74_5 ``` //訊一乙 36號 潘誠 #include <iostream> using namespace std; #define SIZE 10 int main() { cout << "我正在學習 \"C++\" 程式語言" ; } ``` ![](https://hackmd.io/_uploads/SJaEktBJp.png) 輸出結果: ![](https://hackmd.io/_uploads/BkarkYHyp.png) ## P74_7 ``` //訊一乙 36號 潘誠 #include <iostream> using namespace std; int main() { int a = 10, b = 20, c = 30; int temp; int temp2; cout << "交換前 a = "<<a<<",\tb = "<<b<<",\tc = "<<c<<endl; temp = a; a = c; temp2 = b; b = temp; c = temp2; cout << "交換後 a = "<<a<<",\tb = "<<b<<",\tc = "<<c<<endl; } ``` ![](https://hackmd.io/_uploads/HJ3vltHJT.png) 輸出結果: ![](https://hackmd.io/_uploads/rkD3lYHJ6.png) # 2023/09/23 課堂作業(CH4運算子與運算式) ## Ch04-05 前置與後置遞增運算子 ``` //訊一乙 36號 潘誠 #include <iostream> using namespace std; int main() { int i = 100 , j ; j = (i++) + 5; // 後置增遞 std::cout << "i = "<< i << "\t\tj = "<< j; i = 100; j = (++i) + 5; // 前置增遞 std::cout << "\ni = "<< i << "\t\tj = "<< j; } ``` ![](https://hackmd.io/_uploads/S12REbhJT.png) 輸出結果: ![](https://hackmd.io/_uploads/HJeWrZhyT.png) ## Ch04-06 關係運算子練習 ![](https://hackmd.io/_uploads/BkMHBb3kT.png) ``` //訊一乙 36號 潘誠 #include<iostream> using namespace std; int main() { int i = 3,j = 3; cout << boolalpha; // 改用文字的方式輸出布林值 cout << "(i == j) :" << (i == j)<< endl; cout << "(i > j) :" << (i > j) << endl; cout << "(++i > j):" << (++i > j) << endl; cout << "(j-- < 3):" << (j-- < 3) << endl; cout << "(i != j) :" << (i != j) << endl; } ``` ![](https://hackmd.io/_uploads/SkPJOZ2y6.png) 輸出結果: ![](https://hackmd.io/_uploads/HJgWub2J6.png) ## Ch04-07 邏輯運算子練習 ![](https://hackmd.io/_uploads/SJaNObnJp.png) ``` //訊一乙 36號 潘誠 #include<iostream> using namespace std; int main() { int i = 3; bool b = false; cout << boolalpha; // 改用文字的方式輸出布林值 cout << "i = " << i << "\tb =" << b << endl; cout << "i && b:" << (i && b) << endl; cout << "i || b:" << (i || b) << endl; cout << "i &&!b:" << (i &&! b) << endl; cout << "i ||!b:" << (i ||!b) << endl; } ``` ![](https://hackmd.io/_uploads/Hk41qb2ka.png) 輸出結果: ![](https://hackmd.io/_uploads/ryk0tZhJT.png) # 心得感想 前置遞增運算子(++i)會先將變數增加,然後返回新值,而後置遞增運算子(i++)則會返回原始值,然後再增加。這對於迭代和循環控制非常有用 關係運算子(例如==、!=、>、<等)用於比較變數之間的值,返回布林結果。這對於控制流程和條件判斷非常重要,確保程式按照預期的方式執行 邏輯運算子(例如&&、||、!等)則用於組合多個條件,並生成更複雜的判斷式。它們有助於簡化程式碼並增加可讀性,同時保持邏輯的一致性。 總之,說了這麼多我還是看不懂 # 2023/09/25 課堂作業(CH4運算子與運算式) ## Ch04-08 位元運算子練習 ![](https://hackmd.io/_uploads/rkFJ2sCkT.png) ``` //訊一乙 36號 潘誠 #include<iostream> using namespace std; int main() { short i = 9,j = 25; cout << "i >> 1 = " << (i >> 1) << endl; cout << "i << 1 = " << (i << 1) << endl; cout << "~i = " << (~i) << '\n' << endl; cout << "i = " << i << "\tj =" << j << endl; cout << "i & j = " << (i & j) << endl; cout << "i | j = " << (i | j) << endl; cout << "i ^ j = " << (i ^ j) << endl; } ``` ![](https://hackmd.io/_uploads/BJNQnoR1p.png) 輸出結果: ![](https://hackmd.io/_uploads/SyHS3oA1T.png) 位元運算子練習_運算過程: ![](https://hackmd.io/_uploads/HkKDb3CJ6.png) ![](https://hackmd.io/_uploads/BJ3Fb3Ayp.png) ![](https://hackmd.io/_uploads/HJLobhAJp.png) ![](https://hackmd.io/_uploads/HJ8h-hA1a.png) ![](https://hackmd.io/_uploads/ByCn-2R1p.png) ![](https://hackmd.io/_uploads/rJvCWhCk6.png) ## 應用實例練習(P.98) ## Ch04-15 模擬換幣機 ``` //訊一乙 36號 潘誠 #include<iostream> using namespace std; int main() { int money,ten,five,one; cout << "請輸入您的金幣金額 : "; cin >> money; ten = money / 10; //計算拾圓硬幣的個數 five = (money%10)/5; //計算伍圓硬幣的個數 one = (money%10)%5; //計算壹圓硬幣的個數 cout << money << " 元共可兌換零錢 : " << "拾圓" << ten << "個 " << "伍圓" << five <<"個 " << "壹圓" << one << "個 "; } ``` ![](https://hackmd.io/_uploads/HyDsI3CJ6.png) 輸出結果: ![](https://hackmd.io/_uploads/S1PAHnCka.png) ## Ch04-16 華氏溫度轉攝氏溫度 ``` //訊一乙 36號 潘誠 #include<iostream> using namespace std; int main() { float C, F; cout << "請輸入華氏溫度 : "; cin >> F; C = (F - 32) * 5/9; //溫度轉換公式的算式 cout << "換算成攝氏溫度為 " << C << " 度" ; } ``` ![](https://hackmd.io/_uploads/SynBwh0yT.png) 輸出結果: ![](https://hackmd.io/_uploads/SkUPP2Ak6.png) ## Ch04-18 成績計算程式 ``` //訊一乙 36號 潘誠 #include<iostream> using namespace std; int main() { int chinese, english, math; cout << "請輸入3科成績 : \n"; cin >> chinese; cin >> english; cin >> math; cout << "平均是" << double(chinese + english + math) / 3; } ``` ![](https://hackmd.io/_uploads/Byc9u3Rk6.png) 輸出結果: ![](https://hackmd.io/_uploads/rJXh_2AJ6.png) # 實作練習 ## 攝氏溫度轉華氏溫度 ``` //訊一乙 36號 潘誠 #include<iostream> using namespace std; int main() { float C, F; cout << "請輸入攝氏溫度 : "; cin >> C; F = C * 9/5 + 32; cout << "換算成華氏溫度為 " << F << " 度" ; } ``` ![](https://hackmd.io/_uploads/Bye3Fh0Jp.png) 輸出結果: ![](https://hackmd.io/_uploads/r1ETt2Aya.png) ## 自動販售機(幣值轉換練習) ``` //訊一乙 36號 潘誠 #include<iostream> using namespace std; int main() { int money,fifty,ten,five,one; cout << "請輸入您的金幣金額 : "; cin >> money; fifty = money / 50; ten = (money%50)/10; five = ((money%50)%10)/5; one = ((money%50)%10)%5; cout << money << " 元共可兌換零錢 : " << "伍拾圓" << fifty << "個 " << "拾圓" << ten << "個 " << "伍圓" << five <<"個 " << "壹圓" << one << "個 "; } ``` ![](https://hackmd.io/_uploads/ryT_ohAJa.png) 輸出結果: ![](https://hackmd.io/_uploads/ByqYo3Ay6.png) # 心得感想 十進位和二進位制度在計算機科學中扮演著極為重要的角色。這兩種制度之間的轉換對於理解電腦內部運作至關重要。 在C++中,我們可以使用運算子來處理二進位數字,例如AND、OR和XOR。 總體來說,了解十進位和二進位以及C++的運算子,不僅有助於我更深入地理解電腦的科學原理,還可以讓我更高效率的完成工作,真的是太神啦老鐵666! # 2023/10/16~23 課堂作業(CH5流程控制) ![](https://hackmd.io/_uploads/Bk3Sfi7MT.png) ## 1. if 條件判斷控制 ### 語法 - if條件式 ``` if ( 條件運算式 ) { 如果條件成立時做什麼... } ``` ### 語法 - if-else條件式 ``` if( 條件運算式 ) { 如果條件成立時做什麼... } else { 否則做什麼... } ``` ### 語法 - 巢狀if…else ``` if (條件運算式1){ if (條件運算式2) 程式區塊1 else 程式區塊2 } else{ if (條件運算式3) 程式區塊3 else 程式區塊4 } ``` ### 語法 - if…else if ``` if (條件運算式1){ {程式區塊1} // 條件運算式1成立時 else if (條件運算式2) {程式區塊2} // 條件運算式2成立時 else if (條件運算式n) {程式區塊n} // 條件運算式n成立時 else {程式區塊m} // 其他狀況 } ``` ### 語法 - 條件運算子 ``` (條件運算式) ? (敘述1) : (敘述2) ``` ## 2.switch多條件分支敘述 ``` switch(條件運算式){ case 條件值 1: 程式區塊1 break; case 條件值 2: 程式區塊2 break; case 條件值 N: 程式區塊N break; default: } ``` ## 3.迴圈 ### 語法 - for ``` for(初始運算式; 條件運算式; 控制運算式){ 迴圈內的敘述; } ``` ### 語法 - while ``` while(條件運算式){ 迴圈內的敘述 } ``` ### 語法 - do while ``` do{ 敘述... // 先執行一次迴圈 }while(條件運算式); // 再檢查條件運算式是否成立 ``` ### 語法 - for ``` for(初始運算式; 條件運算式; 控制運算式){ 迴圈內的敘述; } ``` ## 綜合練習 ### 1.夏季電費計算 ![](https://hackmd.io/_uploads/SJpSWjXM6.png) ``` 夏季電費,請輸入用電度數:800 本月電費為:2836.8 元 ``` ``` 夏季電費,請輸入用電度數:600 本月電費為:1773.8 元 ``` ``` int main() { int n; float money; cout << "夏季電費,請輸入用電度數:"; cin >> n; if (n<=120) money = n*1.63; else if (n>=121 && n<=330) money = 120*1.63 + (n-120)*2.38; else if (n>=331 && n<=500) money = 120*1.63+(330-120)*2.38+(n-330)*3.52; else if (n>=501 && n<=700) money = 120*1.63+(330-120)*2.38+(500-330)*3.52+(n-500)*4.8; else if(n>=701 && n<=1000) money = 120*1.63+(330-120)*2.38+(500-330)*3.52+(700-500)*4.8+(n-700)*5.83; else money = 120*1.63+(330-120)*2.38+(500-330)*3.52+(700-500)*4.8+(1000-700)*5.83+(n-1000)*7.69; cout<<"本月電費為:"<<money<<endl; } ``` ### 2.成績加總 將使用者輸入的成績以do…while迴圈進行加總,直到使用者輸入-1,才顯示總分。 ``` #include <iostream> using namespace std; int main() { int sum = 0, n = 0; do{ sum = sum + n; cout << "請輸入成績:"; cin >> n; } while(n != -1); cout << "總分為:" << sum; } ``` ### 3.判斷奇數或偶數 請撰寫一個程式,使用if比較運算式,判斷使用者輸入的數值為奇數或偶數 ``` #include<iostream> using namespace std; int main() { int i; cout << "請輸入一個整數:"; cin >> i; if ( i % 2 == 0 ) { cout << i << "為偶數"; }else{ cout << i << "為奇數"; } } ``` ### 4.可被3整除得總和+ 請撰寫一個程式,可計算出1到某正整數(由使用者輸入)間,所有可被3整除的數值之總和。並請加上可讓使用者決定是否再算一次的功能。 ``` #include <iostream> using namespace std; int main(){ int num, total; cout << "請輸入一正整數以求得1到該數值可被3整除的數值之總和:"; cin >> num; while ( num!=0 ) { if ( num % 3 == 0 ) total = total + num; num--; if ( num == 0 ) { cout << total << endl; cout << "是否要再算一次(要請輸入數值,不要則輸入0):"; cin >> num; total = 0; } } } ``` ### 5.購物折扣 某購物網網站在使用者購物時,會依據使用者的身份(會員或非會員)給予不同等級的折扣,而會員又可分3個等級,折扣方式如下圖所示: ![](https://hackmd.io/_uploads/S1RRbjQfp.png) (1) 由使用者輸入商品訂價、是否為會員(輸入1為會員、2為非會員) (2) 再判斷會員等級(輸入1為鑽石會員、2為白金會員、3為普通會員),鑽石會員可打7折、白金會員可打8折、普通會員可打9折 (3) 非會員可選擇是否加入會員(輸入1為加入會員、2為不加入會員),加入會員後即為普通會員,可打9折,若不加入會員則以原價購買商品。 ``` #include<iostream> using namespace std; int main(){ float price; //價格 int level; //會員等級 int member; //判別是否為會員 int join; //判別是否要加入會員 cout << "請輸入商品訂價:"; cin >> price; cout << "請問是否為會員(是=1,否=2):"; cin >> member; if (member == 1){ cout << "請輸入會員等級(鑽石為1、白金為2、普通為3):"; cin >> level;; if ( level == 1){ cout << "您為鑽石會員,您的商品價格為" << price * 0.7; }else if ( level == 2 ) { cout << "您為白金會員,您的商品價格為" << price * 0.8; }else { cout << "您為普通會員,您的商品價格為" << price * 0.9; } } else { cout << "請問是否要加入會員(是=1,否=2):"; cin >> join; if (join == 1) cout << "您現在為普通會員,您的商品價格為" << price * 0.9; else cout << "您不是本購物網會員,您的商品價格為:" << price; } } ``` ### 6.計算全班平均 請設計可輸入全班成績(人數不固定)的程式,此程式可完成以下工作: (1) 請同學輸入全班人數 (2) 再輸入每位同學的成績 (3) 計算全班平均成績、及格人數、與不及格人數(60分為及格),並將其輸出 ``` #include<iostream> using namespace std; int main(){ int number; //全班人數 float total; //計算平均的累加成績 int a; //成績 int b = 0; //及格人數 cin >> number; for(int i = 1; i <= number; i++){ cin >> a; total = total + a; if ( a >= 60 ) b++; } cout << "全班平均:"<< total/number << endl; cout << "及格人數:"<< b << endl; cout << "不及格人數:"<< number-b; } ``` ### 7.取絕對值 ``` 請輸入一整數:-10 -10 的絕對值是 10 ``` ``` #include <iostream> using namespace std; int main() { int x, y; cout << "請輸入一整數:"; cin >> x; y = (x < 0) ? (-x) : (x); cout << x << " 的絕對值是 " << y << endl; } ``` ### 8.雞兔同籠 學生家佑最近在數學課學到了雞兔同籠問題,請建立C程式使用while迴圈幫家佑解雞兔同籠問題,目前只知道在籠子中共有40隻雞或兔,總共有100隻腳,請問雞兔各有多少隻? ``` 雞:30兔:10 ``` ``` #include<iostream> using namespace std; int main() { int rab, cock, feet; cock = 1; while (cock <= 40) { rab = 40 - cock; feet = cock * 2 + rab * 4; if (feet == 100) { cout<<"雞:"<<cock<<"\t兔:"<<rab; } cock = cock + 1; } } ``` ## 本章應用實例 (HW)(20231023) ### 05-24電影分級制度 ``` //訊一乙 36號 潘誠 #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; case 121 ... 999: cout << "請輸入合理年齡"; break; } return 0; } ``` ![](https://hackmd.io/_uploads/B1wB85mGp.png) 輸出結果: ![](https://hackmd.io/_uploads/HkydI5mfp.png) ![](https://hackmd.io/_uploads/ryrd8cXMa.png) ### 05-27 猜數字遊戲(自行設計) ``` //訊一乙 36號 潘誠 #include<iostream> #include <stdio.h> #include <stdlib.h> #include <time.h> using namespace std; int main() { srand( time(NULL) ); int min = 1; int max = 10; int x = rand() % (max - min + 1) + min; int n, ans = x; while (n != ans){ cout << "猜猜數字是多少?(1~10):"; cin >> n; if(n < x) cout << "猜大一點\n"; if(n > x) cout << "猜小一點\n"; } cout << "猜對了!YA!"; } ``` ![](https://hackmd.io/_uploads/BkjXY9XMT.png) 輸出結果: ![](https://hackmd.io/_uploads/SJuLtcXG6.png) ![](https://hackmd.io/_uploads/Sy9wt9Xz6.png) ### 05-28 2的n次方 ``` //訊一乙 36號 潘誠 #include <iostream> #include <cmath> using namespace std; int main() { int n; cout << "請輸入n值以求得2的n次方值:"; cin >> n; int result = pow(2, n); cout << "2的" << n << "次方是:" << result << endl; return 0; } ``` ![](https://hackmd.io/_uploads/HyG2c5Xza.png) 輸出結果: ![](https://hackmd.io/_uploads/rJ6aq5XzT.png) ### 05-29 成績加總 ``` //訊一乙 36號 潘誠 #include <iostream> using namespace std; int main() { int sum = 0, n = 0; do{ sum = sum + n; cout << "請輸入成績:"; cin >> n; } while(n != -1); cout << "總分為:" << sum; } ``` ![](https://hackmd.io/_uploads/Syi2aqmf6.png) 輸出結果: ![](https://hackmd.io/_uploads/Syeo697za.png) ### 05-31 顯示1~50的質數 ``` //訊一乙 36號 潘誠 #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; } } ``` ![](https://hackmd.io/_uploads/r1DFA9XGa.png) 輸出結果: ![](https://hackmd.io/_uploads/B1ncA5QM6.png) ### 05-32 階層計算機 ``` //訊一乙 36號 潘誠 #include <iostream> using namespace std; int main() { while(true){ cout << "請輸入 1~170 間的整數(輸入 0 即結束程式):"; int num = 0; cin >> num; if (num == 0) break; // 若使用者輸入 0, 就會跳出迴圈 cout << num << "! 等於 "; double fact; // 用來儲存、計算階乘值的變數 for(fact = 1; num > 0;num--) // 計算階乘的迴圈 fact = fact * num; // 每輪皆將 fact 乘上目前的 num cout << fact << "\n\n"; // 輸出計算所得的階乘值 } cout << "謝謝您使用階乘計算程式。"; } ``` ![](https://hackmd.io/_uploads/HkYKgjQMp.png) 輸出結果: ![](https://hackmd.io/_uploads/Byi5eoXzT.png) ## 課後練習 (10/16~23完成) ## 1.心理測驗 (if…else if 應用) ``` //訊一乙 36號 潘誠 #include<iostream> using namespace std; int main() { int chose; cout << "發生船難,你選擇留下什麼?"; cout << "\n選擇(1)留下老人和孕婦\n選擇(2)留下老人和嬰\n選擇(3)留下老人和金銀珠寶\n選擇(4)留下孕婦和嬰兒\n選擇(5)留下孕婦和金銀珠寶\n選擇(6)留下嬰兒和金銀珠寶"; cin >> chose; if (chose=1) cout << "你的情感很細膩,善於觀察細節。在感情中,相比於戀人的甜言蜜語,你更在乎對方實際的行動"; else if (chose = 2) cout << "在感情中,你不僅洞察力超好,第六感也很強,越是親近的人這種直覺越敏銳,所以另一半對你很難有所隱藏,因為你可以憑借著蛛絲馬跡得到你想要知道的消息。"; else if (chose = 3) cout << "你是個典型的顏控,在擇偶時很注重另一半的外表和身高。"; else if (chose = 4) cout << "面對感情你很承盾,一方面你很感性,渴望浪漫熱烈的愛情;另一方面你又很理性,明白現實的殘酷和金錢的重要性"; else if (chose = 5) cout << "在感情方面你很挑剔,所以很難遇到心動的對象。不過在戀愛時你卻很專一,一旦喜歡上某個人,你就會全心全意的對他好,所以和你談戀愛是一件很幸福的事。"; else if (chose = 6) cout << "在感情中你很缺乏安全感,有時候會忍不住通過試探或考驗的方式去確認對方是否愛你"; else cout << "亂輸入,活該一輩子舔狗命!!!"; } ``` ![](https://hackmd.io/_uploads/BkmBgL5Wp.png) 輸出結果: ![](https://hackmd.io/_uploads/Hy4oeI5WT.png) ![](https://hackmd.io/_uploads/r1V2eL5b6.png) ## 2.判斷座標(x,y)在第幾象限。 (巢狀if…else 應用) ``` //訊一乙 36號 潘誠 #include<iostream> using namespace std; int main() { int x,y; cout << "請輸入x的值:"; cout << "請輸入y的值:"; cin >> x; 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 << "在第三象限"; } ``` ![](https://hackmd.io/_uploads/Bk-lSL9ba.png) 輸出結果: ![](https://hackmd.io/_uploads/S19a4U9-p.png) ![](https://hackmd.io/_uploads/BytRVU5ZT.png) ## 3.用*組成三角形(for迴圈的應用) ``` //訊一乙 36號 潘誠 #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; 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; } } ``` ![](https://hackmd.io/_uploads/r1u0nIqba.png) 輸出結果: ![](https://hackmd.io/_uploads/SJxl689Zp.png) ![](https://hackmd.io/_uploads/H1IfpU5-6.png) ## 4.十二生肖查詢(switch…case的應用) ``` //訊一乙 36號 潘誠 #include<iostream> using namespace std; int main() { int yy; cout << "請輸入民國(年):"; cin >> yy; switch (yy%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 0: cout << "豬\n"; break; } } ``` ![](https://hackmd.io/_uploads/rkeAAL5ba.png) 輸出結果: ![](https://hackmd.io/_uploads/SJaekDc-6.png) ![](https://hackmd.io/_uploads/SJuZJv5ba.png) ## 5.閏年 ``` //訊一乙 36號 潘誠 #include<iostream> using namespace std; int main() { int BRUH; cout << "請輸入一個西元年:"; cin >> BRUH; if((BRUH % 4) == 0){ if((BRUH % 400) == 0) cout << BRUH << "年是閏年" << endl; else if((BRUH % 100) == 0) cout << BRUH << "年非閏年" << endl; else cout << BRUH << "年是閏年" << endl; } else{cout << BRUH << "年非閏年" << endl; } } ``` ![](https://hackmd.io/_uploads/ByiYzYmMa.png)- 輸出結果: ![](https://hackmd.io/_uploads/BySnVPcZT.png) ![](https://hackmd.io/_uploads/Hyia4P5bT.png) ## 6.重複計算BMI(while 迴圈)(P.129)(必考) ``` //訊一乙 36號 潘誠 #include<iostream> using namespace std; int main() { char go_again = 'Y'; float height, weight; while (go_again == 'Y' || go_again == 'y') { // 大小寫 Y 都會 cout << "請輸入身高 (公分):"; // 執行迴圈 cin >> height; cout << "請輸入體重 (公斤):"; cin >> weight; cout << "您的BMI為:" << weight / (height * height) * 10000 << endl; //BMI 為體重除以身高(公尺) 平方 cout << "要繼續計算另一位嗎?" << "(要請輸入 Y 或 y , 若不要請輸入非 Y 或 y 的字元):"; cin >> go_again; } } ``` ![](https://hackmd.io/_uploads/SyuSOw9Zp.png) 輸出結果: ![](https://hackmd.io/_uploads/rk71dv5W6.png) ## 7. 計算兩整數的最大公因數(while 迴圈)(P.131)(2023/10/23) ``` //訊一乙 36號 潘誠 #include<iostream> using namespace std; int main() { int BRUH1,BRUH2,temp; //temp的作用為暫存餘數 cout << "計算兩整數的最大公因數\n"; cout << "計算輸入第1個數字:"; cin >> BRUH1; cout << "計算輸入第2個數字:"; cin >> BRUH2; while (BRUH2!=0) { // 當餘數為0時, temp = BRUH1%BRUH2; // 以這一輪迴的除數作為下一輪迴的被除數 BRUH1 = BRUH2; // 餘數作為除數 BRUH2 = temp; } cout << "最大公因數是:" << BRUH1; } ``` ![](https://hackmd.io/_uploads/rJmUctXG6.png) 輸出結果: ![](https://hackmd.io/_uploads/BkMwcF7fT.png) ## 8. 密碼檢驗程式(P.133)(自行設計)(2023/10/23) ``` //訊一乙 36號 潘誠 #include<iostream> using namespace std; int main() { int pwd; int i=1; do{ cout << "請輸入第"<< i <<"次密碼"; cin >> pwd; if (i==3 and pwd!=123) { cout << "密碼連續輸入3次錯誤密碼,無法登入"; return 0; } else { if (pwd!=123) cout << "密碼錯誤,請重新輸入\n"; } i++; }while(pwd!=123); cout << "歡迎使用ATM"; } ``` ![](https://hackmd.io/_uploads/SJXazcmGp.png) 輸出結果: ![](https://hackmd.io/_uploads/S1pQ75Qf6.png) ![](https://hackmd.io/_uploads/BkUHQcmGp.png) # 心得感想 學習 C++ 的if-else、for迴圈和while迴圈讓我更了解程式控制結構的威力。if-else讓我能根據不同情況採取不同操作,增加程式的選擇性。for迴圈使我能輕鬆處理需要固定次數迭代的工作,而while迴圈則應對不確定次數的情況。這些工具的熟練運用提高了我的程式能力,讓我能更有效率地處理各種挑戰。我期待在 C++ 的基礎上建立更多功能,進一步提升我的開發技能。 # 2023/11/06 課堂作業(CH6函式) ## 隨堂練習 ### Ch06-21 以時間亂數種子產生5個200~600的亂數 ``` //訊一乙 36號 潘誠 #include<iostream> #include <stdio.h> #include <stdlib.h> #include <time.h> using namespace std; int main() { srand( time(NULL) ); int min = 200; int max = 600; for (int i = 0; i < 5; i++) cout << (rand() % (max - min + 1) + min) << endl; } ``` ![image.png](https://hackmd.io/_uploads/BkMpsbUXa.png) 輸出結果: ![image.png](https://hackmd.io/_uploads/r1J1nWLQa.png) ## 應用實例練習 ### Ch06-25 猜數字遊戲 ``` //訊一乙 36號 潘誠 #include<iostream> #include <stdio.h> #include <stdlib.h> #include <time.h> using namespace std; int main() { srand( time(NULL) ); int min = 1; int max = 10; int x = rand() % (max - min + 1) + min; int n, ans = x; while (n != ans){ cout << "猜猜數字是多少?(1~10):"; cin >> n; if(n < x) cout << "猜大一點\n"; if(n > x) cout << "猜小一點\n"; } cout << "猜對了!YA!"; } ``` ![image.png](https://hackmd.io/_uploads/BJEUh-UQp.png) 輸出結果: ![image.png](https://hackmd.io/_uploads/ByawhbLQT.png) ## Ch06-26 猜拳遊戲 ``` //訊一乙 36號 潘誠 #include<iostream> #include <stdio.h> #include <stdlib.h> #include <time.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/B1bSR-L76.png) 輸出結果: ![image.png](https://hackmd.io/_uploads/B1wU0bI7p.png) ![image.png](https://hackmd.io/_uploads/Byx_R-8Xp.png) ![image.png](https://hackmd.io/_uploads/Hk-t0ZL7p.png) ## Ch06-28 猜點數遊戲 ``` //訊一乙 36號 潘誠 #include <time.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/Bkuj7G8X6.png) 輸出結果: ![image.png](https://hackmd.io/_uploads/BJHFXfIXT.png) # 心得感想(100字): 這個亂數函式真的是太有趣啦,我use了這個<stdlib.h>就可以將亂數函式넣다我的程式碼,然後就可以讓number在輸出結果裡更有appear randomly,讓我的программа更加リッチ,也讓這個โปรแกรม變得更有persuasiveness,讓我научился很多新지식,今天真的學到了rất nhiều ## Ch06-24 查詢語音費率函式 ``` //訊一乙 36號 潘誠 #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/rklhG2__4T.png) 輸出結果: ![image](https://hackmd.io/_uploads/SkrU3dO4p.png) ![image](https://hackmd.io/_uploads/rkWDhO_N6.png) ![image](https://hackmd.io/_uploads/ry7_nOOVa.png) ## Ch06-27 階乘函式 ``` //訊一乙 36號 潘誠 #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/rknBadOE6.png) 輸出結果: ![image](https://hackmd.io/_uploads/SkhLT_u4p.png) # 課後練習 ## 1.組成三角形 ``` //訊一乙 36號 潘誠 #include <iostream> using namespace std; bool T(int a, int b, int c) { return (a + b > c) && (a + c > b) && (b + c > a); } int main() { int side1, side2, side3; cout << "請輸入三個邊長: " << endl; cin >> side1 >> side2 >> side3; if (T(side1, side2, side3)) { cout << "可以構成三角形。" << endl; } else { cout << "無法構成三角形。" << endl; } return 0; } ``` ![image](https://hackmd.io/_uploads/HJXdoOuET.png) 輸出結果: ![image](https://hackmd.io/_uploads/BypYjud4p.png) ## 2.遞迴 ``` #include<iostream> using namespace std; int main(){ void func(); func(); } void func(){ cout << " \n"; cout << " \n"; cout << " \n"; cout << " .:~!?JY5PPPPPPPPY?7~^. \n"; cout << " .~JPB#&&&############&&&##BPJ!: \n"; cout << " .!5#&&##BBGGGBBGGBBBBBBBBBBBB##&&#G?: \n"; cout << " ^Y#&&#BGGGGBBBBBBGGGGBB###&&&#BBBBB##&#5^ \n"; cout << " ~P&@&####B#&BPBBGGGP5Y7777??JYPB#&BGGGGB#&&5^ \n"; cout << " :G@@#GPPP5JJYJ77YYYYYYPP?777777777?YG##BGPB##@&Y: \n"; cout << " ~B&PY5GBBG5777777777777777777777?P5777?P&@##@@@BB&? \n"; cout << " .5&P?5#B5J?77777777777?Y?7777777777P@P7777?G&G5##YG@@5. \n"; cout << " ~##J775Y777777777777777?&#?7777??7777Y&#J77775@BJJYGG&@P. \n"; cout << " ^##?77777?YJ7?J777777JGJ7Y&#Y?77P#57777?G&G?777Y&#Y#&G&&@5 \n"; cout << " :##?7PB?77Y@5?&B7777775@G77Y&@#G5J5#BJ7777JB&P?77?G&@@@&B&&: \n"; cout << " Y@J7J@G7?5#@JY@5777777?#@BJ7J##YYGBB&&B5J777JG#GY77JG&##&&@! \n"; cout << " :&B77P@55#BB&?G@#PGJ7777Y&@&GJY#&5P#B#@@&#BG5YJYPBBG5YP#@&#B#P?^ \n"; cout << " 7@Y77G@&@5.5@J##B@@&PJY55#@&@&##BB&&~?#G&5JYPGBB###&@@&BPPY7?YGBGY!. \n"; cout << " 5@J77G@@&55#@#&#B#&&GP5Y?!~!&#JJYYY&G.:.?#B?777?5&#Y????7777777?J5BB5!. \n"; cout << " J@J775@&?~^P@GPPGPB@7 .. ?@Y^~~~#B... ^P&P?777?P#B5?777777777777?5B#5: \n"; cout << " ^&G77?#@G. ~@B^!7!?@Y .......!GBPPBB~.:^:^.~G#P?777?5G#BPYJ?7777777?77J##^ \n"; cout << " J@Y77J#&~ ?&5!~!P@!...:^~:...^!!^...:..... !&&B5?YBY?J5GBBBGBBB##BJ77?##. \n"; cout << " .G@577?G&J^.~PGGG5~....?P5~............... ~#BYYGBB&@#BGGGB##&&BPY?7777Y@J \n"; cout << " P@&#PYJ5#&5!..:................... ..:~Y#BJJJY5G#&@@&&#&@#GGGP?777777#B. \n"; cout << " .#@#@BY55PGG5^.......... ...:^~!7J5GB#@@&#GPG&@&&&@P^::#G:^7G@Y777777##. \n"; cout << " !!:?#Y~:....::::::^^^~!7?JY55G#&&##@@&G&@@@&YG#&##B@B. .##PG#@J777777J@Y \n"; cout << " :!!^^~7?JG@&###GPB##&#B#&&@#B&#PJJ#&#####@&&&##@@@@#BBB#@? ...G&7^J@GBJ777J&G. \n"; cout << " :PBY#&&&&&&@&BB&@&G&@@B&#BGB&@5!!5@@&#GG@#BB5?P#B#@@@&#BB&&^ .. !@5^J@#Y?775&5. \n"; cout << " :JGG@J 7@#BBBBB####@@&@&&#&@#&BB@P:J&&#@&##B5?5PP7B#B#&@@&&&@@7^^::.J@YG&?77Y#B! \n"; cout << " Y@PJ@5 5@#B#####B&@##@&BB#@&@@&&@&@@BB&#&&J5GBGYY###BB#&&B55G#&&&&#G##BY?5B#J. \n"; cout << " .~77P@^ .P@#BB##BB&@&&@##B#&@#G#@@@&@#?~~!&&GPPPG######BB#&&##GG&#GY?7J5GBP!. \n"; cout << " .B#^ ?&@####&&#B@&B##B#@B!~!J5PJ^^!YG&@&#######&&&&&&&&@&&@&5?JPBB57: \n"; cout << " .P&7. .J&@@@@#G#@#BB##&#&@#PYJ7?P#@@@B&@&&##BGGPPPPPPPPPPPPB@BPJ~. \n"; cout << " !GGYYPPJ~^~75&&##&@&GG@&#&@&&&&&#BB#&#BB@@###&&&&#######&&@BJ~: \n"; cout << " .!?7!!7J5GGB@##@&P5#@@?.~?&@##&@@&&##G#&@&BBBBBB######BBB##&&#B5?~: \n"; cout << " 7##&&#G5YJ7P@&&G5B&&@B. !&&P5YG&&&B5YB&@&##B#############BB##&&&#GJ~. \n"; cout << " .^7JY5PGGGB&&GPB&&##@J ^B&55G@@BBB#@P?P#&&##BB#############BBB##&&#57: \n"; cout << " ...5@B#&&#BB&@~ ... .7YJ!?G&BJY@B: :!YB#&###BBBB############B##&&#P7. \n"; cout << " !@&##B###&#. .... .~YGBY... ^J&@@&&#####B###########BB#&@#Y: \n"; cout << " ?@#B#####@G ......... .. . .5#Y!Y&@&&&&&###############B##@#~ \n"; cout << " \n"; cout << " \n"; cout << " \n"; cout << " .:~!7JY5PPPPPP5YJ?!:. \n"; cout << " .^7JPB#&&&&##########&&&#BP?^ \n"; cout << " .!YB&&&##BBBBBBBBBBBBBBBBBBB##&&G?. \n"; cout << " ^Y#&##BBGGG####BBB####BBBBBBBBBBBB#@#?. \n"; cout << " .5&&#####BBBBPP5Y555PGB#&&&##BBBBBBBBB#@#7. \n"; cout << " :B@@&#BBBBB5?75GGG5?7777??J5G#&&BGGBBBB##&@B~ \n"; cout << " ^B@B5?JBG5YYJ77?JJYG&P77777777?JP#####&@@G#@@&? \n"; cout << " .Y&GJ777??77777777777YPGJ?777777777?5&&5YB#YB@@@@J \n"; cout << " ~##J7777777777777J5?7J@P7&B77777777777?P&BPJY#B#@&@? \n"; cout << " ~&B???5J7777777777#&?7?##?J&BJ7777?777777Y&&YB&##@#&@! \n"; cout << " :##?G#P@5777777??77G@577Y@&PYG&P?777??77777?G&&@#&#BB@B. \n"; cout << " Y@JY@#&&?Y5???7??77Y@@P?7Y&@#GG#&GY?777777777JG#B#BBB#@7 \n"; cout << " .#B7B@&&#?B@##&Y77777G@&#5?5&#5#@@@@@#PPG55YYYY5P&@@#B#@Y \n"; cout << " ^@P?&@?P@JP@B&@&GGGGGB&#&&###&@Y7##7JG@&#&&#B@#P5YYG#&&@P^. \n"; cout << " :&GJ@@GPB@####B@&7!!~^:J@G5PGG@J.J! :B&J7??75#BY?77?JY5B#BP5?~. \n"; cout << " B&J&@G::&#YJ55B@^ .....B@7~7Y@? ....:P&57777?5B#G5J?7777?JYPBB57. \n"; cout << " !@PB&Y^ ?@J:~~G&^......^B#J?#B:..^::...7BBY7?5?7?YGBBBGP5YJ???J5B#Y: \n"; cout << " Y@G&Y...?&GJP&?..~!7:...755?:..^:^.... J@#BG&&5YJ???JYPG#&@&#BJ7JG#Y: \n"; cout << " J&#@B?. ^?Y?^..:Y5J^... . ........ .Y@PYG&#G5G@#BGGGBBGGP5J???77JB#7 \n"; cout << " 7@@&BP:.. ...... .......... ...^?B@&#G#&~^?B@G?JJ???777777???777P@J \n"; cout << " ^JG&?:.. .....::^!7?Y5GB&@@@@7 5@B#PJG&?77777?????????77775@J \n"; cout << " !5PP55YJJJJJJJYY5555B#&##&@@@@&#&#@G !@P~^^7@P7????????????777777G@^ \n"; cout << " :^~^!YG&@@@@@@#B@@5B#5J&@BG#B&@&#&@&#@Y . P&!^^~B#?7??????????????777J@J \n"; cout << " .PB#@&&&&##&@@#P&BG&@J^J@&B#@@#&#B#&@@&@J :B#!^^G&?7?77?????????77777?@P \n"; cout << " .P@BJ@#BBB#B#@&&&@B##@G~G@&@@@#G57P###&&@#~.. :P#Y?&B777??????????7777?7J@Y \n"; cout << " JB@YJ@&BBBB&@#B#@BB@&@@@&5&@&#YGP7B#B###&@#G55P##BP?7JP&G7?????777???77B&^ \n"; cout << " ?@J?&@#B#@&B#B&@#B&@@@@#P?#&Y5Y5#####BB#@@B5Y???YP#@@BJ7?7777?7?777?B&~ \n"; cout << " 7&5~Y&&&@####B&&?7YGBP!^7#@#BB#BB#BB##B##&&####B&&GY77?77777777?YG#P^ \n"; cout << " ^GBYG#@&BB###&@@B5?77Y#@@&&#&&&&&&&########&&@@@BY?7777777?YPBBP?^ \n"; cout << " ~7^?@##&&&#&@@&&&&@@@@&&##&&&&&&@@@&&&&@&&@&###&&BPYY5GBBPJ!. \n"; cout << " 5@#&&BGB&@&##&&&&&&BG#&##BBBBGGGGGG#@&&&#BBB###&&@@G7. \n"; cout << " .#@&BB#&#&&~5@#&@&&&G5#@@@@&&##&&&&####BB###########&#P7. \n"; cout << " .J@&#&&#B#@G ~@BJ5&&#BG#@P^?P#&&#BBBB##################&&#Y^ \n"; cout << " ?@@@&#B#B#@Y J&GB@&#BB&&7 :75#&&###B###################&&P~ \n"; cout << " .P@#####B#@7 ~7^?B#5Y##! .~JPB&&&##################B#&@P^ \n"; cout << " G@#######@! .!5BP: .... .J@GP#&&#################B#@&~ \n"; cout << " \n"; cout << " \n"; cout << " \n"; cout << " .~7?YY5555YYJ?7!~^:. \n"; cout << " .~JG#&&&&#######&&&&&##BGY?!: \n"; cout << " J@@&#BBBBBBBBBBBBBBBBBB###&&#BY!. \n"; cout << " J&&BBBBBBBBBBBBBBBBBBBBBBBBBBB#&&#Y^ \n"; cout << " Y@BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB#&&5: \n"; cout << " :JB@GGBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB#&&? \n"; cout << " .?#B&&5BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB#@G: \n"; cout << " ~B#Y?&&5GBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB@#~ \n"; cout << " ?&#Y?J&&G5BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB&&^ \n"; cout << " ?@G&&B&@@&GPBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB@G \n"; cout << " ^&BB@#5GB5P@#BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB&#. \n"; cout << " 5@P@##5PPB#@BPGBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB#@5 \n"; cout << " B#G@@#B@#B&&&G5GBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB#@@~ \n"; cout << " 5@@B&&PYJP5JB@#PPGBBBBBBBBBBBBBBBBBBBBBBBBBBBBB#&&#@^ \n"; cout << " .P@75@?77B@P7JG&#GPGBBBBBBBBBBBBBBBBBBBBBBBB#&&@@GY@J \n"; cout << " ?@~:##?7J#@Y77?5B##BBBBBBBBBBBBBBBBBBB##&&#BPB&P?7G@! \n"; cout << " !@J ^B#J7?G@57777?Y5GB##&&&###########BG5JJP#BJ7777G&7 \n"; cout << " :5@5~^P&5775&BJ7777777??J5#&@&##BBGGP5YYGB#@&GGP?7775&BPY!^. \n"; cout << " :&GP&@&B&@#Y?JG#GJ7777777777?JY555PPPPG@@@@&####@B7777JB@&BBGP?^ \n"; cout << " P#7^!Y#@PP#&&BG#&BY?77777777777777777B@#&@@@&#&@5?77777JG#GYYG#GJ: \n"; cout << " J@BG5J5@5 ^&@BGGB&&#BY77777777777777Y@&#&@@@@@&&@B?777777JPJ77?YB#? \n"; cout << " 5&!~7JJ##. J@Y77???JJ?77777777777777?5PP5B@#&@&#@G?777777777777775@5 \n"; cout << " J@7^^^^G&: ^&&J7?777777777777777777777777?G#BB&G5B#G5J?77777777777Y@J \n"; cout << " ^@5^~^~&G .B@&Y777777777777777777777777777777JGBB@@@&#BJ7777777777B&: \n"; cout << " G&~^^P@~ G@&@G?777777777777777777777777777777?J5PG&&BY?777777777P@^ \n"; cout << " ^&BJB&! ..:B@&&&&P?777777777777777777777777777777777?JPB#BP5YJJJJY&@7 \n"; cout << " :YB&G5PPG&&##BB#&&GY?7777777777777777777777777777777777?Y5PGGBBBBB&#. \n"; cout << " ..::.!@&BB##BB#&&#PY?777777777777777777777777777777777777777777#B. \n"; cout << " ~@@&#BBBBBB##@@#BPJ?777777777777777777777777777777777777J##~ \n"; cout << " ^@@&&&&######&@BGG##BG5YJ?77777777777777777777777777?YPB#Y: \n"; cout << " .#@B###&&&&&&&@&PYG5GBG#&&BGP5YYJJ??????777???JJY5PGBB57. \n"; cout << " 5@#BBBB######&@B55PYGYP#@@@#BB###B#BBB#######&&&@#J^. \n"; cout << " ^@&###########&&B5P5PPYG&&P&#PY5GP5GG#@@&GB###BB##&&GY~. \n"; cout << " 5@#B##########&&&#BGB5PB@P7G&BGB##BP?!5&#GPGB###BB##&&#5~ \n"; cout << " :#@###########B###BB@&GGB&7.#@@G7^. ~&@&BPPB#####B##&&G7. \n"; cout << " ~&&BB###########B5B@&G?:.. G#?PB5!. 7@?7G&#GPGB#####B#&@P: \n"; cout << " \n"; cout << " \n"; cout << " \n"; cout << " .^~7?JY5PPPP55YJ7!^. \n"; cout << " ^YG#&&&&&#########&&&&#GY!: \n"; cout << " ~P&&#BBBBBBBBBBBBBBBBBBBB#&&&G?:. \n"; cout << " :P@&BBBBBBBBBBBBBBBBBBBBBBBBBBB#&&BY!. \n"; cout << " .!#@BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB5B@@##?. \n"; cout << " ~G&@&BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB5G#@5Y&B^ \n"; cout << " ~@@@#BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBPPB#@57?B&! \n"; cout << " P@@&BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBGGBB&&JGP?B&^ \n"; cout << " !B&&@BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBP5B#&#J7#@G?&P \n"; cout << " .Y@BJ#@BBBBBBBBBBBBBBBBBBBBBBBBBBBBBGPPB&&G?77J#@JG@^ \n"; cout << " :G@P77#@BBBBBBBBBBBBBBBBBBBBBBBBBBBGPP#&&GJ77777J@G5@~ \n"; cout << " .B@5777G@#BBBBBBBBBBBBBBBBBBBBBBBGPPG#&#P?7777777?#&G@~ \n"; cout << " 5@Y77775@@#BBBBBBBBBBBBBBBBBGGPPGB#&#PJ7777777777?#@@&: \n"; cout << " 7@57777?&#G@#BBBBBBBBBBBBPPBBBB#&#B5J77777777777777B@G! \n"; cout << " :&B777777G@J5&&#BBBBBBGGPGB#&&#G5J?7777777777777777J@5.^!!::. \n"; cout << " Y@J777777?#&??5B#&&##BB###BPYJ77777?Y5GBJ7777777777G@#B5Y#&GBP. \n"; cout << " .##77777777J##G#Y?JJYG&G??777??Y5PGBBGP5J?7777777775@&@@~ :G##B. \n"; cout << " ^@G777777777?P&@@5?G&&BP5PGGBBBGP5YJ?7777777777777Y@&B#@G. .B&^ \n"; cout << " :&G77777777777JP&@&@@@&BP5YJJ?7777777777777777777?&@&BB&@~ ~@Y \n"; cout << " :##7777777777777#@&@&&###BGJ777777777777777777777?JG@#B#@7~5G&&###GP5Y?!^. \n"; cout << " Y@@Y77777777777?&&B&@@&#&@&J77777777777777777777777?B@##@7^7Y&@5YPB##BGGBBP?: \n"; cout << " .~P@Y7777777777?#@#&@@@@&&&#BJ7777777777777777777777?5&@@??GGJ^ :!P@G7?YB&7 \n"; cout << " Y&P?777777777JPP&@##@&&&@BJ777777777777777777777777?5G#&&GYYY5PPPPBGJ7777#&. \n"; cout << " Y@&GY?777777777JB&##@&Y5GBBP5J?7777777777777777777777???JYYYYYJYJ?7??JJP@@P~ \n"; cout << " .G&5##Y7777777777?J?JG#GYJ5&&@&#P777?77777777777777777777YBBBGGGGBBBBBGPYJ#&: \n"; cout << " .5BPB@BY7777777777777J5BB#&@&GGY777P#PJ??77?55JJ??????77????JJJJJ?????JYB@J \n"; cout << " ^?Y??&#PJ7777777777777???J?777777?YPBBBBGG&@@@&&&###BBBBBGGGGGB##BGGGPY7 \n"; cout << " Y@@&&#PJ?777777777777PBPJ?7777777??JJYYYYYYYYJJJJJ????JJ5P#&5^.. \n"; cout << " .G@&&&&@&B#BP5J??777777?YPBBBGP5YJJ??7777777777777??JY5PBBGY7: \n"; cout << " :B@#B##&@BYPPG#@&#BGP5YYJ???J5PB#&&&&&##BBBBBBBBB##&&@#57~. \n"; cout << " :B@#B##B#@BYPYYP#@&BPG##B###BBBBBGBB#&@@@@@@@@@@@@@@&&&##BPJ!^. \n"; cout << " .G@#B#####@BYPY55B@&&B55PP5PGPPPB&@@&################BBBB###&&&BY!: \n"; cout << " ?@#B######@&55Y5YG&#?#&B55PP5PPGB&&BBB##########################&&#P!. \n"; cout << " ^&&B######B#@##BBGG#@?.J#&BP5PG#B5B&#GPPGB##########################&@#Y^ \n"; cout << " Y@##############&BB@@@Y..7P#&&#7. .~YB&#BGPGBB#######################B#&&5^ \n"; cout << " .B@#############B#G5#@@@G7. :?&@GJ^ .B&G&&BGPPGB######################B#@&7 \n"; cout << " \n"; cout << " \n"; cout << " \n"; cout << " :^!?JY5PPGGPPP5YJ?!~:. \n"; cout << " :?5B#&&&&############&&&##G57. \n"; cout << " ^5&&##BBBBBBBBBBBBBBBBBBBBBB####57~: \n"; cout << " .J&&#BBBBBBBBBBBBBBBBBBBBBBBBBB5P#@@BBG5! \n"; cout << " ^B@#BBBBBBBBBBBBBBBBBBBBBBBBBBG5PBB@&?7JG&P^ \n"; cout << " !&&BBBBBBBBBBBBBBBBBBBBBBBBBBBBGGB#@&Y7777?G&Y. \n"; cout << " !@&BBBBBBBBBBBBBBBBBBBBBBBBBBB5GBB#@G?7777777J&B: \n"; cout << " !&&BBBBBBBBBBBBBBBBBBBBBBBBBBP5GB#&#Y77777777?YJ##^ \n"; cout << " :P@@BBBBBBBBBBBBBBBBBBBBBBBBGPPGB&&BY?777777777J@P?&B. \n"; cout << " !##&&BBBBBBBBBBBBBBBBBBBBBGGPPG#&&BY777777777777J@B75@! \n"; cout << " ?@PJ&&BBBBBBBBBBBBBBBBBBGPPPGB&&#PJ777777777777775@@BY@? \n"; cout << " ?@PP5&&BBBBBBBBBBBBBBBGPGBBB&&&GJ?777777777777777?#@@@&&^ \n"; cout << " ~@P?@BP@#BBBBBBBBBBGGPPPB#&&#GY?7777777777777777775@@5?@Y \n"; cout << " .B&?7##?P&#GGBBGGPPPGG#&&&BPJ?77777777777777777777J&#7 :&5 \n"; cout << " !@5775@P7JG##&&###&##BG5J?7777777?5BP?77777777777J&G^~JP@#P55?. \n"; cout << " Y@J777P@G?77JYY55YJ??777777?JY5GBBG5?777777777775&&PGPB@?^^!B@~ \n"; cout << " 5@?777P@@&B5YJ????JJJY5PPGBBBGP5J?777777777777Y#@&@5: G&~?PB&#. \n"; cout << " Y@J777B@@&#&@@@&#BGGGGP55YJ?7777777777777777JB@&&@P .B@G5?~7@Y \n"; cout << " !@P777G@#@&BB#&@B?7777777777777777777777777J&@#B&@~ ..B#~^^^^B#. \n"; cout << " ~@&J775@##@@&&@&PY?77777777777777777777777?#@#BB&#. . J@7^~~^Y@^ \n"; cout << " 5@B77?PB&@&@#B#@&P77777777777777Y5J?77777Y@&BB#@P :##~^^^5@^ \n"; cout << " .G&J7777Y&@@@&&@G?77777777777777YGBBBGPYJP@&###@P :GB?!J&P:^~7?JYY. \n"; cout << " .5@57777?5YB@5YGBB5J?7777777777777?J5PGGB####&&&P5PP5Y5#@&&&B#&@@#GJ^ \n"; cout << " 7#BJ77777?5#B5P&@@&BJ7777777777777777777??????JJYY555PGBB#&@@#G5555P? \n"; cout << " :Y&G?77777?5G#&B5BP?7777777777777?PGPP555555PPGBBGBGGPP555PGB#@#57^. \n"; cout << " ^5&GJ7777777?77P#GY?777777777777JJY55P5PP55YJJJJJJYY5PPGGPY?^. \n"; cout << " ^Y#GY?77777777JPBBGPPPP55YYYYYYYYYYYY55PPGBGPPPP55555PG7 \n"; cout << " !#@@BPJ?77777777?YPB#&&@@@@&&&@@@@@@@@@&@@GPGGB###BY!^. \n"; cout << " !&@@PG@&#G5J??77777777???JYY55PPPPPPPGGGBB#&&#G5?!: \n"; cout << " ~&@&#YB@B5GB#&#BGPPP555YYYYYYYY555PGBB###&##&&! \n"; cout << " :#@&@P5B@#YPY5G@@##&&&&&&&&&&&&&&&&#######B#&&@&J. \n"; cout << " P@#@&YP5&&5555YG@&#BBBBBBBBBBBBBBBBBBBBBBBB###&@@BY?7!~^::.. \n"; cout << " ?@##@#5PYB@GYGYPYG&&####BBBBBBBBBBBBBBBBBBBBBBBB###&&&&&&&##BGGGPP55YYYJJJJJ! \n"; cout << " :&&B#B#&&B#@&5PP5P5B&@#B##BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB##########&&&&&&&&&@P \n"; cout << " Y@##B5#@??@&@BYPP##G&@BPGBB##BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB#@J \n"; cout << " :&&B#G5&&^.#GG&B##Y^.~5&&BPPGB#BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB#@G. \n"; cout << " \n"; cout << " \n"; cout << " \n"; cout << " .^~7?YY55555YJ7!^: \n"; cout << " :!YG#&&&&########&&&&#BP?~. \n"; cout << " !P#&&#BBBBBBBBGGGBBGBBBB##&&#P?: \n"; cout << " ^JB&&BBBB###&&&#BB##&#BGPGGBBBB##&&P! \n"; cout << " ^5&@#BBB#&&#GP5YYJJJJYY5GB##B#######&@@B! \n"; cout << " .J&&#BBB&@#5?77777?YPPGGGGP??5PY????Y55PB&@P. \n"; cout << " ^B@#BB#&&#57777???7JGPYYJJJJ?7777777?PGBBGYP@B!. \n"; cout << " ~#@BB#&@BJ777???????777777777????????777?J5J?G@@BGP?. \n"; cout << " ^#&B#&@&Y77??????7777777777????????77777??77777Y#@#P@J \n"; cout << " .G@#&&@#?77?????77Y57?5Y77????????7777777Y&Y77777?#@#@G: \n"; cout << " 7@&@G#&?777????7?G&P5&@57Y#G77??77777?BY7?&#?77777J&@P&B. \n"; cout << " B@@55@Y77?????7?B@GB@@57Y@#JJ77777777J@B77Y@G777??7Y@&@Y \n"; cout << " :&@57##?7???????G@@&Y##JJ&&&&@#J7777777#@G?75@BY?77?7Y#@Y^. \n"; cout << " !@G7?&B7??????7J@@@5YG@##&#&@#B#GPPPPPP#@@&P5B@@#G5#B5Y5B#BP? \n"; cout << " P&?7?&B7??????7P@J?. 5@PPGG#@~.^~!!77!7#&BB#B&&PP@@B@&B&#GBBY. \n"; cout << " :&B7?7B&?7777777G@^ ..B#^~??#&:... :&#JYGP@P !&J~@@#&#:. \n"; cout << " :&G7?7J&B?777777P@7...5@7~~5@?..........B#^~7G@~.:^ ^@B5&@7 \n"; cout << " .B&?7?J#@&P?7777?#B~. :5BPGG!.......... 7&P?P&? ....^@G7J&B. \n"; cout << " ^##BBGBB#@&BP5YY5&#?^:.:^:.....:YP7.....~YYJ^.:^^^.~@P7Y&P \n"; cout << " ~#B#@! .:J@@@@@G5PGGGY..........:~:...... ..:.:. J@##@J. \n"; cout << " :&G^~P#Y^. J@P&@#?: .. ........................ .?&G5&#. \n"; cout << " 7@?^^^J@&GJJ@Y^#@#BPYJ?7!~^^::....... ........:^!JB@&YB#~ \n"; cout << " :!P@?^~^P@@@@@@P ^B#PB&@@@@&&###BBP5555P5555PGGB#&&&#&@&@~ \n"; cout << " .!5BBG@5^~^5@@&&@@5 !@@@&&@@@&&####@&Y?P#&&GYP@@@@@&BB#@@@G: \n"; cout << " 7B#P?77B&!^^!#@&&@&^ J@&@&##&&B&@@##&&@@&J^^P@#@&BB@&B#@#J!. \n"; cout << " ^G&5?7777?##?^^7#@@B^ ^Y&&B&@#B#####@BB&&@@@@#B@#P@BJJ@&#@G: \n"; cout << " ^&B?7777?5BBGGGYY#@BJYG&@&#&@&###BBBB##@#!J5G&@@&P?G&YJ@@&J. \n"; cout << " 5@J7?77J#B!:^Y&&GPBB#&@@&&&##########BB&@J::^!?J~::?@&##@G. \n"; cout << " P&?7?7J&P:?B&BY?77777?&&BBBBB####BBBB##&@&PJ!^::^75#&@@&&@&?::: \n"; cout << " ~&B?77J&B&@BJ7777??Y5G@&B##########&&@&#@B5B&#BPB@@@&@@BGG##BB#P. \n"; cout << " ^P#GJ?JPGBGGGG5P#&&PY@&#BBBBBB#&@&@&GP#@@#GBGGGGGPPG@@@&#####&@J. \n"; cout << " ^5@#GPPPPPPGB#&@@@&@@@&&&&&&@@@@BPB@@#@G!?5PGBBB#&&B&@#B#####&#5!. \n"; cout << " .P@G55555555YYYY555555P5555PPPG#@@&#B#@P. .:J#&G#@#&@@&#B##B##&&BY^ \n"; cout << " !5GGGP5YYJJJJJJJ???JJJY5PGB#&&##BB#B#@B^ .7JB@BB@@@&#B####B##&&P! \n"; cout << " .:~7???JY5PPPPG@&&&&&&&####BB#####B#@&7 .7PP!P@&@#B#####BB#&&G! \n"; cout << " ?@#B#BBBBB#############&@Y. .. ~&#B@&########B#&@Y. \n"; func(); }; ``` ![image](https://hackmd.io/_uploads/rk4ddtdVp.png) 輸出結果: ![image](https://hackmd.io/_uploads/rkLYOKdNa.png) ## 3.專案練習 ``` //訊一乙 36號 潘誠 #include <stdio.h> #include <iostream> #include <cstdlib> #include <ctime> using namespace std; void p01(); void p02(); void p03(); void p04(); void p05(); void p06(); void p07(); void p08(); void p09(); 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<<"訊一乙 36號 潘誠\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: p04(); break; case 6: p04(); break; case 7: p04(); break; } system("PAUSE"); cout<<"\n"; } } void p01(){ srand( time(NULL) ); int min = 1; int max = 10; int x = rand() % (max - min + 1) + min; int n, ans = x; while (n != ans){ cout << "猜猜數字是多少?(1~10):"; cin >> n; if(n < x) cout << "猜大一點\n"; if(n > x) cout << "猜小一點\n"; } cout << "猜對了!YA!"; } 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 p02(){ int user; srand(time(0)); cout << "請輸入你要出的拳(1.剪刀2.石頭3.布)"; cin >> user; game(user); } int dice() { int a; a = (rand() % 6 ) + 1; return a; } 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; } } 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 << "謝謝您使用階乘計算程式 "; } bool T(int a, int b, int c) { return (a + b > c) && (a + c > b) && (b + c > a); } void p06(){ int side1, side2, side3; cout << "請輸入三個邊長: " << endl; cin >> side1 >> side2 >> side3; if (T(side1, side2, side3)) { cout << "可以構成三角形。" << endl; } else { cout << "無法構成三角形。" << endl; } } double factorial(int n) { double fact; for(fact = 1; n > 0; n--) fact = fact * n; return fact; } void p07(){ void func(); func(); } void func(){ cout << " \n"; cout << " \n"; cout << " \n"; cout << " .:~!?JY5PPPPPPPPY?7~^. \n"; cout << " .~JPB#&&&############&&&##BPJ!: \n"; cout << " .!5#&&##BBGGGBBGGBBBBBBBBBBBB##&&#G?: \n"; cout << " ^Y#&&#BGGGGBBBBBBGGGGBB###&&&#BBBBB##&#5^ \n"; cout << " ~P&@&####B#&BPBBGGGP5Y7777??JYPB#&BGGGGB#&&5^ \n"; cout << " :G@@#GPPP5JJYJ77YYYYYYPP?777777777?YG##BGPB##@&Y: \n"; cout << " ~B&PY5GBBG5777777777777777777777?P5777?P&@##@@@BB&? \n"; cout << " .5&P?5#B5J?77777777777?Y?7777777777P@P7777?G&G5##YG@@5. \n"; cout << " ~##J775Y777777777777777?&#?7777??7777Y&#J77775@BJJYGG&@P. \n"; cout << " ^##?77777?YJ7?J777777JGJ7Y&#Y?77P#57777?G&G?777Y&#Y#&G&&@5 \n"; cout << " :##?7PB?77Y@5?&B7777775@G77Y&@#G5J5#BJ7777JB&P?77?G&@@@&B&&: \n"; cout << " Y@J7J@G7?5#@JY@5777777?#@BJ7J##YYGBB&&B5J777JG#GY77JG&##&&@! \n"; cout << " :&B77P@55#BB&?G@#PGJ7777Y&@&GJY#&5P#B#@@&#BG5YJYPBBG5YP#@&#B#P?^ \n"; cout << " 7@Y77G@&@5.5@J##B@@&PJY55#@&@&##BB&&~?#G&5JYPGBB###&@@&BPPY7?YGBGY!. \n"; cout << " 5@J77G@@&55#@#&#B#&&GP5Y?!~!&#JJYYY&G.:.?#B?777?5&#Y????7777777?J5BB5!. \n"; cout << " J@J775@&?~^P@GPPGPB@7 .. ?@Y^~~~#B... ^P&P?777?P#B5?777777777777?5B#5: \n"; cout << " ^&G77?#@G. ~@B^!7!?@Y .......!GBPPBB~.:^:^.~G#P?777?5G#BPYJ?7777777?77J##^ \n"; cout << " J@Y77J#&~ ?&5!~!P@!...:^~:...^!!^...:..... !&&B5?YBY?J5GBBBGBBB##BJ77?##. \n"; cout << " .G@577?G&J^.~PGGG5~....?P5~............... ~#BYYGBB&@#BGGGB##&&BPY?7777Y@J \n"; cout << " P@&#PYJ5#&5!..:................... ..:~Y#BJJJY5G#&@@&&#&@#GGGP?777777#B. \n"; cout << " .#@#@BY55PGG5^.......... ...:^~!7J5GB#@@&#GPG&@&&&@P^::#G:^7G@Y777777##. \n"; cout << " !!:?#Y~:....::::::^^^~!7?JY55G#&&##@@&G&@@@&YG#&##B@B. .##PG#@J777777J@Y \n"; cout << " :!!^^~7?JG@&###GPB##&#B#&&@#B&#PJJ#&#####@&&&##@@@@#BBB#@? ...G&7^J@GBJ777J&G. \n"; cout << " :PBY#&&&&&&@&BB&@&G&@@B&#BGB&@5!!5@@&#GG@#BB5?P#B#@@@&#BB&&^ .. !@5^J@#Y?775&5. \n"; cout << " :JGG@J 7@#BBBBB####@@&@&&#&@#&BB@P:J&&#@&##B5?5PP7B#B#&@@&&&@@7^^::.J@YG&?77Y#B! \n"; cout << " Y@PJ@5 5@#B#####B&@##@&BB#@&@@&&@&@@BB&#&&J5GBGYY###BB#&&B55G#&&&&#G##BY?5B#J. \n"; cout << " .~77P@^ .P@#BB##BB&@&&@##B#&@#G#@@@&@#?~~!&&GPPPG######BB#&&##GG&#GY?7J5GBP!. \n"; cout << " .B#^ ?&@####&&#B@&B##B#@B!~!J5PJ^^!YG&@&#######&&&&&&&&@&&@&5?JPBB57: \n"; cout << " .P&7. .J&@@@@#G#@#BB##&#&@#PYJ7?P#@@@B&@&&##BGGPPPPPPPPPPPPB@BPJ~. \n"; cout << " !GGYYPPJ~^~75&&##&@&GG@&#&@&&&&&#BB#&#BB@@###&&&&#######&&@BJ~: \n"; cout << " .!?7!!7J5GGB@##@&P5#@@?.~?&@##&@@&&##G#&@&BBBBBB######BBB##&&#B5?~: \n"; cout << " 7##&&#G5YJ7P@&&G5B&&@B. !&&P5YG&&&B5YB&@&##B#############BB##&&&#GJ~. \n"; cout << " .^7JY5PGGGB&&GPB&&##@J ^B&55G@@BBB#@P?P#&&##BB#############BBB##&&#57: \n"; cout << " ...5@B#&&#BB&@~ ... .7YJ!?G&BJY@B: :!YB#&###BBBB############B##&&#P7. \n"; cout << " !@&##B###&#. .... .~YGBY... ^J&@@&&#####B###########BB#&@#Y: \n"; cout << " ?@#B#####@G ......... .. . .5#Y!Y&@&&&&&###############B##@#~ \n"; func(); } ``` # 2023/12/04 課堂作業(CH7陣列與指標) ## Ch07-03 請使用者輸入8個數字,找出最大值 ``` //訊一乙 36號 潘誠 #include <iostream> const int SIZE = 8; using namespace std; int main(){ int numbers[SIZE]; cout << "請輸入8個數字, 程式將找出最大值" << endl; for (int i = 0; i < SIZE; i++) { cout << "請輸入第 " << (i+1) << " 個數字:" ; cin >> numbers[i]; } int MAX = numbers[0]; for (int i = 1; i < SIZE; i++) if (numbers[i] > MAX) MAX = numbers[i]; cout << "再輸入的數字中, 數值最大的是 " << MAX; } ``` ![image](https://hackmd.io/_uploads/r1tdtysr6.png) 輸出結果: ![image](https://hackmd.io/_uploads/BJHlcksBp.png) ## Ch07-04 氣泡排序法 阿死ㄎ一碼:L=76/u=117/c=99/a=97/s=115 ``` //訊一乙 36號 潘誠 #include <iostream> #define SIZE 5 using namespace std; int main() { char array[SIZE]={'L','u','c','a','s'}; cout << "排序前"; for (int i = 0; i < SIZE; i++) cout << array[i]; for (int i = 1; i < SIZE; i++) for (int j = 0; j < SIZE - i; j++) if (array[j] < array[j + 1]) { char temp = array[j]; array[j] = array[j + 1]; array[j + 1] = temp; } cout << endl << "排序後:"; for (int i = 0; i < SIZE; i++) cout << array[i]; } ``` ![image](https://hackmd.io/_uploads/ByirEgjrT.png) 輸出結果: ![image](https://hackmd.io/_uploads/ryKM4xsHp.png) ## Ch07-05 選擇排序法 ``` //訊一乙 36號 潘誠 #include <iostream> #define SIZE 5 using namespace std; int main(){ int a[SIZE] = {25,12,47,18,10}; int s, temp; cout << "排序前"; for (int i = 0; i < SIZE; i++) cout << a[i] << " "; for (int i = 0; i < SIZE - 1; i++){ s = i; for (int j = s + 1; j <= SIZE - 1; j++) if(a[s] > a[j]) s = j; temp = a[i]; a[i] = a[s]; a[s] = temp; } cout << endl << "排序後:"; for (int i = 0; i < SIZE; i++) cout << a[i] << " "; } ``` ![image](https://hackmd.io/_uploads/HJFCVeoH6.png) 輸出結果: ![image](https://hackmd.io/_uploads/S1QgrejSa.png) ## Ch07-06 循序搜尋法 ``` //訊一乙 36號 潘誠 #include <iostream> using namespace std; int main() { int date[10] = {2,15,3,55,46,98,54,7,6,34}; int num; cout << "請輸入要搜尋的值:"; cin >> num; for (int i = 0; i < 10; i++){ if(date[i] == num){ cout << "再註標" << i << "的位置找到" << num; return 0; } } cout << num << "不在陣列中"; } ``` ![image](https://hackmd.io/_uploads/HyOA6loST.png) 輸出結果: ![image](https://hackmd.io/_uploads/BJxx0eoBT.png) ![image](https://hackmd.io/_uploads/S1bb0ejHT.png) ## Ch07-07 二元搜尋法 ``` //訊一乙 36號 潘誠 #include <iostream> using namespace std; int main() { int a[7] = {2,16,34,47,53,67,81}; int Target, M, L = 0, R = 6; cout << "請輸入要搜尋的值:"; cin >> Target; while(L <= R){ M = (L + R) / 2; if(a[M] == Target){ cout << "在註標" << M << "的位置找到" << Target; return 0; }else if (a[M] > Target){ R = M - 1; }else if (a[M] < Target){ L = M + 1; } } cout << Target << "不在陣列中"; } ``` ![image](https://hackmd.io/_uploads/Bk2NCgiS6.png) 輸出結果: ![image](https://hackmd.io/_uploads/By2UAeirT.png) ![image](https://hackmd.io/_uploads/H1kuAesSp.png) ## 1204/1211專案練習 ``` //訊一乙 36號 潘誠 #include <stdio.h> #include <iostream> #include <cstdlib> #include <ctime> using namespace std; void p01(); void p02(); void p03(); void p04(); void p05(); int main() { string menuItem[]= { "[1]找出最大值", "[2]氣泡排序法", "[3]選擇排序法", "[4]循序搜尋法", "[5]二元搜尋法", "[0]離開" }; int i,num; int selMenu=99; while(selMenu!=0) { system("chcp 950"); //編碼 system("cls"); //清除畫面 cout<<"訊一乙 36號 潘誠\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; } system("PAUSE"); cout<<"\n"; } } ``` ``` #include <iostream> const int SIZE = 8; using namespace std; void p01(){ int numbers[SIZE]; cout << "請輸入8個數字, 程式將找出最大值" << endl; for (int i = 0; i < SIZE; i++) { cout << "請輸入第 " << (i+1) << " 個數字:" ; cin >> numbers[i]; } int MAX = numbers[0]; for (int i = 1; i < SIZE; i++) if (numbers[i] > MAX) MAX = numbers[i]; cout << "再輸入的數字中, 數值最大的是 " << MAX; } ``` ``` #include <iostream> #define SIZE 5 using namespace std; void p02() { char array[SIZE]={'L','u','c','a','s'}; cout << "排序前"; for (int i = 0; i < SIZE; i++) cout << array[i]; for (int i = 1; i < SIZE; i++) for (int j = 0; j < SIZE - i; j++) if (array[j] < array[j + 1]) { char temp = array[j]; array[j] = array[j + 1]; array[j + 1] = temp; } cout << endl << "排序後:"; for (int i = 0; i < SIZE; i++) cout << array[i]; } ``` ``` #include <iostream> #define SIZE 5 using namespace std; void p03(){ int a[SIZE] = {25,12,47,18,10}; int s, temp; cout << "排序前"; for (int i = 0; i < SIZE; i++) cout << a[i] << " "; for (int i = 0; i < SIZE - 1; i++){ s = i; for (int j = s + 1; j <= SIZE - 1; j++) if(a[s] > a[j]) s = j; temp = a[i]; a[i] = a[s]; a[s] = temp; } cout << endl << "排序後:"; for (int i = 0; i < SIZE; i++) cout << a[i] << " "; } ``` ``` #include <iostream> using namespace std; void p04() { int date[10] = {2,15,3,55,46,98,54,7,6,34}; int num; cout << "請輸入要搜尋的值:"; cin >> num; int found=0; for (int i = 0; i < 10; i++){ if(date[i] == num){ cout << "再註標" << i << "的位置找到" << num; found=1; } } if (found==0) cout << num << "不在陣列中"; } ``` ``` #include <iostream> using namespace std; void p05() { int a[7] = {2,16,34,47,53,67,81}; int Target, M, L = 0, R = 6; int found=0; cout << "請輸入要搜尋的值:"; cin >> Target; while(L <= R){ M = (L + R) / 2; if(a[M] == Target){ cout << "在註標" << M << "的位置找到" << Target; found=1; }else if (a[M] > Target){ R = M - 1; }else if (a[M] < Target){ L = M + 1; } } if (found==0) cout << Target << "不在陣列中\n"; } ``` ![image](https://hackmd.io/_uploads/H10wWEVU6.png) ![image](https://hackmd.io/_uploads/HywdZ44Up.png) ![image](https://hackmd.io/_uploads/HyJtZ4VLT.png) ![image](https://hackmd.io/_uploads/SyJqWVV8T.png) ![image](https://hackmd.io/_uploads/BkS5-VELa.png) ![image](https://hackmd.io/_uploads/rkaq-VN86.png) 輸出結果: ![image](https://hackmd.io/_uploads/Syf2WNN8T.png) ![image](https://hackmd.io/_uploads/H1P6bNVIT.png) ![image](https://hackmd.io/_uploads/HJVkMNEUT.png) ![image](https://hackmd.io/_uploads/ByWlzV4IT.png) # 心得感想(100字): C++函式真的是超方便的!。這樣不僅提高了程式碼的組織性,還方便了日後的維護。而且函式可以接受不同的參數,像是一個工廠,你給他原材料,它就會產出你需要的東西。總之,函式就是程式設計的好夥伴,讓一切都井井有條。台灣真的是做得太好啦,老鐵666 # 2023/12/18 CH7隨堂練習 ## Ch07-08 輸出字串與字元陣列大小(P.245) ``` //訊一乙 36號 潘誠 #include <iostream> using namespace std; int main() { char name1[]="Lucas Poon"; char name2[]={'L','U','C','A','S',' ','P','O','O','N'}; cout << "name1[]大小為:" << sizeof(name1) << endl; cout << "name2[]大小為:" << sizeof(name2) << endl; cout << "name1[]" << name1 << endl; cout << "name2[]" << name2 << endl; } ``` ![image](https://hackmd.io/_uploads/S1k8WwpUT.png) 輸出結果: ![image](https://hackmd.io/_uploads/Sy-vbwT8a.png) ## Ch07-27 以指標實作氣泡排序法(P.277) ``` //訊一乙 36號 潘誠 #include <iostream> using namespace std; int main() { int num; cout << "請輸入欲排序的數字數量:"; cin >> num; int *ptr = new int[num]; for(int i = 0; i < num; i++){ cout << "請輸入第" << i+1 << "個數字:"; cin >> *(ptr+i); } for(int i = 1; i < num; i++) for(int j = 0; j < num - i; j++) if(*(ptr+j) < *(ptr+j+1)){ char temp = *(ptr+j); *(ptr+j) = *(ptr+j+1); *(ptr+j+1) = temp; } cout << "排序後:"; for(int i = 0; i < num; i++) cout << *(ptr+i) << '\t'; delete[]ptr; } ``` ![image](https://hackmd.io/_uploads/B1KNeD6La.png) 輸出結果: ![image](https://hackmd.io/_uploads/S1aYevp8p.png) ## CH7課後練習(2023/12/18) ### 1.大樂透539是一種樂透型遊戲:(參考P.275) 您必須從01~39中任選6個號碼進行投注。開獎時,開獎單位將隨機開出六個號碼,這一組號碼就是該期大樂透的中獎號碼,也稱為「獎號」。您的六個選號中,如果有三個以上(含三個號碼)對中當期開出之六個號碼,即為中獎,並可依規定兌領獎金。 [大樂透中獎說明] (https://www.taiwanlottery.com.tw/lotto649/index.asp) 對中當期獎號之任三碼 普獎 對中當期獎號之任四碼 伍獎 對中當期獎號之任五碼 參獎 與當期六個獎號完全相同者 頭獎 ``` //訊一乙 36號 潘誠 #include <iostream> #include <time.h> #include <stdlib.h> using namespace std; void Bucket539(int userNum[6]) { srand(time(0)); int winNum[6]; int matchNum[39] = {0}; for (int i = 0; i < 6; i++) { int num; do { num = (rand() % 39) + 1; } while (matchNum[num - 1] > 0); winNum[i] = num; matchNum[num - 1] = 1; } cout << "本期今Bucket539的號碼是: "; for (int i = 0; i < 6; i++) cout << winNum[i] << " "; cout << std::endl; int Match = 0; for (int i = 0; i < 6; i++) { if (matchNum[userNum[i] - 1] > 0) { Match++; } } if (Match == 6) { cout << "恭喜您中了頭獎!\n"; } else if (Match == 5) { cout << "恭喜您中了參獎!\n"; } else if (Match == 4) { cout << "恭喜您中了伍獎!\n"; } else if (Match == 3) { cout << "恭喜您中了普獎!\n"; } else { cout << "再接再厲,沒有中獎。\n"; } } int main() { int userNum[6]; int num; cout << "請輸入您欲投注的6個號碼(1~39):\n"; for (int i = 0; i < 6; i++) { cout << "請輸入第" << (i + 1) << "個數字:"; cin >> num; if (num < 1 || num > 39) { cout << "輸入錯誤,請輸入有效範圍內的數字(1~39)。\n"; i--; } else { userNum[i] = num; } } Bucket539(userNum); return 0; } ``` ![image](https://hackmd.io/_uploads/r1FFLqLP6.png) 輸出結果: ![image](https://hackmd.io/_uploads/SyZ3UqLvp.png) ### 2.奇數或偶數:使用亂數函數 rand() 產生 5個1~20的整數,分別對奇數的數加總以及偶數的數加總。 *小提示: 奇數:if(num%2!=0) ``` //訊一乙 36號 潘誠 #include <iostream> #include <stdlib.h> #include <time.h> using namespace std; int main() { int num[5]; int sum1=0,sum2=0; srand(time(0)); for(int i=0;i<5;i++) { num[i]=rand()%20+1; for(int j=0;j<i;j++) { if(num[j]==num[i]) { num[i]=rand()%20+1; j=0; } } cout<<num[i]<<" "; if(num[i]%2==0) { sum1+=num[i]; } else { sum2+=num[i]; } } cout<<"\n偶數總和:"<<sum1; cout<<"\n奇數總和:"<<sum2; } ``` ![image](https://hackmd.io/_uploads/ByogY5Uwa.png) 輸出結果: ![image](https://hackmd.io/_uploads/Sy2-t5LwT.png) #### .在「字元陣列」中放入10個英文字母「television」,然後將字母依小到大(a到z)順序從螢幕輸出。(參考P.277)(注意:字母須由小排到大!!) ``` //訊一乙 36號 潘誠 #include<iostream> #include<algorithm> using namespace std; int main() { char charArray[] = "pneumonoultramicroscopicsilicovolcanoconiosis"; sort(charArray, charArray + 45); cout << "排序後的字元陣列: " << charArray << endl; return 0; } ``` ![image](https://hackmd.io/_uploads/SyfV95LwT.png) 輸出結果: ![image](https://hackmd.io/_uploads/ByhSqcLDT.png) ## 心得感想(100字)(12/18): 在C++的實習過程中,我學到了如何運用指標實作氣泡排序法,這有效提升了對資料排序的理解。同時,深入研究字串和字元陣列處理,增進了對字串操作的熟練度。此外,學習亂數函數的應用,豐富了生成隨機數據的技能,提高了實際應用的靈活性。這段經驗不僅擴展了我的程式語言技能,也讓我更深入了解C++在實務上的應用。老鐵刷一波77777777777777777777777777777777777777777 # 2023/12/25(CH7課後練習) ## 1.請使用二維陣列存取學生的多科成績,並輸出班級成績單。 ### 輸出結果如下: ### 輸入每位學生的成績(依序為國、英、數、基電、物理,以空格區隔) ### 1 號學生:80 80 90 80 90 ### 2 號學生:80 60 50 60 60 ### 3 號學生:90 90 60 90 60 ### 4 號學生:60 50 50 60 60 ### 班級成績單 ### 座號 國文 英文 數學 基電、物理 總分 ### ============================== ### 1 80 80 90 80 90 420 ### 2 80 60 50 60 60 310 ### 3 90 90 60 90 60 390 ### 4 60 50 50 60 60 280 ``` //訊一乙 36號 潘誠 #include <iostream> using namespace std; const int NUM_STUDENTS = 4; const int NUM_SUBJECTS = 5; int main() { int grades[NUM_STUDENTS][NUM_SUBJECTS]; int total[NUM_STUDENTS] = {0}; for (int i = 0; i < NUM_STUDENTS; i++) { cout << "輸入每位學生的成績(依序為國、英、數、基電、物理,以空格區隔)" << endl; cout << i + 1 << " 號學生:"; for (int j = 0; j < NUM_SUBJECTS; j++) { cin >> grades[i][j]; total[i] += grades[i][j]; // 計算總分 } } cout << "班級成績單" << endl; cout << "座號 國文 英文 數學 基電、物理 總分" << endl; cout << "===============================\n"; for (int i = 0; i < NUM_STUDENTS; i++) { cout << i + 1 << " "; for (int j = 0; j < NUM_SUBJECTS; j++) { cout << grades[i][j] << " "; } cout << total[i] << endl; } return 0; } ``` ![image](https://hackmd.io/_uploads/HyGx69UvT.png) 輸出結果: ![image](https://hackmd.io/_uploads/Hyk4a98Pp.png) ## 2.在圖一的表格中,橫排稱為列(row),直排稱為行(column),以Xij來表示表格X中的第i列第j行的元素。如圖一中的X00=1、X12=6。 請設計一個程式可執行以下工作: (1) 根據使用者輸入的數值設定表格大小(先輸入列,再輸入行) (2) 由使用者輸入數值填滿表格 (3) 輸入完成後,將整個表格印出 ``` //訊一乙 36號 潘誠 #include <iostream> int main() { int rows, columns; std::cout << "請輸入表格行數:"; std::cin >> rows; std::cout << "請輸入表格列數:"; std::cin >> columns; int** table = new int*[rows]; for (int i = 0; i < rows; ++i) { table[i] = new int[columns]; } std::cout << "請輸入表格數值:" << std::endl; for (int i = 0; i < rows; ++i) { for (int j = 0; j < columns; ++j) { std::cout << "輸入 A" << i << j << ":"; std::cin >> table[i][j]; } } std::cout << "印出完整表格:" << std::endl; for (int i = 0; i < rows; ++i) { for (int j = 0; j < columns; ++j) { std::cout << "A" << i << j << "=" << table[i][j] << " "; } std::cout << std::endl; } for (int i = 0; i < rows; ++i) { delete[] table[i]; } delete[] table; return 0; } ``` ![image](https://hackmd.io/_uploads/rJHSCqUva.png) 輸出結果: ![image](https://hackmd.io/_uploads/B1oXR5IPT.png) ## 3.請設計一程式可執行以下工作: (1) 輸入5個值並存入一維陣列a中 (2) 計算陣列a中5個值的總和,並印出輸入 ``` //訊一乙 36號 潘誠 #include <iostream> using namespace std; int main() { int a[5],sum; for(int i=0;i<5;i++) { cout << "數值" << i+1 << ":"; cin >> a[i]; sum += a[i]; } cout << "加總:" << sum; } ``` ![image](https://hackmd.io/_uploads/HklfJsUPT.png) 輸出結果: ![image](https://hackmd.io/_uploads/ryq7yoLvT.png) ## 4.請設計一程式可執行以下工作: (1) 宣告一個指標p,並使用new運算子動態配置一塊記憶體 (2) 輸入5個值,放到指標p所控制的5個連續記憶體位址 (3) 印出指標變數p (4) 找出5個值中的最大值,印出最大值及其所在的記憶體位址 輸入 ``` //訊一乙 36號 潘誠 #include <iostream> int main() { // (1) 宣告一個指標p,並使用new運算子動態配置一塊記憶體 int* p = new int[5]; // (2) 輸入5個值,放到指標p所控制的5個連續記憶體位址 std::cout << "請輸入5個整數值:" << std::endl; for (int i = 0; i < 5; ++i) { std::cin >> *(p + i); } // (3) 印出指標變數p std::cout << "指標變數p的值:" << p << std::endl; // (4) 找出5個值中的最大值,印出最大值及其所在的記憶體位址 int max = *p; int* maxAddress = p; for (int i = 1; i < 5; ++i) { if (*(p + i) > max) { max = *(p + i); maxAddress = p + i; } } std::cout << "最大值:" << max << std::endl; std::cout << "最大值所在的記憶體位址:" << maxAddress << std::endl; // 釋放動態配置的記憶體 delete[] p; return 0; } ``` ![image](https://hackmd.io/_uploads/SkUukoIwp.png) 輸出結果: ![image](https://hackmd.io/_uploads/r1lqJi8va.png) ## 心得感想(100字)(12/25): 透過C++的實習,深刻體會到陣列和記憶體位址的重要性。學會有效操作陣列提升程式效能,同時透過記憶體位址的理解,提高程式的靈活性和效率。這次經驗不僅讓我更熟悉C++的核心概念,也拓展了我對程式設計的深層認識。太神啦瑞斯,瑞斯一打三!!!