# 2023918 課堂作業 ## Ch03-12各種字面常數表示法的應用 ### 程式碼 ``` #include<iostream> using namespace std; int main() { int i; double d; i = 074; d = 6.02e22; cout << "i= " << i << " d= " << d << endl; i = 0xA38; d = 3.14159e-1; cout << "i= " << i << " d= " << d << endl; } ``` ![](https://hackmd.io/_uploads/B1ST-OH1T.png) ### 執行結果: ![](https://hackmd.io/_uploads/H1TgxdBy6.png) ## Ch03-18讓使用者從鍵盤輸入資料 ### 程式碼 ``` #include<iostream> using namespace std; int main() { int grade; int student_ID; cout<<"請問你是幾年級的學生"; cin>>grade; cout<<"請問你的學號是:"; cin>>student_ID; cout<<"你是"<<grade<<"年級的學生"<<"\n"; cout<<"學號是" <<student_ID; } ``` ![](https://hackmd.io/_uploads/rJJWfOSJT.png) ### 執行結果: ![](https://hackmd.io/_uploads/r1kmedHyp.png) ## Ch03-19換匯程式 ### 程式碼 ``` #include<iostream> using namespace std; #define US 29.5 //定義巨集常數 美金匯率 #define JP 0.277 //定義巨集常數 日幣匯率 int main() { double money; cout<<"輸入要兌換的金額"; cin>>money; cout<<"可兌換"<<money/US<<"美金\n"; cout<<"或兌換"<<money/US<<"日幣"; } ``` ![](https://hackmd.io/_uploads/B1KXMOSJa.png) ### 執行結果: ![](https://hackmd.io/_uploads/BkuUWdHJT.png) ## Ch03-20 交換兩變數的值 ### 程式碼 ``` #include<iostream> using namespace std; int main() { int a=10 , b=20; //宣告兩個int變數a,b int temp; //宣告站存整數變數temp cout<<"交換前a="<<a<<"\t b="<<b<<"\n"; temp=a; //將變數a的值指定給暫存變數 a=b; //把變數b的值指定給a b=temp; //把暫存變數指定給a的值指定給b cout<<"交換後a="<<a<<"\t b="<<b<<"\n"; } ``` ![](https://hackmd.io/_uploads/H1pYH_rJa.png) ### 執行結果: ![](https://hackmd.io/_uploads/S194__S1T.png) ## Ch03-21 大小寫轉換 ## 程式碼 ``` #include<iostream> using namespace std; int main() { char upper='S',lower; //宣告兩個字元 lower=upper+32; cout<<upper<<"的小寫是"<<lower<<"\n" ; } ``` ![](https://hackmd.io/_uploads/S1eiKuSkT.png) ### 執行結果: ![](https://hackmd.io/_uploads/SJCWquryT.png) ### 心得感想 知道了一些字符之後我也能寫出一些簡單的程式了 希望之後還能學到更多東西 讓我的程式設計能力更精進 ### 實作練習 ## P74_1 ### 程式碼 ``` #include<iostream> using namespace std; #define SIZE 10 int main() { cout<<"SIZE="<<SIZE; } ``` ![](https://hackmd.io/_uploads/rybv6ur1a.png) ### 執行結果 ![](https://hackmd.io/_uploads/BJJ3TdH16.png) ## P74_2 ### 程式碼 ``` #include<iostream> using namespace std; int main() { int a=10; double b=101.7; char c='c'; cout<<"a="<<a<<"b="<<b<<"c="<<c; } ``` ![](https://hackmd.io/_uploads/Bkme1KS1a.png) ### 執行結果 ![](https://hackmd.io/_uploads/Hy8GJYSJ6.png) ## P74_5 ### 程式碼 ``` #include<iostream> using namespace std; int main() { cout<<"我正在學習\"C++\"程式語言"; } ``` ![](https://hackmd.io/_uploads/rkDf-FrJp.png) ### 執行結果 ![](https://hackmd.io/_uploads/BJjagtSkT.png) ## P74_7 ### 程式碼 ``` #include<iostream> using namespace std; int main() { int a=10 , b=20 , c=30; int temp; cout<<"交換前a="<<a<<"\t b="<<b<<"\t c="<<c<<"\n"; temp=b; b=a; a=c; c=temp; cout<<"交換後a="<<a<<"\t b="<<b<<"\tc="<<c<<"\n"; } ``` ![](https://hackmd.io/_uploads/HyA1mYryT.png) ### 執行結果 ![](https://hackmd.io/_uploads/SJ2AMtBya.png) # 2023925 課堂作業 ## P81 Ch4-5 前置與後置遞增運算子 ### 程式碼 ```#include<iostream> 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/SJMsL-h1T.png) ### 執行結果 ![](https://hackmd.io/_uploads/S1DtUWhJp.png) ## P81 Ch4-6 關係運算子練習 ### 程式碼 ``` #include<iostream> using namespace std; int main() { int i=3,j=3; cout<<boolalpha; cout<<"(i==j):"<<(i== j)<<"\n"; cout<<"(i>j):"<<(i>j)<<"\n"; cout<<"(++i>j):"<<(++i>j)<<"\n"; cout<<"(j--<3):"<<(j--<j)<<"\n"; cout<<"(i!=j):"<<(i!=j)<<"\n"; } ``` ![](https://hackmd.io/_uploads/B1qoN-216.png) ### 執行結果 ![](https://hackmd.io/_uploads/rkJfSb2J6.png) ## P81 Ch4-7 邏輯運算子練習 ### 程式碼 ``` #include<iostream> using namespace std; int main() { int i=3; bool b=false; cout<<boolalpha; cout<<"i="<<i<<"\tb="<<b<<"\n"; cout<<"i&&b:"<<(i&&b)<<"\n"; cout<<"i||b:"<<(i||b)<<"\n"; cout<<"i&&!b:"<<(i&&!b)<<"\n"; cout<<"i||!b:"<<(i||!b)<<"\n"; } ``` ![](https://hackmd.io/_uploads/S16Y_Zhkp.png) ### 執行結果 ![](https://hackmd.io/_uploads/BJidd-316.png) ## 實習心得 今天我學到了運算式 運算子和運算元 運算子大概的意思就是加減乘除 運算原是裡面的數字 而運算式就是指整個運算的式子 跟小學數學有點關係 不過其中的數字是可以代變數的 另外還有學到遞增 遞減的運算子 總而言之今天學了一堆以前不知道的東西 ## Ch04-08 位元運算子練習 ### 程式碼 ``` #include<iostream> using namespace std; int main() { short i=9 ,j=25; cout<<"i>>1="<<(i>>1)<<"\n"; cout<<"i<<1="<<(i<<1)<<"\n"; cout<<"~i="<<(~i)<<"\n"; cout<<"i="<<i<<"\tj="<<j<<"\n"; cout<<"i&1="<<(i&1)<<"\n"; cout<<"i|1="<<(i|1)<<"\n"; cout<<"i^1="<<(i^1)<<"\n"; } ``` ![](https://hackmd.io/_uploads/rJg7YiRkp.png) ### 執行結果 ![](https://hackmd.io/_uploads/BkBEtjR1p.png) ### 位元運算子練習_運算過程: ![](https://hackmd.io/_uploads/r1kmosR1p.png) ## 應用實例練習(P.98) ## Ch04-15 模擬換幣機 ### 程式碼 ``` #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/r1D8u30y6.png) ### 執行結果 ![](https://hackmd.io/_uploads/ryxTL2Rkp.png) ## Ch04-16 華氏溫度轉攝氏溫度 ### 程式碼 ``` #include<iostream> using namespace std; int main() { float C,F; cout<<"請輸入華式的溫度:"; cin>>F; C=(F-32)*5/9; //溫度換算公式 cout<<"換算成攝氏溫度為"<<C<<"度"; } ``` ![](https://hackmd.io/_uploads/HJnzF2CJ6.png) ### 執行結果 ![](https://hackmd.io/_uploads/H1AeYnR16.png) ## Ch04-18 成績計算程式 ### 程式碼 ``` #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/HJU0q3A16.png) ### 執行結果 ![](https://hackmd.io/_uploads/SkN3c20J6.png) ### 實習心得 今天學了2進位式的算法 就是8位代碼 第一位代表1第一位代表2第一位代表4以此類推 還知道了運算子的優先順序及結合順序 和如何強制把檔案的類型更變 而且學到的東西也藉由位元運算子練習等等的例題實際操作一遍了 總之今天學的東西有點數學課的感覺 # 20231016 課堂作業(CH5流程控制) ### 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.夏季電費計算 夏季電費,請輸入用電度數: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個等級,折扣方式如下圖所示: (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) ### 05-24電影分級制度 下列程式將依據年齡層來判斷可觀賞的影片等級(0~5歲:普遍級、6~11歲:普遍級及保護級、12~17歲:限制級以外的影片、18歲(含)以上:各級影片)。 ### 程式碼 ``` #include<iostream> using namespace std; int main() { int a; cin>>a; cout<<a<<"歲\n"; if (a>=18) cout<<"各級影片"; else if (a>=12 and a<=17) cout<<"限制級以外的影片"; else if (a>=6 and a<=11) cout<<"普遍級及保護級"; else cout<<"普遍級"; } ``` ![](https://hackmd.io/_uploads/Bye7koQM6.png) ### 執行結果 ![](https://hackmd.io/_uploads/HytV1oXza.png) ### 05-27 猜數字遊戲 下列程式為一個猜數字遊戲(假設答案為7),遊戲會發出提示訊息要使用者輸入 所要猜的數字(1~10),若猜錯,遊戲會繼續進行,直到猜對為止。 ### 程式碼 ``` #include<iostream> using namespace std; int main() { int a,b; while(a!=7){ cin>>b; if (b==7){ break; } else if (b!=7) cout<<"遊戲繼續\n";} cout<<"遊戲結束"; } ``` ![](https://hackmd.io/_uploads/ryWUbo7Ma.png) ### 執行結果 ![](https://hackmd.io/_uploads/S1AdWiQzp.png) ### 05-28 2的n次方 ### 程式碼 ``` #include<iostream> using namespace std; int main() { int a,b; b=1; cout<<"請輸入二的幾次方"; cin>>a; cout<<(b<<a); } ``` ![](https://hackmd.io/_uploads/r1mQmoXMp.png) ### 執行結果 ![](https://hackmd.io/_uploads/B1jNQsmMa.png) ### 05-29 成績加總 ### 程式碼 ``` #include<iostream> using namespace std; int main() { int a,b,c,d,e; cin>>a; cin>>b; cin>>c; cin>>d; cin>>e; cout<<a+b+c+d+e<<"分"; } ``` ![](https://hackmd.io/_uploads/rJ9W4jXzp.png) ### 執行結果 ![](https://hackmd.io/_uploads/SJgeEoQM6.png) ### 05-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; } } ``` ![](https://hackmd.io/_uploads/H1U_Ismfa.png) ### 執行結果 ![](https://hackmd.io/_uploads/H14LLoQz6.png) ### 05-32 階層計算機 ### 程式碼 ### 執行結果 # 1016課後練習 ## 1.心理測驗 (if…else if 應用) 發生船難,你選擇留下什麼? 選擇(1)留下老人和孕婦 選擇(2)留下老人和嬰兒 選擇(3)留下老人和金銀珠寶 選擇(4)留下孕婦和嬰兒 選擇(5)留下孕婦和金銀珠寶 選擇(6)留下嬰兒和金銀珠寶 輸入各選擇後回應如下: (1)你的情感很細膩,善於觀察細節。在感情中,相比於戀人的甜言蜜語,你更在乎對方實際的行動。 (2)在感情中,你不僅洞察力超好,第六感也很強,越是親近的人這種直覺越敏銳,所以另一半對你很難有所隱藏,因為你可以憑借著蛛絲馬跡得到你想要知道的消息。 (3)你是個典型的顏控,在擇偶時很注重另一半的外表和身高。 (4)面對感情你很承盾,一方面你很感性,渴望浪漫熱烈的愛情;另一方面你又很理性,明白現實的殘酷和金錢的重要性 (5)在感情方面你很挑剔,所以很難遇到心動的對象。不過在戀愛時你卻很專一,一旦喜歡上某個人,你就會全心全意的對他好,所以和你談戀愛是一件很幸福的事。 (6)在感情中你很缺乏安全感,有時候會忍不住通過試探或考驗的方式去確認對方是否愛你。 其他輸入: 亂輸入,活該一輩子舔狗命!!! ### 程式碼 ``` #include<iostream> using namespace std; int main() { int a; cout<<"發生船難,你選擇留下什麼?\n選擇\n(1)留下老人和孕婦選擇\n(2)留下老人和嬰兒選擇\n(3)留下老人和金銀珠寶選擇\n(4)留下孕婦和嬰兒選擇\n(5)留下孕婦和金銀珠寶選擇\n(6)留下嬰兒和金銀珠寶\n"; cin>>a; if (a==1) cout<<"(1)你的情感很細膩,善於觀察細節。在感情中,相比於戀人的甜言蜜語,你更在乎對方實際的行動。"; else if (a==2) cout<<"(2)在感情中,你不僅洞察力超好,第六感也很強,越是親近的人這種直覺越敏銳,所以另一半對你很"; else if (a==3) cout<<"(3)你是個典型的顏控,在擇偶時很注重另一半的外表和身高。"; else if (a==4) cout<<"(4)面對感情你很承盾,一方面你很感性,渴望浪漫熱烈的愛情;另一方面你又很理性,明白現實的殘酷和金錢的重要性"; else if (a==5) cout<<"(5)在感情方面你很挑剔,所以很難遇到心動的對象。不過在戀愛時你卻很專一,一旦喜歡上某個人,你就會全心全意的對他好,所以和你談戀愛是一件很幸福的事。"; else if (a==6) cout<<"(6)在感情中你很缺乏安全感,有時候會忍不住通過試探或考驗的方式去確認對方是否愛你。"; else cout<<"亂輸入,活該一輩子舔狗命!!!"; } ``` ![](https://hackmd.io/_uploads/BJhnH8qba.png) ### 執行結果 ![](https://hackmd.io/_uploads/HkjJUL5-T.png) ## 2.判斷座標(x,y)在第幾象限。 (巢狀if…else 應用) 下列程式將讓使用者輸入x、y值,判斷(x, y)在第幾象限。若x與y皆為正數是在第一象限;皆為負數是在第三象限;若x為正數、y為負數,在第四象限;若x為負數、y為正數,則在第二象限。 ### 程式碼 ``` #include<iostream> using namespace std; int main() { int a,b; cout<<"請輸入x值(不可輸入0):\n請輸入y值(不可輸入0):\n"; cin>>a; cin>>b; if (a>0) if (b>0) cout<<"第一象限"; else cout<<"第四象限"; if (a<0) if (b>0) cout<<"第二象限"; else cout<<"第三象限"; } ``` ![](https://hackmd.io/_uploads/BkXVKU5WT.png) ### 執行結果 ![](https://hackmd.io/_uploads/ry28Y8cb6.png) ## 3.用*組成三角形(for迴圈的應用) 輸入層數n即輸出層的*三角形。 ### 程式碼 ``` #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; } } ``` ![](https://hackmd.io/_uploads/HJKqsIqW6.png) ### 執行結果 ![](https://hackmd.io/_uploads/BJudoUcZT.png) ## 4.十二生肖查詢(switch…case的應用) ### 程式碼 ``` #include<iostream> using namespace std; int main() { int year; cout<<"請輸入民國(年):"; cin>> year; int zodiac=(year%12); switch(zodiac) { case 1: cout<<"鼠"; break; case 2: cout<<"牛"; break; case 3: cout<<"虎"; break; case 4: cout<<"兔"; break; case 5: cout<<"龍"; break; case 6: cout<<"蛇"; break; case 7: cout<<"馬"; break; case 8: cout<<"羊"; break; case 9: cout<<"猴"; break; case 10: cout<<"雞"; break; case 11: cout<<"狗"; break; case 0: cout<<"豬"; break; } } ``` ![](https://hackmd.io/_uploads/Sycon85-p.png) ### 執行結果 ![](https://hackmd.io/_uploads/HJUT38qW6.png) ## 5.閏年 除以 4 能整除,且除以 100 不能整除 如果剛好是 100 的倍數,且除以 400 能整除 舉例來說 2000 年是 100 的倍數且除以 400 能整除,所以是 2000 年是閏年,例如 2100 年雖然是 4 的倍數,但除以 100 能整除,所以 2100 年是平年。 ### 程式碼 ``` #include<iostream> using namespace std; int main() { int a; cout<<"請輸入一個西元年"; cin>>a; if (a%4==0) cout<<"閏年"; else cout<<"平年"; } ``` ![](https://hackmd.io/_uploads/BJg_mP9-p.png) ### 執行結果 ![](https://hackmd.io/_uploads/H1lfEQPcZ6.png) ![](https://hackmd.io/_uploads/rJ9HmPqW6.png) ## 6.重複計算BMI(while 迴圈)(P.129)(必考) ### 程式碼 ``` #include<iostream> using namespace std; int main() { char go_again = 'Y'; float h,w; while (go_again =='y'||go_again =='Y'){ cout << "請輸入身高(公分):"; cin >> h; cout << "請輸入體重(公斤):"; cin >> w; cout << "您的BMI?:" << w/ (h * h) *10000 <<"\n"; cout << "要繼續計算另一位嗎?\n" << "(要請輸入Y或y,若不要請輸入非Y或y的字元):"; cin >> go_again; } } ``` ![](https://hackmd.io/_uploads/r1E5EvqZT.png) ### 執行結果 ![](https://hackmd.io/_uploads/SyhvND5ba.png) ## 7. 計算兩整數的最大公因數(while 迴圈)(P.131)(20231023) ### 程式碼 ``` #include<iostream> using namespace std; int main() { int a,b,c; cout<<"計算兩整數的大公因數\n"; cout<<"請輸入第一個數字"; cin>>a; cout<<"請輸入第二個數字"; cin>>b; while (b!=0){ c=a%b; a=b; b=c; } cout<<"最大公因數是:"<<a; } ``` ![](https://hackmd.io/_uploads/BycYKYmzT.png) ### 執行結果 ![](https://hackmd.io/_uploads/HJcIqKQfT.png) ## 8. 密碼檢驗程式(P.133)(自行設計)(20231023) 請設計一個密碼輸入次數不可大於三次的密碼檢驗程式,密碼=123,當密碼輸入正確請顯示:歡迎進入本系統。密碼輸入錯誤3次,無法登入。 ### 程式碼 ``` #include<iostream> using namespace std; int main() { int a,b; while(b != 3){ cout<<"輸入ATM密碼:"; cin>>a; b ++; if (a == 123){ cout<<"歡迎進入本系統"; break; } else if (b == 3){ cout<<"密碼輸入錯誤3次,無法登入"; break; } else{ cout <<"第"<<b<< "密碼輸入錯誤"; } } } ``` ![](https://hackmd.io/_uploads/rki5s5XGT.png) ### 執行結果 ![](https://hackmd.io/_uploads/Skwz3q7Gp.png) ![](https://hackmd.io/_uploads/HyYQncQfT.png) # 實習心得 今天學了if判斷else其他case個別條件值的列出還有while迴圈等等的程式碼也都把它們練習過一次了而且剛好上次資訊課也是在教if else elif都沒有沒聽懂的不過我覺得python比較簡單一些 # 20231106 課堂作業(CH6函式) ## 隨堂練習 ## Ch06-21 以時間亂數種子產生8個200~600的亂數 ### 程式碼: ``` #include<iostream> #include<stdlib.h> #include<time.h> using namespace std; int main() { srand(time(0)); for (int i=0;i<8;i++) cout<<(rand()%401)+200<<"\n"; } ``` ![image.png](https://hackmd.io/_uploads/SJfx2ZLX6.png) ### 執行結果: ![image.png](https://hackmd.io/_uploads/SyH0i-I7p.png) ## 應用實例練習 ## Ch06-25 猜數字遊戲 ### 程式碼: ``` #include<iostream> #include<stdlib.h> #include<time.h> using namespace std; int number(){ int a=(rand()%10)+1; return a; } int main() { srand(time(0)); int n,b; b=number(); while(n!=b) { cout <<"猜猜數字是多少?(1~10):"; cin>>n; if(n>b) cout<<"猜錯了,在小一點\n"; else if(n<b) cout<<"猜錯了,在大一點\n"; } cout<<"猜對了,YA!"; } ``` ![image.png](https://hackmd.io/_uploads/HkkxJfIm6.png) ### 執行結果: ![image.png](https://hackmd.io/_uploads/SyV3R-8mp.png) ## Ch06-26 猜拳遊戲 ### 程式碼: ``` #include<iostream> #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/H1l-uzUma.png) ### 執行結果: ![image.png](https://hackmd.io/_uploads/HytADGI7p.png) ## Ch06-28 猜點數遊戲 ### 程式碼: ``` #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"; cout<<"輸入1~6其中一個數字,猜骰子點數(或輸入0以結束遊戲):"; cin>>num1; } } int dice() { int a ; a = (rand()%6)+1; return a; } ``` ![image.png](https://hackmd.io/_uploads/ryZc_zLX6.png) ### 執行結果: ![image.png](https://hackmd.io/_uploads/rJcwufLma.png) ## 心得感想(100字): 今天學到了很多東西,例如:函數 隨機亂數等等的程式碼也全都應用了一遍,我覺得我的程式設計的能力已經比剛開學好很多了 ## Ch06-24 查詢語音費率函式 下列程式為依據手機月租費方案(如下),查詢語音費率的函式。 y 月租費196:每秒0.0869元。 y 月租費396:每秒0.0782元。 y 月租費796:每秒0.0696元。 ### 程式碼: ``` //資訊一乙 31號 陳奕鋒 #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/H1bCROdET.png) ### 執行結果: ![image](https://hackmd.io/_uploads/rJcoCu_Va.png) ## Ch06-27 階乘函式 利用階乘函式factorial()傳回n!的值給main()。 ### 程式碼: ``` //資訊一乙 31號 陳奕鋒 #include<iostream> using namespace std; double factorial(int n){ double fact; for(fact = 1;n>0;n--) fact=fact*n; return fact; } int main() { while(true){ cout<<"請輸入1-170間的小數(輸入0即結束程式):"; int num=0; cin>>num; if(num==0) break; cout<<num<<"!等於"<<factorial(num)<<endl; } cout<<endl<<"謝謝您使用計算機程式"; } ``` ![image](https://hackmd.io/_uploads/BJw9GF_V6.png) ### 執行結果: ![image](https://hackmd.io/_uploads/SktdGK_NT.png) # 課後練習 ## 1.組成三角形 以函式的方式撰寫判斷輸入的三個值是否可組成三角形之程式(判斷條件:任兩邊和須大於第三邊)。 ### 程式碼: ``` //資訊一乙 31號 陳奕鋒 #include<iostream> using namespace std; double tr(float a,float b,float c){ if((a+b)>c && (b+c)>a && (a+c)>b){ return 1; }else{ return 0; } } int main() { double a,b,c,t; cout<<"請輸入三角形的三個邊:\n"; cin>>a; cin>>b; cin>>c; t=tr(a,b,c); if(t==1){ cout<<a<<"_"<<b<<"_"<<c<<"可組成三角形\n"; }else{ cout<<a<<"_"<<b<<"_"<<c<<"不可組成三角形\n"; } } ``` ![image](https://hackmd.io/_uploads/S1FeptdNp.png) ### 執行結果: ![image](https://hackmd.io/_uploads/HyYRnFONT.png) ## 2.遞迴 能使用遞迴的程式技巧實作階乘運算。 ### 程式碼: ``` //資訊一乙 31號 陳奕鋒 #include<iostream> using namespace std; long double fact(int n){ if(n==1){ return 1; }else{ return (n*fact(n-1)); } } int main() { int x; while(true){ cout<<"請輸入1-170間的整數:\n"; cin>>x; if(x==0) break; cout<<x<<"!="<<fact(x)<<endl; } } ``` ![image](https://hackmd.io/_uploads/SkySk5d4a.png) ### 執行結果: ![image](https://hackmd.io/_uploads/H1B7kquN6.png) ## 3.專案練習 ### 程式碼: ``` #include <stdio.h> #include <iostream> #include <cstdlib> #include <ctime> using namespace std; int number(){ int a=(rand()%10)+1; return a; } void p01(){ srand(time(0)); int n,b; b=number(); while(n!=b) { cout <<"猜猜數字是多少?(1~10):"; cin>>n; if(n>b) cout<<"猜錯了,在小一點\n"; else if(n<b) 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(); 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"; cout<<"輸入1~6其中一個數字,猜骰子點數(或輸入0以結束遊戲):"; cin>>num1; } } int dice() { int a ; a = (rand()%6)+1; return a; }; 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<<"謝謝您使用計算機程式"; } double tr(float a,float b,float c){ if((a+b)>c && (b+c)>a && (a+c)>b){ return 1; }else{ return 0; } } void p06(){ double a,b,c,t; cout<<"請輸入三角形的三個邊:\n"; cin>>a; cin>>b; cin>>c; t=tr(a,b,c); if(t==1){ cout<<a<<"_"<<b<<"_"<<c<<"可組成三角形\n"; }else{ cout<<a<<"_"<<b<<"_"<<c<<"不可組成三角形\n"; } } long double fact(int n){ if(n==1){ return 1; }else{ return (n*fact(n-1)); } } void p07(){ int x; while(true){ cout<<"請輸入1-170間的整數:\n"; cin>>x; if(x==0) break; cout<<x<<"!="<<fact(x)<<endl; } } 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<<"程式設計實習 資訊一乙 31號 陳奕鋒\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"; } } ``` # 20231204 課堂作業(CH7陣列與指標) ## 隨堂練習 ## Ch07-03 請使用者輸入8個數字,找出最大值 ### 程式碼: ``` //資訊一乙 31號 陳奕鋒 #include <iostream> const int size = 8; using namespace std; int main() { int number[size]; cout<<"請輸入8個數字,程式將找出找大值\n"; for(int i=0;i<size;i++){ cout<<"請輸入第"<<(i+1)<<"個數字:"; cin>>number[i]; } int max = number[0]; for (int i=1;i<size;i++) if(number[i]>max) max=number[i]; cout<<"在輸入的數字中,數值最大的是"<<max; } ``` ![image](https://hackmd.io/_uploads/rkOo9kjHT.png) ### 執行結果: ![image](https://hackmd.io/_uploads/SkX1j1iHa.png) ## Ch07-04 氣泡排序法 ### 程式碼: {B 66 e 101 n 110 } ``` //資訊一乙 31號 陳奕鋒 #include <iostream> #define size 3 using namespace std; int main() { char array[size] = {'B','e','n'}; cout<<"排序前 = "; for (int i=0; i<size;i++) cout<<array[i]; for (int i=1; i<size;i++) for (int j=1; j<size-i;j++) if(array[j]<array[j+1]){ char t=array[j]; array[j]=array[j+1]; array[j+1]=t; } cout<<"\n排序後:"; for(int i=0;i<size;i++) cout<<array[i]; } ``` ![image](https://hackmd.io/_uploads/HyAzwxirp.png) ### 執行結果: ![image](https://hackmd.io/_uploads/H10QwljBT.png) ## Ch07-05 選擇排序法 ### 程式碼: ``` //資訊一乙 31號 陳奕鋒 #include <iostream> #define size 5 using namespace std; int main() { int a[size] = {25,12,47,18,10}; int s,t; 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; t=a[i]; a[i]=a[s]; a[s]=t; } cout<<"\n排序後:"; for(int i=0;i<size;i++) cout<<a[i]<<" "; } ``` ![image](https://hackmd.io/_uploads/HkRU6eoH6.png) ### 執行結果: ![image](https://hackmd.io/_uploads/HyqwaloBa.png) ## Ch07-06 循序搜尋法 ### 程式碼: ``` //資訊一乙 31號 陳奕鋒 #include <iostream> using namespace std; int main() { int data[10] = {2,15,3,55,46,98,54,7,6,34}; int num; cout << "請輸入要搜尋的值:"; cin >> num; for(int i = 0; i < 10; i++){ if(data[i] == num){ cout << "在註標"<<i<<"的位置找到"<<num; return 0; } } cout << num << "不在陣列中"; } ``` ![image](https://hackmd.io/_uploads/BJSyClirT.png) ### 執行結果: ![image](https://hackmd.io/_uploads/HyeZAxsSa.png) ## Ch07-07 二元搜尋法 ### 程式碼: ``` //資訊一乙 31號 陳奕鋒 #include <iostream> using namespace std; int main(){ int a[7] = {2,16,34,47,53,67,81}; int n,m,l=0,r=6; int found=0; cout << "請輸入要搜尋的值:"; cin >> n; while(l<=r){ m=(l+r)/2; if(a[m] == n) { found=1; cout << "在註標" << m << "的位置找到"<<n; return 0; }else if(a[m] > n) { r=m-1; }else if(a[m] < n) { l=m+1; } } if(found==0) cout << n << "不在陣列中"; } ``` ![image](https://hackmd.io/_uploads/BkS_IXEUp.png) ### 執行結果: ![image](https://hackmd.io/_uploads/SkFNI7NI6.png) ## 1204專案練習 ### 程式碼: ``` //資訊一乙 31號 陳奕鋒 #include <iostream> #include <stdio.h> #include <cstdlib> #include <ctime> const int SIZE=8; const int sIZE=8; const int SIZe=5; 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<<"程式設計實習 資訊一乙 31號 陳奕鋒\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"; } } void p01(){ int numbers[SIZE]; cout << "請輸入8個數字, 將找出最大值\n"; 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; } void p02(){ char name[sIZE] ={'B','e','n'}; cout << "排序前:"; for (int i= 0;i<sIZE;i++) cout << name[i]; for (int i=1;i<sIZE;i++) for (int j=0;j<sIZE-i;j++) if (name[j]<name[j+1]) { char t=name[j]; name[j]=name[j+1]; name[j+1]=t; } cout << "\n排序前:"; for (int i= 0;i<sIZE;i++) cout << name[i]; } void p03(){ int name[SIZe] ={25,12,47,18,10}; int s,t; cout << "排序前:"; for (int i= 0;i<SIZe;i++) cout << name[i]<< " "; for (int i= 0;i<SIZe -1;i++){ s=i; for (int j=s+1;j<=SIZe-1;j++) if (name[s]>name[j]) s=j; t=name[i]; name[i]=name[s]; name[s]=t; } cout << "\n排序前:"; for (int i= 0;i<SIZe;i++) cout << name[i]<<" "; } void p04(){ int data[10] = {2,15,3,55,46,98,4,7,6,34}; int num; int found=0; cout << "請輸入要搜尋的值:"; cin >> num; for(int i = 0; i < 10; i++){ if(data[i] == num) { cout << "在註標" << i << "的位置找到"<<num; found=1; } } if(found==0)cout << num << "不在陣列中"; } void p05(){ int a[7] = {2,16,34,47,53,67,81}; int n,m,l=0,r=6; int found=0; cout << "請輸入要搜尋的值:"; cin >> n; while(l<=r){ m=(l+r)/2; if(a[m] == n) { found=1; cout << "在註標" << m << "的位置找到"<<n; return; }else if(a[m] > n) { r=m-1; }else if(a[m] < n) { l=m+1; } } if(found==0)cout << n << "不在陣列中"; } ``` ## 心得感想(100字): 經過這麼多次的練習我覺得我的程式能力有慢慢進步了比起一開始什麼都不會到現在已經可以打出一些簡單的程式也可以把他們統整成一個專案了不過還是有很多地方不太懂希望我可以漸漸變強 # 12/18隨堂練習 ## Ch07-08 輸出字串與字元陣列大小(P.245) ## 程式碼: ``` //資訊一乙 31號 陳奕鋒 #include<iostream> using namespace std; int main() { char name1[]="big apple"; char name2[]={'b','i','g',' ','a','p','p','l','e'}; cout <<"name1[]大小為:"<<sizeof(name1)<<endl; cout <<"name2[]大小為:"<<sizeof(name2)<<endl; cout<<"name1[]:"<<name1<<endl; cout<<"name2[]:"<<name2<<endl; } ``` ![image](https://hackmd.io/_uploads/rk-wpUa8p.png) ## 執行結果: ![image](https://hackmd.io/_uploads/Bkg2kwa8a.png) ## Ch07-27 以指標實作氣泡排序法(P.277) ## 程式碼: ``` //資訊一乙 31號 陳奕鋒 #include<iostream> using namespace std; int main() { int n; cout <<"請輸入欲排序的數字數量"; cin>>n; int *ptr=new int[n]; for(int i=0;i<n;i++){ cout<<"請輸入第"<<i+1<<"個數字"; cin>>*(ptr+i); } for(int i=1;i<n;i++) for(int j=0;j<n-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<n;i++) cout<<*(ptr+i)<<'\t'; delete[]ptr; } ``` ![image](https://hackmd.io/_uploads/r1hDoD68T.png) ## 執行結果: ![image](https://hackmd.io/_uploads/rye8jP68T.png) # CH7課後練習(2023/12/18) ## 1.大樂透539是一種樂透型遊戲:(參考P.275) 您必須從01~39中任選6個號碼進行投注。開獎時,開獎單位將隨機開出六個號碼,這一組號碼就是該期大樂透的中獎號碼,也稱為「獎號」。您的六個選號中,如果有三個以上(含三個號碼)對中當期開出之六個號碼,即為中獎,並可依規定兌領獎金。 ## 程式碼: ``` //資訊一乙 31號 陳奕鋒 #include<iostream> #include<stdlib.h> #include<time.h> using namespace std; int main(){ cout<<"本期今彩539的號碼是(亂數):"; srand(time(0)); int l,pp,i=0; int p[6]; while(i<6){ l=(rand()%39)+1; p[i]=l; for(int j=0;j<=i-1;j++){ if(p[j]==l) i=i-1; } i++; } for(int kk=0;kk<6;kk++){ cout<<p[kk]<<"\t"; } cout<<"\n"; int a,b,c,d,e,f; cin>>a; cin>>b; cin>>c; cin>>d; cin>>e; cin>>f; for(int i=0;i<6;i++) if(p[i]==a||p[i]==b||p[i]==c||p[i]==d||p[i]==e||p[i]==f) pp=pp+1; if(pp==6) cout<<"頭獎"; if(pp==5) cout<<"三獎"; if(pp==4) cout<<"五獎"; if(pp==3) cout<<"普獎"; if(pp<3) cout<<"無獎"; cout<<pp; } ``` ![image](https://hackmd.io/_uploads/Syea85UwT.png) ## 執行結果: ![image](https://hackmd.io/_uploads/HyvywqIvT.png) ## 2.奇數或偶數:使用亂數函數 rand() 產生 10 個整數,分別對奇數的數加總以及偶數的數加總。 ## 程式碼: ``` //資訊一乙 31號 陳奕鋒 #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/HJbMA9IPp.png) ## 執行結果: ![image](https://hackmd.io/_uploads/HJ1IRq8va.png) ## 3.在「字元陣列」中放入10個英文字母「television」,然後將字母依小到大(a到z)順序從螢幕輸出。(參考P.277)(注意:字母須由小排到大!!) ## 程式碼: ``` //資訊一乙 31號 陳奕鋒 #include <iostream> using namespace std; int main(){ int x=10; char A[x]; for(int i=0;i<x;i++) { cout<<"請輸入A["<<i+1<<"]的字元:"; cin>>A[i]; for(int j=0;j<i;j++) { if(A[j]<A[i]){ int temp=A[j];A[j]=A[i];A[i]=temp; } } } cout<<"由小排到大:"; for(int i=0;i<x;i++){ cout<<A[i]; } } ``` ![image](https://hackmd.io/_uploads/SJ_-ysUvp.png) ## 執行結果: ![image](https://hackmd.io/_uploads/ry_y1sIwp.png) ## CH7課後練習(2023/12/25) ### 1.請使用二維陣列存取學生的多科成績,並輸出班級成績單。 ## 程式碼: ``` //資訊一乙 31號 陳奕鋒 #include<iostream> #include<time.h> #include<stdlib.h> #include<algorithm> using namespace std; int main(){ cout<<"輸入幾位學生"; int n; cin>>n; int a[n][5]; cout<<"輸入每位學生的成績"<<endl; for(int i = 0;i<n;i++){ int num; cout<<i+1<<" 號學生:"; for(int j = 0;j<5;j++){ cin>>a[i][j];} } cout<<"班級成績單"<<endl; cout<<"座號 國文 英文 數學 基電、物理 總分"<<endl; cout<<"=============================="<<endl; for(int i = 0;i<n;i++){ int score = 0; cout<<i+1; for(int j = 0;j<5;j++){ cout<<" "<<a[i][j]; score+=a[i][j]; } cout<<" "<<score; cout<<endl; } } ``` ![image](https://hackmd.io/_uploads/r1TKzj8va.png) ## 執行結果: ![image](https://hackmd.io/_uploads/HJASziLv6.png) ## 2.在圖一的表格中,橫排稱為列(row),直排稱為行(column),以Xij來表示表格X中的第i列第j行的元素。如圖一中的X00=1、X12=6。(參考P.252) 請設計一個程式可執行以下工作: (1) 根據使用者輸入的數值設定表格大小(先輸入列,再輸入行) (2) 由使用者輸入數值填滿表格 (3) 輸入完成後,將整個表格印出 ## 程式碼: ``` //資訊一乙 31號 陳奕鋒 #include <iostream> using namespace std; int main(){ cout<<"根據使用者輸入設定表格大小"; int row,col; cin>>row>>col; int iArray[row][col]; cout<<"請輸入數值填滿表格\n"; for(int i=0;i<row;i++){ for(int j=0;j<col;j++) { cin>>iArray[i][j]; } } for(int i=0;i<row;i++){ for(int j=0;j<col;j++) { cout<<"iArray["<<i<<"]["<<j<<"]="<<iArray[i][j]<<endl; } } } ``` ![image](https://hackmd.io/_uploads/SkqcniLw6.png) ## 執行結果: ![image](https://hackmd.io/_uploads/ryX_3oIDp.png) ## 3.請設計一程式可執行以下工作: (1) 輸入5個值並存入一維陣列a中 (2) 計算陣列a中5個值的總和,並印出 輸入 ## 程式碼: ``` //資訊一乙 31號 陳奕鋒 #include <iostream> using namespace std; int main(){ int a[5],r; for(int i=0;i<5;i++){ cout<<"數值"<<i+1<<":"; cin>>a[i]; r+=a[i]; } cout<<"總和:"<<r; } ``` ![image](https://hackmd.io/_uploads/rJCCPjUvp.png) ## 執行結果: ![image](https://hackmd.io/_uploads/HJ8MdjIPp.png) ## 4.請設計一程式可執行以下工作:(P.263)(P.267) (1) 宣告一個指標p,並使用new運算子動態配置一塊記憶體 (2) 輸入5個值,放到指標p所控制的5個連續記憶體位址 (3) 印出指標變數p (4) 找出5個值中的最大值,印出最大值及其所在的記憶體位址 輸入 ## 程式碼: ``` //資訊一乙 31號 陳奕鋒 #include<iostream> #include<algorithm> using namespace std; int main(){ cout<<"請輸入五個數值:"; int *p = new int[5]; for(int i = 0;i<5;i++){ cin>>p[i]; } cout<<"指標變數p所指向的記憶體起點:"<<p<<endl; sort(p,p+5); cout<<"最大值"<<p[4]<<endl;; int *a = new int(p[4]); cout<<"最大值記憶體位址:"<<a<<endl; delete[] p; } ``` ![image](https://hackmd.io/_uploads/ryvLajUPT.png) ## 執行結果: ![image](https://hackmd.io/_uploads/ryuraiIwa.png) ## 心得感想(100字)(12/25):