# h120230918 課堂作業 ## Ch3-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/BkLsU4SJa.png) 執行結果 ![](https://hackmd.io/_uploads/BkxCIVB1a.png) ## Ch3-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/SkCOZrHy6.png) 執行結果 ![](https://hackmd.io/_uploads/B1G3XHS1a.png) ## Ch3-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 / JP << "日幣"; } ![](https://hackmd.io/_uploads/SkNiOHrJ6.png) 執行結果 ![](https://hackmd.io/_uploads/Sy7cUHSkT.png) ## Ch3-20交換兩變數的值 #include<iostream> using namespace std; int main() { int a=10,b=20; int temp; cout<<"交換前a=" << a << "\t b=" << b << endl; temp=a; a=b; b=temp; cout<<"交換後 a=" << a <<"\t b=" << b << endl; } ![](https://hackmd.io/_uploads/B1EytSByp.png) 執行結果 ![](https://hackmd.io/_uploads/rkQfFHHka.png) ## Ch3-21大小寫轉換 #include<iostream> using namespace std; int main() { char upper='S',lower; lower=upper+32; cout<< upper <<"的小寫是"<< lower << endl; } ![](https://hackmd.io/_uploads/S1Y8crHy6.png) 執行結果 ![](https://hackmd.io/_uploads/HkbF9rHyT.png) ## 心得感想 在經過這次實習後我覺得我對寫程式的熟悉度又更高了,不再像第一次寫程式時那麼的不知所措也比較能聽得懂課程的內容,而且自己也越來越願意多花一點心思投入在其中,試著靠自己的力量去完成一個又一個練習,我希望未來的我可以越做越好。 ## 實作練習 ## p74_1 #include<iostream> using namespace std; int main() { #define SIZE 10; cout<<SIZE; } ![](https://hackmd.io/_uploads/BkSVhHrJa.png) 執行結果 ![](https://hackmd.io/_uploads/S1KH2SrJa.png) ## p74_2 #include<iostream> using namespace std; int main() { int a=10; float b=101.7; char c='c'; cout <<"a="<<a<<"\tb="<<b<<"\tc="<<c; } ![](https://hackmd.io/_uploads/SJRd5ToJa.png) 執行結果 ![](https://hackmd.io/_uploads/HJqf96ika.png) ## p74_5 #include<iostream> using namespace std; int main () { cout<<"我正在學習\"C++\"程式語言"; } ![](https://hackmd.io/_uploads/rJBLCbwy6.png) 執行結果 ![](https://hackmd.io/_uploads/r1lF0bwJp.png) ## p74_7 #include<iostream> using namespace std; int main() { int a=10,b=20,c=30; int www; cout << "交換前\ta=" <<a<< "\,\tb=" <<b<< "\,\tc="<<c<<"\n"; www=a,a=c,c=b,b=www; cout << "交換後\ta=" <<a<< "\,\tb=" <<b<< "\,\tc="<<c<<"\n"; } ![](https://hackmd.io/_uploads/Syz-wMvy6.png) 執行結果 ![](https://hackmd.io/_uploads/HJHh7GPya.png) ## 其他題目 ## p74_3 int main () { const int a = 10 , b = 20; int c = a*b; cout << "矩形的面積為"<< c; } ![](https://hackmd.io/_uploads/BJqpgRsJa.png) 執行結果 ![](https://hackmd.io/_uploads/HknkZCiya.png) ## p74_6 #include<iostream> using namespace std; int main() { int ww ; cout <<"請輸入你想要的數值:"; cin >> ww; const int r=2*ww; cout << r; } ![](https://hackmd.io/_uploads/Syv_XAsJa.png) 執行結果 ![](https://hackmd.io/_uploads/HJ1jQ0sk6.png) # 20230925 課堂作業(CH4運算子與運算式) ## Ch04-05 前置與後置遞增運算子 程式碼: #include<iostream> using namespace std; int main() { int i = 100, j; j =(i++) +5; cout << "i=" << i <<" \t\tj = " << j; i = 100; j =(++i) + 5; cout << "\ni=" << i << "\t\tj=" << j; } 程式碼實作畫面: ![](https://hackmd.io/_uploads/Bk4aDDCJp.png) 執行結果:![](https://hackmd.io/_uploads/rJYADDCyT.png) ## Ch04-06 關係運算子練習 程式碼: #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 << "(i-- < j) :" << (i-- < j) <<"\n"; cout << "(i != j) :" << (i != j) <<"\n"; } 程式碼實作畫面:![](https://hackmd.io/_uploads/HyoNcDAJ6.png) 執行結果: ![](https://hackmd.io/_uploads/BJP_qDAJ6.png) ## Ch04-07 邏輯運算子練習 程式碼: #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/H1gaq3DCy6.png) 執行結果: ![](https://hackmd.io/_uploads/rJm02PRka.png) ## 0923實習心得(至少100字) 在這次時實習課中,我覺得課程開始漸漸有了難度了,因為開始出現許多符號但這些符號卻與我原本所認知的意思不太一樣,所以我應該要跟勤勞的去練習如何運用這些符號,將他們運用在我的程式中,希望之後再看到這些符號時都可以快且正確地將其辨別出來。 ## 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' <<"\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/rkq-QtRJT.png) 執行結果: ![](https://hackmd.io/_uploads/SJF77K0kT.png) ## 位元運算子練習_運算過程(請拍照上傳手寫計算過程): ![](https://hackmd.io/_uploads/rJTHO3Iga.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/B1phxF01p.png) 執行結果: ![](https://hackmd.io/_uploads/Bky0eYCy6.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/rk2wXt0y6.png) 執行結果: ![](https://hackmd.io/_uploads/HkJFXYAya.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/Bk5IBK0y6.png) 執行結果: ![](https://hackmd.io/_uploads/r1VdHY0kp.png) # 實作練習 執行結果: ## 攝氏溫度轉華氏溫度 程式碼: #include<iostream> using namespace std; int main() { float C, F; cout << "請輸入攝氏溫度:"; cin >> F; C = (F+32) *9/5; cout << "換成華氏溫度" << C <<" 度"; } 程式碼實作畫面: ![](https://hackmd.io/_uploads/SJyX8FAy6.png) 執行結果: ![](https://hackmd.io/_uploads/ryR7LKCkp.png) ## 自動販售機(幣值轉換練習) 程式碼: #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/SJqEPtAy6.png) 執行結果: ![](https://hackmd.io/_uploads/ByNwwYRyT.png) ## 0925實習心得(至少100字) 在這次的實習完後我感到很開心,因為覺得自己寫出來的東西好像越來越貼近生活了,而不是說只侷限於考試課本,而是在生活中真正會用到的,希望我以後可以寫出許多實用且方便的程式,讓大家都可以以最簡單快速的方式去使用它。 # 20231016 課堂作業(CH5流程控制) # 本章應用實例 (HW) ## 05-24電影分級制度 下列程式將依據年齡層來判斷可觀賞的影片等級(0~5歲:普遍級、6~11歲:普 遍級及保護級、12~17歲:限制級以外的影片、18歲(含)以上:各級影片)。 程式碼: #include<iostream> using namespace std; int main() { int a; cout << "請輸入年齡 (0~120) :"; cin >> a; switch(a){ case 0 ... 5: cout << "可難普遍級"; break; case 6 ... 11: cout << "可看普遍級及保護及"; break; case 12 ... 17: cout << "可看限制級以外的影片"; break; case 18 ... 120: cout << "可看各級影片"; break; default: cout << "您並未輸入正確的年齡" ; } } 程式碼實作畫面: ![](https://hackmd.io/_uploads/Hyy5qc0-a.png) 執行結果: ![](https://hackmd.io/_uploads/Bycjq5CZT.png) ## 05-27 猜數字遊戲 下列程式為一個猜數字遊戲(假設答案為7),遊戲會發出提示訊息要使用者輸入 所要猜的數字(1~10),若猜錯,遊戲會繼續進行,直到猜對為止。 若使用者未猜到答案時,要給予使用者提示(再大一點或再小一點),直到使用者猜對為止,該如何改寫程式呢? (提示:在while迴圈中加入if條件判斷敘述。) 程式碼: #include<iostream> using namespace std; int main() { int n, ans = 7; while(n != ans){ cout << "猜猜看數字是多少?(1~10):\n"; cin >> n; if (n>7) cout << "再小一點\n"; else cout << "再大一點\n"; } cout << "泥蠔蚌"; } 程式碼實作畫面: ![](https://hackmd.io/_uploads/HkupVvQGp.png) 執行結果: ![](https://hackmd.io/_uploads/B1sLrP7Mp.png) ## 05-28 2的n次方 程式碼: #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; } 程式碼實作畫面: ![](https://hackmd.io/_uploads/Hy-81oA-6.png) 執行結果: ![](https://hackmd.io/_uploads/SJKD1iCW6.png) ## 05-29 成績加總 程式碼: #include<iostream> using namespace std; int main() { int s = 0, n = 0; do{ s = s + n; cout << "請輸入成績:"; cin >> n; }while(n != -1); cout << "總分為:"<< s; } 程式碼實作畫面: ![](https://hackmd.io/_uploads/Hk17Mi0bp.png) 執行結果: ![](https://hackmd.io/_uploads/BkNVGiC-p.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/ByjVVsCb6.png) 執行結果: ![](https://hackmd.io/_uploads/BJ-UNjAWa.png) # 課後練習 ## 1.心理測驗 (if…else if 應用) 程式碼: #include<iostream> using namespace std; int main() { int w; cout << "發生船難,你選擇留下什麼?\n"; cout << "選擇(1)留下老人和孕婦\n"; cout << "選擇(2)留下老人和嬰兒\n"; cout << "選擇(3)留下老人和金銀珠寶\n"; cout << "選擇(4)留下孕婦和嬰兒\n"; cout << "選擇(5)留下孕婦和金銀珠寶\n"; cout << "選擇(6)留下嬰兒和金銀珠寶\n"; cin >> w; if (w==1) cout << "你的情感很細膩,善於觀察細節。在感情中,相比於戀人的甜言蜜語,你更在乎對方實際的行動。"; else if (w==2){ cout << "在感情中,你不僅洞察力超好,第六感也很強,越是親近的人這種直覺越敏銳,所以另一半對你很\n"; cout << "難有所隱藏,因為你可以憑借著蛛絲馬跡得到你想要知道的消息。"; } else if (w==3) cout << "你是個典型的顏控,在擇偶時很注重另一半的外表和身高"; else if (w==4){ cout << "面對感情你很承盾,一方面你很感性,渴望浪漫熱烈的愛情;另一方面你又很理性,明白現實的殘\n"; cout << "酷和金錢的重要性"; } else if (w==5){ cout << "在感情方面你很挑剔,所以很難遇到心動的對象。不過在戀愛時你卻很專一,一旦喜歡上某個人,\n"; cout << "你就會全心全意的對他好,所以和你談戀愛是一件很幸福的事。" ; } else if (w==6) cout << "在感情中你很缺乏安全感,有時候會忍不住通過試探或考驗的方式去確認對方是否愛你。"; else cout << "亂輸入,活該一輩子舔狗命!!!"; } 程式碼實作畫面: ![](https://hackmd.io/_uploads/SyGYDQq-a.png) 執行結果: ![](https://hackmd.io/_uploads/Hyj9DXc-p.png) ## 2.判斷座標(x,y)在第幾象限。 (巢狀if…else 應用) 程式碼: #include<iostream> using namespace std; int main() { int x; int y; cout << "請輸入x值(不可輸入0):\n"; cin >> x; cout << "請輸入y值(不可輸入0):\n"; cin >> y; if ((x>0)&&(y>0)) cout << "(" << x << "," << y <<" ) 在第一象限"; else if ((x>0)&&(y<0)) cout << "(" << x << "," << y << ") 在第二象限"; else if ((x<0)&&(y<0)) cout << "(" << x << "," << y << ") 在第三象限"; else cout << "(" << x << "," << y << ") 在第四象限"; } 程式碼實作畫面: ![](https://hackmd.io/_uploads/rJdnvXq-p.png) 執行結果: ![](https://hackmd.io/_uploads/r1Bl_X9Wp.png) ## 3.用*組成三角形(for迴圈的應用) 程式碼: #include<iostream> using namespace std; int main() { int s,w; cout << "請輸入層數:"; cin >> s; for(int i=1;i<=s; i++){ for(int h=1;h<=i; h++){ cout << "*"; } cout << "\n"; } cout << "\n"; for(int i=1;i<=s; i++){ for(int h=1;h<=s-i+1; h++){ cout << "*"; } cout << "\n"; } cout << "\n"; for(int i=1;i<=s; i++){ for(int w=1;w<=s-i; w++ ){ cout << " "; } for(int h=1;h<=i; h++ ){ cout << "*"; } cout << "\n"; } cout << "\n"; for(int i=1;i<=s; i++){ for(int w=1;w<=i-1; w++ ){ cout << " "; } for(int h=1;h<=s-i+1; h++ ){ cout << "*"; } cout << "\n"; } } 程式碼實作畫面: ![](https://hackmd.io/_uploads/SkTrZqAZ6.png) 執行結果: ![](https://hackmd.io/_uploads/H1hFWqRZT.png) ## 4.十二生肖查詢(switch…case的應用) 程式碼: #include<iostream> using namespace std; int main() { int w,b; cout << "請輸入民國(年):\n"; cin >> w; b = w%12; switch (b) { 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/SJYijQcW6.png) 執行結果: ![](https://hackmd.io/_uploads/r116jm9ZT.png) ## 5.閏年 程式碼: #include<iostream> using namespace std; int main() { int w; cout << "請輸入一個西元年:\n"; cin >> w; if ((w%4)==0){ if ((w%400)==0) cout << w <<"年是閏年"; else{ if ((w%100)==0) cout << w << "年非閏年"; else cout << w << "年是閏年"; } } else cout << w <<"年非閏年"; } 程式碼實作畫面: ![](https://hackmd.io/_uploads/SyNrL4cW6.png) 執行結果: ![](https://hackmd.io/_uploads/HyBPUV9ZT.png) ## 6.重複計算BMI(while) 程式碼: #include<iostream> using namespace std; int main() { char ga='m'; float h,w; while (ga == 'm' || ga == 'M'){ cout << "請輸入身高(公分)"; cin >> h; cout << "請輸入體重(公斤)"; cin >> w; cout << "您的BMI為" << w / (h*h)*10000 << "\n"; cout << "要繼續計算另一位嗎?" << "(要請輸入 M 或 m , 若不要請輸入非 M 或 m 的字元):"; cin >> ga; } } 程式碼實作畫面: ![](https://hackmd.io/_uploads/SJTIUq0ZT.png) 執行結果: ![](https://hackmd.io/_uploads/r13uL50ba.png) ## 7.計算兩整數的最大公因數(while迴圈)(P.131) 程式碼: #include<iostream> using namespace std; int main() { int num1,num2,temp; cout << "計算兩正數的最大公因數\n"; cout << "請輸入第一個數字\n"; cin >> num1; cout << "請輸入第二個數字"; cin >> num2; while (num2 != 0){ temp = num1 % num2; num1 = num2; num2 = temp; } cout << "最大公因數是:" << num1; } 程式碼實作畫面: ![](https://hackmd.io/_uploads/ByWmb8mf6.png) 執行結果: ![](https://hackmd.io/_uploads/B1JHbU7zT.png) ## 8. 密碼檢驗程式(P.133) 請設計一個密碼輸入次數不可大於三次的密碼檢驗程式,密碼=123,當密碼輸入正確請顯示:歡迎進入本系統。密碼輸入錯誤3次,無法登入。 程式碼: #include<iostream> using namespace std; int main(){ int ww; for (int i=1;i<=3;i++){ cout << "第"<< i <<"次輸入密碼"; cin >> ww; if (ww == 413) break; } if (ww == 413) cout << "歡迎進入本系統"; else cout << "密碼輸入錯誤3次,無法登入"; } 程式碼實作畫面: ![](https://hackmd.io/_uploads/ByTWgwQM6.png) 執行結果: ![](https://hackmd.io/_uploads/SkW4xDmza.png) ## 心得感想(至少100字): 我覺得for跟while迴圈練習完還是有點模糊,所以我應該花更多的時間去練習,畢竟老師之前也有在課堂上講過,這個程式很重要,想當然我就不能擺爛鬼混過去,希望在之後的練習中,我可以慢慢地去理解,並完善的利用好這個程式。 # 20231106 課堂作業(CH6函式) ## 隨堂練習 ### Ch06-21 以時間亂數種子產生5個500~1000的亂數 程式碼: #include<iostream> #include<stdlib.h> #include<time.h> using namespace std; int main() { srand(time(0)); for (int i = 0; i < 5; i++) cout << (rand()%501)+500 <<"\n"; } 程式碼實作畫面: ![image.png](https://hackmd.io/_uploads/B14zx18mp.png) 執行結果: ![image.png](https://hackmd.io/_uploads/rycXgJI7T.png) ## 應用實例練習 ### Ch06-25 猜數字遊戲 程式碼: #include<iostream> #include<time.h> #include<stdlib.h> using namespace std; int number(){ int num = (rand()%10)+1; return num; } int main() { srand (time(0)); int n,a; a = number(); while (n != a){ cout << "猜猜看數字是多少?(1~10):"; cin >> n; if(n>a) cout << "猜錯了<再小一點\n"; else if(n>a) cout << "猜錯了<再大一點\n"; } cout << "猜對了!ya!"; } 程式碼實作畫面: ![image.png](https://hackmd.io/_uploads/HkrJb18m6.png) 執行結果: ![image.png](https://hackmd.io/_uploads/BJC4ZJIQp.png) ### Ch06-26 猜拳遊戲 程式碼: #include<iostream> #include<time.h> #include<stdlib.h> using namespace std; void game(int user){ int com = (rand()%3)+1; if (user == com){ cout << "平手"; }else if (user == 1){ if(com == 2) cout << "你出剪刀,電腦出石頭,你輸了"; else cout << "你出剪刀,電腦出布,你贏了"; }else if (user == 2){ if(com == 3) cout << "你出石頭,電腦出布,你輸了"; else cout << "你出石頭,電腦出剪刀,你贏了"; }else if (user == 3){ if(com == 1) cout << "你出布,電腦出剪刀,你輸了"; else cout << "你出布,電腦出石頭,你贏了"; } } int main() { int user; srand(time(0)); cout << "請輸入你要出的拳(1.剪刀2.石頭3.布)"; cin >> user; game(user); } 程式碼實作畫面: ![image.png](https://hackmd.io/_uploads/H1wDbk87p.png) 執行結果: ![image.png](https://hackmd.io/_uploads/BkauZyIm6.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" <<"輸入1~6其中一個數字,猜骰子點數(或輸入0以結束遊戲)"; cin >> num1; } } int dice() { int a; a = (rand()%6)+1; return a; } 程式碼實作畫面: ![image.png](https://hackmd.io/_uploads/SkH3Z1L76.png) 執行結果: ![image.png](https://hackmd.io/_uploads/r1_p-kIma.png) ## 心得感想(100字)(11/13): 在這次課堂中我學到了函式,怎麼說呢?雖對函式的使用並沒有到非常熟悉,但我知道我必須得把它學好,因為之後再寫比較複雜的程式時一定得用到函式,所以我應該要多多利用函式去寫程式,慢慢拉近我與它之間的距離,日後要用到時,對它才不會太陌生。 ### Ch06-24 查詢語音費率函式 程式碼: #include<iostream> using namespace std; void bill(int a){ switch(a){ case 196: cout << "語音費率每秒:0.0869元"; break; case 396: cout << "語音費率每秒:0.0782元"; break; case 796: cout << "語音費率每秒:0.0696元"; break; default: cout << "無此費率,請重新查詢!"; } } int main(){ int a; cout << "請輸入要查詢的月租費方案(196.396.796):"; cin >> a; bill(a); } 程式碼實作畫面: ![image](https://hackmd.io/_uploads/SkJ0VBuE6.png) 執行結果: ![image](https://hackmd.io/_uploads/Sk71Br_E6.png) ### Ch06-27 階乘函式 程式碼: #include<iostream> using namespace std; double factorial(int); int main(){ while(true){ cout << "請輸入1-170間的整數(輸入0即結束程式)"; int num = 0; cin >> num; if (num == 0) break; cout << num <<"!等於" << factorial(num) <<"\n"; } cout << "\n謝謝您使用階乘計算機"; } double factorial(int n){ double fact; for(fact=1;n>0;n--) fact = fact*n; return fact; } 程式碼實作畫面: ![image](https://hackmd.io/_uploads/BkTpDBd4p.png) 執行結果: ![image](https://hackmd.io/_uploads/r1yyOB_4p.png) ## 課後練習 ### 1.組成三角形 程式碼: #include<iostream> using namespace std; int a,b,c; void pot(int a,int b,int c){ if(((a+b>c)&&(a+c>b))&&(b+c>a)) cout << "這個是三角形"; else cout << "這個不是三角形"; } int main(){ cout << "請輸入三角形的三邊\n"; cin >>a; cin >>b; cin >>c; pot(a,b,c); } 程式碼實作畫面: ![image](https://hackmd.io/_uploads/SkttQ8OEp.png) 執行結果: ![image](https://hackmd.io/_uploads/rkYvZUOVT.png) ### 2.遞迴 程式碼: #include<iostream> using namespace std; long double bbq(int n){ if(n==1) return 1; else return(n*bbq(n-1)); } main(){ int w; while(true){ cout << "請輸入1-170間的整數(輸入0即結束程式):"; cin >> w; if (w==0) break; cout << w << "! =" << bbq(w)<<"\n"; } } 程式碼實作畫面: ![image](https://hackmd.io/_uploads/rya-vI_V6.png) 執行結果: ![image](https://hackmd.io/_uploads/S1pfDLuEp.png) ### 3.專案練習 程式碼: 程式碼實作畫面: 執行結果: ## 心得感想(100字)(11/20): # 20231204 課堂作業(CH7陣列與指標) ## 隨堂練習 ### Ch07-03 請使用者輸入8個數字,找出最大值 ### 程式碼: #include<iostream> #define 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/HkG2UmT8a.png) 執行結果: ![image](https://hackmd.io/_uploads/rylfPXpLp.png) ### Ch07-04 氣泡排序法 S:83 h:104 i:105 n:110 n:110 y:121 ### 程式碼: #include<iostream> #define SIZE 6 using namespace std; int main() { char ww[SIZE]={'S','h','i','n','n','y'}; cout << "排序前:"; for (int i = 0;i < SIZE; i++) cout << ww[i]; for (int i = 1;i < SIZE; i++) for (int j = 0;j < SIZE - j; j++) if (ww[j] < ww[j+1]) { char tt = ww[j]; ww[j] = ww[j+1]; ww[j+1] = tt; } cout << "\n" << "排序後:"; for (int i = 0;i < SIZE; i++) cout << ww[i]; } 程式碼實作畫面: ![image](https://hackmd.io/_uploads/Hykdnm6La.png) 執行結果: ![image](https://hackmd.io/_uploads/Sye53QpLp.png) ### Ch07-05 選擇排序法 ### 程式碼: #include<iostream> #define SIZE 5 using namespace std; int main() { int a[SIZE] = {25,12,47,18,10}; int s, tt; 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; tt = a[i]; a[i] = a[s]; a[s] = tt; } cout << "\n" << "排序後:"; for(int i = 0;i < SIZE; i++) cout << a[i] << " "; } 程式碼實作畫面: ![image](https://hackmd.io/_uploads/SyE8eVpUa.png) 執行結果: ![image](https://hackmd.io/_uploads/Sk_DxEaUT.png) ### Ch07-06 循序搜尋法 ### 程式碼: #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/HkhIv46UT.png) 執行結果: ![image](https://hackmd.io/_uploads/HyEOvVTIa.png) ### Ch07-07 二元搜尋法 ### 程式碼: #include<iostream> using namespace std; int main() { int a[7] = {2,16,34,47,53,67,81}; int tt,mm,l = 0,r = 6; cout << "請輸入要搜尋的值:"; cin >>tt; while(l <= r){ mm = (l+r)/2; if(a[mm] == tt){ cout << "在標註" << mm << "的位置找到" << tt; return 0; } else if(a[mm] > tt){ r = mm-1; } else if(a[mm] < tt){ l = mm+1; } } cout << tt << "不在陣列中"; } 程式碼實作畫面: ![image](https://hackmd.io/_uploads/Hkz3KV68p.png) 執行結果: ![image](https://hackmd.io/_uploads/S1xRtVpU6.png) ### 1211專案練習(學生練習) ### 程式碼: 程式碼實作畫面: 執行結果: ## 心得感想(100字)(12/11): ## CH7課後練習(2023/12/18) ### Ch07-08 輸出字串與字元陣列大小(P.245) 程式碼: #include<iostream> using namespace std; int main() { char name1[]="john smith"; char name2[]={'J','O','H','N',' ','S','M','I','T','H'}; cout << "name1[]大小為" << sizeof(name1) << "\n"; cout << "name2[]大小為" << sizeof(name2) << "\n"; cout << "name1[]:" << name1 << "\n"; cout << "name2[]:" << name2 << "\n"; } 程式碼實作畫面(需有班級座號姓名浮水印): ![image](https://hackmd.io/_uploads/BkB_HE6IT.png) 執行結果: ![image](https://hackmd.io/_uploads/Sy2FHV6U6.png) ### Ch07-27 以指標實作氣泡排序法(P.277) 程式碼: #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 - 1; j++) if (*(ptr+j) < *(ptr+j+1)){ char tt = *(ptr+j); *(ptr+j) = *(ptr+j+1); *(ptr+j+1) = tt; } cout << "排序後:"; for (int i = 0;i < num; i++) cout << *(ptr+i) << "\t"; delete[]ptr; } 程式碼實作畫面(需有班級座號姓名浮水印): ![image](https://hackmd.io/_uploads/BJ9hSVaLT.png) 執行結果: ![image](https://hackmd.io/_uploads/SJLAHV6U6.png) ### 1.大樂透539是一種樂透型遊戲: 程式碼: #include<iostream> #include<time.h> #include<stdlib.h> using namespace std; int main(){ cout << "本期的中獎號碼是:\n"; srand(time(0)); int num,i = 0; int n[6]; while(i < 6){ num = (rand()%39) + 1; n[i] = num; for (int j = 0;j <=i - 1; j++){ if (n[j] == num) i = i -1; } i++; } for (int i = 0; i < 6; i++) cout << n[i] << " "; int c = 0; int y[6]; for (int h = 0; h < 6; h++){ cout << "請輸入第" << h+1 <<"個數字:"; cin >> y[h]; for (int f = 0; f < 6; f++) if(y[h] == n[f]) c = c + 1; } if(c == 3) cout << "對中當期獎號之任三碼 普獎"; else if(c == 4) cout << "對中當期獎號之任四碼 伍獎"; else if(c == 5) cout << "對中當期獎號之任五碼 參獎"; else if(c == 6) cout << "與當期六個獎號完全相同者 頭獎"; } 程式碼實作畫面(需有班級座號姓名浮水印): ![image](https://hackmd.io/_uploads/BkaNSr6IT.png) 執行結果: ![image](https://hackmd.io/_uploads/rJkzLr6I6.png) ### 2.奇數或偶數:使用亂數函數 rand() 產生 10 整數,分別對奇數的數加總以及偶數的數加總。 程式碼: #include<iostream> #include<stdlib.h> #include<time.h> using namespace std; int main() { int a[5]; int b = 0; int c = 0; srand(time(0)); cout << "亂數為:"; for(int i = 0;i < 5;i++){ a[i] = (rand()%20)+1; cout << a[i] <<","; if (a[i] % 2 == 0) b = b + a[i]; else c = c + a[i]; } cout << "\n奇數加總為:" << c; cout << "\n偶數加總為:" << b; } 程式碼實作畫面(需有班級座號姓名浮水印): ![image](https://hackmd.io/_uploads/SJe4AG7vp.png) 執行結果: ![image](https://hackmd.io/_uploads/SkdH0fQPa.png) ### 3.在「字元陣列」中放入10個英文字母「television」,然後將字母依小到大(a到z)順序從螢幕輸出。(參考P.277)(注意:字母須由小排到大!!) 程式碼: #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[i] > a[j]){ int ww = a[i]; a[i] = a[j]; a[j] = ww; } } } cout << "由小排到大:"; for (int i = 0; i < x; i++){ cout << a[i]; } } 程式碼實作畫面(需有班級座號姓名浮水印): ![image](https://hackmd.io/_uploads/Sy2sx_Iva.png) 執行結果: ![image](https://hackmd.io/_uploads/HJ06xOLPT.png) ### 4.請使用二維陣列存取學生的多科成績,並輸出班級成績單 程式碼: #include<iostream> using namespace std; int main() { int a; int b; cout << "輸入幾位學生:"; cin >> a; int ww[a][5]; cout << "輸入每位學生的成績(依序為國、英、數、基電、物理,以空格區隔)\n"; for (int i = 0; i < a; i++){ cout << i+1 << "號學生:\n"; for (int j = 0; j < 5; j++){ cin >> ww[i][j]; } } cout << "班級成績單\n"; cout << "座號 國文 英文 數學 基電 物理 總分\n"; cout << "========================================================== \n"; for (int i = 0; i < a; i++){ cout << i+1; for (int j = 0; j < 5; j++){ cout << "\t" << ww[i][j]; b = b + ww[i][j]; } cout << "\t" << b <<"\n"; b = 0; } } 程式碼實作畫面(需有班級座號姓名浮水印): ![image](https://hackmd.io/_uploads/rkeuXvID6.png) 執行結果: ![image](https://hackmd.io/_uploads/HygcQwLP6.png) ### 5.在圖一的表格中,橫排稱為列(row),直排稱為行(column),以Xij來表示表格X中的第i列第j行的元素。如圖一中的X00=1、X12=6。 程式碼: #include<iostream> using namespace std; int main() { int a; int b; cout << "請輸入你要的行跟列:\n"; cin >> a; cin >>b; cout << "請輸入要放入的數字\n"; int ww[a][b]; for (int i = 0; i < a; i++){ for (int j = 0; j < b; j++){ cin >> ww[i][j]; } } for (int i = 0; i < a; i++){ for (int j = 0; j < b; j++){ cout << "ww[" << i << "][" << j <<"]:" << ww[i][j] << "\n"; } } } 程式碼實作畫面(需有班級座號姓名浮水印): ![image](https://hackmd.io/_uploads/HJoXovUDp.png) 執行結果: ![image](https://hackmd.io/_uploads/SJ-BovLva.png) ### 6.請設計一程式可執行以下工作: 程式碼: #include<iostream> using namespace std; int main() { int a[5]; int b = 0; cout << "請輸入5個值:\n"; for (int i = 0; i < 5; i++){ cin >> a[i]; b = a[i] + b; } cout << "加總為:" << b; } 程式碼實作畫面(需有班級座號姓名浮水印): ![image](https://hackmd.io/_uploads/r1XmHv8Da.png) 執行結果: ![image](https://hackmd.io/_uploads/HyI4SwID6.png) ### 7.請設計一程式可執行以下工作: 程式碼: #include<iostream> #include<algorithm> using namespace std; int main() { int *p = new int[5]; cout << "請輸入5個數:\n"; for (int i = 0; i<5; i++){ cin >> p[i]; } cout << "指標變數p所指向的記憶體起點:" << p << "\n"; sort(p,p+5); cout << "最大值為" << p[4] << "\n"; int *a = new int(p[4]); cout << "最大值的記憶體位址:" << a; delete[] p; } 程式碼實作畫面(需有班級座號姓名浮水印): ![image](https://hackmd.io/_uploads/SJyOWOLva.png) 執行結果: ![image](https://hackmd.io/_uploads/Hk4KZuUPT.png) ## 心得感想(100字)(12/18):