# 程式設計實習 ## Ch03-12 各種字面常數表示法的應用 程式碼: ``` //資訊一乙 30號 陳品博 #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/SJ1tGDSya.png) 結果: ![](https://hackmd.io/_uploads/BJs7mwSJ6.png) ## Ch03-18讓使用者從鍵盤輸入資料 ``` //資訊一乙 30號 陳品博 #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/H1fpLPBya.png) 結果: ![](https://hackmd.io/_uploads/S1-ZPPBkp.png) ## CH03-19 換匯程式 ``` //資訊一乙 30號 陳品博 #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/B1NHzdry6.png) 結果: ![](https://hackmd.io/_uploads/SJuHfuHkp.png) ## Ch03-20 交換兩變數的值 ``` //資訊一乙 30號 陳品博 #include<iostream> using namespace std; int main() { int a=10,b=20; int temp; cout<<"交換前 a = "<<a<<"\t b = "<<b<<'\n'; temp=a; a=b; b=temp; cout<<"交換後 a = "<<a<<"\t b = "<<b<<'\n'; } ``` ![](https://hackmd.io/_uploads/rk-oFOrJ6.png) 結果: ![](https://hackmd.io/_uploads/HkLsF_Bya.png) ## Ch03-21 大小寫轉換 ``` //資訊一乙 30號 陳品博 #include<iostream> using namespace std; int main() { char upper = 'S', lower; lower = upper+32; cout<<upper<<" 的小寫是 "<< lower<<'\n'; } ``` ![](https://hackmd.io/_uploads/S1SaYOSkp.png) 結果: ![](https://hackmd.io/_uploads/HyX15dBy6.png) ## 心得感想 這次的實習課,我學到了巨集常數、變數等等。 p74_1 ``` //資訊一乙 30號 陳品博 #include<iostream> using namespace std; #define SIZE 10 int main() { cout<<SIZE; } ``` ![](https://hackmd.io/_uploads/ByV7hOSyp.png) 結果: ![](https://hackmd.io/_uploads/ry_72uS1T.png) p74_2 ``` //資訊一乙 30號 陳品博 #include<iostream> using namespace std; int main() { int a=10; double b=101.7; char c='c' ; cout<<a<<b<<c; } ``` ![](https://hackmd.io/_uploads/Hk2tyYBJp.png) 結果: ![](https://hackmd.io/_uploads/SyM91YryT.png) p74_5 ``` //資訊一乙 30號 陳品博 #include<iostream> using namespace std; int main() { cout<<"我正在學習\"C++\"程式"; } ``` ![](https://hackmd.io/_uploads/H17pCurkp.png) 結果: ![](https://hackmd.io/_uploads/H1ha0_B1T.png) p74_7 ``` //資訊一乙 30號 陳品博 #include<iostream> using namespace std; int main() { int a=10,b=20,c=30,x; cout<<"交換前 "<<"a = "<<a<<"b = "<<b<<"c = "<<c<<'\n'; x=c; c=b; b=a; a=x; cout<<"交換後 "<<"a = "<<a<<"b = "<<b<<"c = "<<c; } ``` ![](https://hackmd.io/_uploads/BJN_ztSkT.png) 結果: ![](https://hackmd.io/_uploads/SJewQKS1T.png) # 20230925 課堂作業(CH4運算子與運算式) ## Ch04-05 前置與後置遞增運算子 程式碼: ``` //資訊一乙 30號 陳品博 #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/Hk8u8W3ya.png) 執行結果: ![](https://hackmd.io/_uploads/S1zr8-3J6.png) ## Ch04-06 關係運算子練習 程式碼: ``` //資訊一乙 30號 陳品博 #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/r1pp4Z2k6.png) 執行結果: ![](https://hackmd.io/_uploads/ryjyBb31a.png) ## Ch04-07 邏輯運算子練習 程式碼: ``` //資訊一乙 30號 陳品博 #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/rJ_9dZ2yp.png) 執行結果: ![](https://hackmd.io/_uploads/BycDd-nJT.png) ## 實習心得 經過今天實習課,我學習到了運算子、運算式、運算元等等,其中運算子分成算術運算子、關係運算子、邏輯運算子等等,其中邏輯運算子分成!、&&、\||等等,口訣:!:反向、&&:有0則0、\||:有1則1,這一次我一開始main寫成mian下次要改進。 ## Ch04-08 位元運算子練習 程式碼: ``` //資訊一乙 30號 陳品博 #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 & j = " << (i & j) << '\n'; cout << "i | j = " << (i | j) << '\n'; cout << "i ^ j = " << (i ^ j) << '\n'; } ``` 程式碼實作畫面: ![](https://hackmd.io/_uploads/S1w2Bj0yT.png) 執行結果: ![](https://hackmd.io/_uploads/S18CHsRJ6.png) 位元運算子練習_運算過程: ![](https://hackmd.io/_uploads/ryd7tjR1p.jpg) # 應用實例練習(P.98) ## Ch04-15 模擬換幣機 程式碼: ``` //資訊一乙 30號 陳品博 #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/SkGcU3CkT.png) 執行結果: ![](https://hackmd.io/_uploads/HyOt83Aka.png) ## Ch04-16 華氏溫度轉攝氏溫度 程式碼: ``` //資訊一乙 30號 陳品博 #include <iostream> using namespace std; int main() { float C, F; cout << "請輸入華氏溫度:"; cin >> F; C = (F - 32) * 5/9; cout << "換算成攝氏溫度為 " << C << " 度"; } ``` 程式碼實作畫面: ![](https://hackmd.io/_uploads/ByPou3Ayp.png) 執行結果: ![](https://hackmd.io/_uploads/BJYqunA16.png) ## Ch04-18 成績計算程式 程式碼: ``` //資訊一乙 30號 陳品博 #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/Sklx23AJ6.png) 執行結果: ![](https://hackmd.io/_uploads/H1p133Ay6.png) ## 0925實習心得(至少100字) 經過今天實習課,我學習到了位元運算子、指定運算子、複合運算子等等,還有運算子的優先順序(先乘除後加減)、結合性,還有先算括號內的算式等等......等等諸如此類的,我相信這以後一定會用到所以深深記在我的腦海中。 # 20231016 課堂作業(CH5流程控制) ## 1.心理測驗 (if…else if 應用) ``` //資訊一乙 30號 陳品博 #include<iostream> using namespace std; int main() { int a; cout<<"課後練習 (1016完成)1.心理測驗 (if…else if 應用)發生船難,你選擇留下什麼?\n"; cout<<"選擇(1)留下老人和孕婦\n"; cout<<"選擇(2)留下老人和嬰兒\n"; cout<<"選擇(3)留下老人和金銀珠寶\n"; cout<<"選擇(4)留下孕婦和嬰兒\n"; cout<<"選擇(5)留下孕婦和金銀珠寶\n"; cout<<"選擇(6)留下嬰兒和金銀珠寶\n"; cin>>a; switch(a){ case 1: cout<<"你的情感很細膩,善於觀察細節。在感情中,相比於戀人的甜言蜜語,你更在乎對方實際的行動。"; break; case 2: cout<<"在感情中,你不僅洞察力超好,第六感也很強,越是親近的人這種直覺越敏銳,所以另一半對你很難有所隱藏,因為你可以憑借著蛛絲馬跡得到你想要知道的消息。"; break; case 3: cout<<"你是個典型的顏控,在擇偶時很注重另一半的外表和身高。"; break; case 4: cout<<"面對感情你很承盾,一方面你很感性,渴望浪漫熱烈的愛情;另一方面你又很理性,明白現實的殘酷和金錢的重要性"; break; case 5: cout<<"在感情方面你很挑剔,所以很難遇到心動的對象。不過在戀愛時你卻很專一,一旦喜歡上某個人,你就會全心全意的對他好,所以和你談戀愛是一件很幸福的事。"; break; case 6: cout<<"感情中你很缺乏安全感,有時候會忍不住通過試探或考驗的方式去確認對方是否愛你。"; break; default: cout<<"亂輸入,活該一輩子舔狗命!!!"; } } ``` ![](https://hackmd.io/_uploads/r1FGB8cb6.png) ## 2.判斷座標(x,y)在第幾象限。 (巢狀if…else 應用) ``` //資訊一乙 30號 陳品博 #include<iostream> using namespace std; int main() { int x; cout<<"請輸入一個x值(不為0):"; cin>>x; int y; cout<<"請輸入一個y值(不為0):"; 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/S1x3ZLLqbp.png) ## 3.用*組成三角形(for迴圈的應用) ``` //資訊一乙 30號 陳品博 #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/r1l_ILcZT.png) ## 4.十二生肖查詢(switch…case的應用) ``` //資訊一乙 30號 陳品博 #include<iostream> using namespace std; int main() { int x; cout<<"請輸入民國年:"; cin>>x; int y=(x%12); switch(y){ 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/HkE2P8qbT.png) ## 5.閏年 ``` //資訊一乙 30號 陳品博 #include<iostream> using namespace std; int main() { int x; cout<<"請輸入西元年:"; cin>>x; if (x%4!=0) cout<<x<<"年非閏年"; else cout<<x<<"年是閏年"; } ``` ![](https://hackmd.io/_uploads/SkHaIUqba.png) ## 6.重複計算BMI(while 迴圈)(P.129)(必考) ``` //資訊一乙 30號 陳品博 #include<iostream> using namespace std; int main() { char go_again ='Y'; float height,weight; while (go_again =='Y'||go_again =='Y'){ cout<<"請輸入身高(公分)"; cin>>height; cout<<"請輸入體重(公斤)"; cin>>weight; cout<<"您的BMI為:"<<weight/(height*height)*10000<<endl; cout<<"要繼續計算另一位嗎?"<< "(要請輸入Y or y,若不要請輸入非Y or y的字元):"; cin>>go_again; } } ``` ![](https://hackmd.io/_uploads/SJjxdLcZp.png) ## 7. 計算兩整數的最大公因數(while 迴圈)(P.131)(20231023) ``` //資訊一乙 30號 陳品博 #include<iostream> using namespace std; int main() { int a,b,c; cout<<"請計算兩整數的最大公因數:\n"<<"請輸入第1個數字:"; cin>>a; cout<<"請輸入第2個數字:"; cin>>b; while(b!=0){ c = a%b; a = b; b = c; } cout<<"最大公因數是:"<<a; } ``` ![](https://hackmd.io/_uploads/r1do9tmM6.png) ![](https://hackmd.io/_uploads/rJEbfcQz6.png) ## 8. 密碼檢驗程式(P.133)(自行設計)(20231023) ``` //資訊一乙 30號 陳品博 #include<iostream> using namespace std; int main(){ int a,b; a=123; for(int i=1;i <= 3;i++){ cout<<"請輸入密碼\n"<<"第"<<i<<"次輸入密碼:"; cin>>b; if(a==b) break; } if(a==b) cout<<"歡迎進入本系統"; else cout<<"連續數入三次錯誤密碼,無法登入"; } ``` ![](https://hackmd.io/_uploads/BksKtFmGp.png) ![](https://hackmd.io/_uploads/SJaa-5Qza.png) ## 本章應用實例 (HW) ## 05-24電影分級制度 ``` //資訊一乙 30號 陳品博 #include<iostream> using namespace std; int main() { int age; cout << "請輸入年齡\n"; cin >> age; switch(age) { case 0 ... 5: cout << "可看普遍級"; break; case 6 ... 11: cout << "可看普遍級及保護級"; break; case 12 ... 17: cout << "可看限制級以外的影片"; break; case 18 ... 1000000000: cout << "可看各級影片"; break; } } ``` ![](https://hackmd.io/_uploads/HJUcIwqWT.png) ## 05-27 猜數字遊戲 ``` //資訊一乙 30號 陳品博 #include<iostream> using namespace std; int main() { int n,a=7; cout << "猜一個數字"; cin >> n; while(n != a) { cout << "錯了"; if(n<a){ cout<<"太小了大一點"; } else{ cout<<"太大了小一點"; } cin >> n; } cout << "對了" ; } ``` ![](https://hackmd.io/_uploads/B1MUE5mfp.png) ## 05-28 2的n次方 ``` //資訊一乙 30號 陳品博 #include<iostream> #include<math.h> using namespace std; int main() { int n; cin >> n; cout << pow(2,n); } ``` ![](https://hackmd.io/_uploads/Sy8JcPcZp.png) ## 05-29 成績加總 ``` //資訊一乙 30號 陳品博 #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/HJ7Q3w9ba.png) ## 05-31 顯示1~50的質數 ``` //資訊一乙 30號 陳品博 #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/SJXXw5Qfa.png) ## 05-32 階層計算機 ``` //資訊一乙 30號 陳品博 #include<iostream> using namespace std; int main() { double b=1,n,c=0,d,e; cout<<"請輸入一個數\n"; cin>>n; while(n!=0){ for(int a = 0 ; a != n ; a++ ){ b = b*(a+1); } cout<<n<<"! 等於"<<b; cout<<"\n請輸入一個數\n"; cin>>n; } cout<<"謝謝使用階乘計算機"; } ``` ![](https://hackmd.io/_uploads/ry5X8oXMa.png) ## 實習心得(至少100字) 經過今天實習課,我學會了if、while、for等等,這一次練習ch05-32我不照課本的寫竟然也可以,我反而覺得課本寫的比較亂呢!這帶給了我極大的成就感!讓我覺得程式變有趣了呢!!! # 20231106 課堂作業(CH6函式) ## Ch06-21 以時間亂數種子產生8個200到600的亂數 ``` //資訊一乙 30號 陳品博 #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<<endl; } ``` ![image.png](https://hackmd.io/_uploads/SJgNjW8X6.png) ![image.png](https://hackmd.io/_uploads/B1D7OZLmp.png) ## Ch06-25 猜數字遊戲 ``` //資訊一乙 30號 陳品博 #include<iostream> #include<stdlib.h> #include<time.h> using namespace std; int main() { int max=10,min=1,n; srand(time(0)); int ans=(rand() % (max-min+1))+ min; while(n != ans){ cout << "猜猜看數字是多少?(1-10):"; cin >> n; if(n > ans) cout << "猜錯,再小一點\n"; if(n > ans) cout << "猜錯,再大一點\n"; } cout << "對了"; } ``` ![image.png](https://hackmd.io/_uploads/H1mETbLmT.png) ![image.png](https://hackmd.io/_uploads/HJ9-p-8mT.png) ## Ch06-26 猜拳遊戲 ``` //資訊一乙 30號 陳品博 #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/H1IFHfIXT.png) ![image.png](https://hackmd.io/_uploads/HJtvBfL7T.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/ryx61DfUX6.png) ## Ch06-24 查詢語音費率函式 下列程式為依據手機月租費方案(如下),查詢語音費率的函式。 y 月租費196:每秒0.0869元。 y 月租費396:每秒0.0782元。 y 月租費796:每秒0.0696元。 ``` //資訊一乙30號陳品博 #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/rkZ03dd4a.png) 執行結果: ![image](https://hackmd.io/_uploads/ryEw3uuVp.png) ## Ch06-27 階乘函式 利用階乘函式factorial()傳回n!的值給main()。 程式碼: ``` //資訊一乙30號陳品博 #include <iostream> using namespace std; double factorial(int); int main() { while(true){ cout <<"請輸入1-170間的整數(輸入0即結束程式):"; int a=0; cin >> a; if (a == 0) break; cout <<a<<"! 等於"<< factorial(a)<< endl; } cout << '\n' << "謝謝您使用"; } double factorial(int n) { double fact; for(fact = 1;n >0; n--) fact = fact * n ; return fact; } ``` 程式碼實作畫面(需有班級座號姓名浮水印): ![image](https://hackmd.io/_uploads/ryWDGYOVp.png) 執行結果: ![image](https://hackmd.io/_uploads/S10NfKd4T.png) # 課後練習 ## 1.組成三角形 以函式的方式撰寫判斷輸入的三個值是否可組成三角形之程式(判斷條件:任兩邊和須大於第三邊)。 程式碼: ``` //資訊一乙30號陳品博 #include <iostream> using namespace std; int fact(int a,int b,int c) { bool t; if ((a+b) <= c){ t = 0; }else if ((a+c) <= b){ t = 0; }else if ((b+c) <= a){ t = 0; }else{ t = 1; } return t; } int main() { int a,b,c; cout << "輸入三個邊" ; cin >> a >> b >> c; bool t = fact(a,b,c); if (t == 1) cout <<"是三角形"; else cout <<"不是三角形"; } ``` 程式碼實作畫面(需有班級座號姓名浮水印): ![image](https://hackmd.io/_uploads/By5N-5dET.png) 執行結果: ![image](https://hackmd.io/_uploads/r1K8b5uV6.png) ## 2.遞迴 能使用遞迴的程式技巧實作階乘運算。 程式碼: ``` //資訊一乙30號陳品博 #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間的整數(輸入0即結束程式):"; cin >> x; if (x == 0) break; cout << x <<"! ="<< fact(x)<< endl; } cout << '\n' << "謝謝您使用"; } ``` ![image](https://hackmd.io/_uploads/rkR0EKuNT.png) ![image](https://hackmd.io/_uploads/By5TNK_Ea.png) ## 3.專案練習 將1~4題寫成一個專案型式。 主畫面: ![BJ8o1UOVT](https://hackmd.io/_uploads/r1B8IduVp.png) 程式碼: ``` //資訊一乙30號陳品博 #include <iostream> #include <stdio.h> #include <cstdlib> #include <ctime> using namespace std; void p01(); void p02(); void p03(); void p04(); void p05(); void p06(); void p07(); int fact(int a,int b,int c) { bool t; if ((a+b) <= c){ t = 0; }else if ((a+c) <= b){ t = 0; }else if ((b+c) <= a){ t = 0; }else{ t = 1; } return t; } 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 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 dice() { int a ; a = (rand()%6)+1; return a; } double factorial(int n) { double fact; for(fact = 1;n >0; n--) fact = fact * n ; return fact; } long double fact(int n) { if(n == 1) return 1; else return (n * fact(n-1)); } 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<<"程式設計實習 資訊一乙 30 陳品博\n"; cout<<"-----------------------------"<<endl; int arrLength=sizeof(menuItem)/sizeof(menuItem[0]); //讀取幾個Item for(i=0; i<arrLength; i++) { cout<<menuItem[i]<<endl; } cout<<"請輸入選項:"; cin>>selMenu; switch(selMenu) { case 1: p01(); break; case 2: p02(); break; case 3: p03(); break; case 4: p04(); break; case 5: p05(); break; case 6: p06(); break; case 7: p07(); break; } system("PAUSE"); cout<<"\n"; } } void p01(){ int max=10,min=1,n; srand(time(0)); int ans=(rand() % (max-min+1))+ min; while(n != ans){ cout << "猜猜看數字是多少?(1-10):"; cin >> n; if(n > ans){ cout << "猜錯,再小一點\n"; }else if(n > ans) cout << "猜錯,再大一點\n"; } cout << "對了"; } void p02(){ int user; srand(time(0)); cout<<"請輸入你要出的拳(1.剪刀2.石頭3.布):"; cin>>user; game(user); } 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 p04(){ int a; cout<<"請輸入要查詢的月租費方案(196、396、796):"; cin >> a; bill(a); } void p05(){ while(true){ cout <<"請輸入1-170間的整數(輸入0即結束程式):"; int a=0; cin >> a; if (a == 0) break; cout <<a<<"! 等於"<< factorial(a)<< endl; } cout << '\n' << "謝謝您使用"; } void p06(){ int a,b,c; cout << "輸入三個邊" ; cin >> a >> b >> c; bool t = fact(a,b,c); if (t == 1) cout <<"是三角形"; else cout <<"不是三角形"; } void p07(){ int x; while(true){ cout <<"請輸入1-170間的整數(輸入0即結束程式):"; cin >> x; if (x == 0) break; cout << x <<"! ="<< fact(x)<< endl; } cout << '\n' << "謝謝您使用"; } ``` 程式碼實作畫面(需有班級座號姓名浮水印): ## 心得感想(100字): 今天的實習課老師教了很多很多東西,比如說:函數參數,傳回值(return),還有取決對值,現在時間等等等。這讓我收穫良多,取指數也不用像之前一樣麻煩。 ## 心得感想(100字): 今天的實習課老師教了很多很多東西,比如說:...等等。這讓我收穫良多 # 20231204 課堂作業(CH7陣列與指標) ## 隨堂練習 ### Ch07-03 請使用者輸入8個數字,找出最大值 程式碼: ``` //資訊一乙30號陳品博 #include <iostream> const int SIZE=8; using namespace std; int main() { 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; } ``` 程式碼實作畫面(需有班級座號姓名浮水印): ![image](https://hackmd.io/_uploads/r110j1oHa.png) 執行結果: ![image](https://hackmd.io/_uploads/HkbKs1sSa.png) ### Ch07-04 氣泡排序法 B:66 o:111 g:103 d:100 a:97 n:110 程式碼: ``` //資訊一乙30號陳品博 #include <iostream> const int SIZE=6; using namespace std; int main() { char name[SIZE] ={'B','o','g','d','a','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]; } ``` 程式碼實作畫面(需有班級座號姓名浮水印): ![image](https://hackmd.io/_uploads/B1pq8xiBT.png) 執行結果: ![image](https://hackmd.io/_uploads/r11tUeiSa.png) ### Ch07-05 選擇排序法 程式碼: ``` //資訊一乙30號陳品博 #include <iostream> const int SIZe=5; using namespace std; int main() { 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]<<" "; } ``` 程式碼實作畫面(需有班級座號姓名浮水印): ![image](https://hackmd.io/_uploads/HJBBWborp.png) 執行結果: ![image](https://hackmd.io/_uploads/ryYQWbsBp.png) ### Ch07-06 循序搜尋法 程式碼: ``` //資訊一乙30號陳品博 #include <iostream> using namespace std; int main() { int data[10] = {2,15,3,55,46,98,4,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/By5UIZorT.png) 執行結果: ![image](https://hackmd.io/_uploads/Skpv8WoHp.png) ### Ch07-07 二元搜尋法 程式碼: ``` //資訊一乙30號陳品博 #include <iostream> using namespace std; int main() { int a[7] = {2,16,34,47,53,67,81}; int n,m,l=0,r=6; cout << "請輸入要搜尋的值:"; cin >> n; while(l<=r){ m=(l+r)/2; if(a[m] == n) { cout << "在註標" << m << "的位置找到"<<n; return 0; }else if(a[m] > n) { r=m-1; }else if(a[m] < n) { l=m+1; } } cout << n << "不在陣列中"; } ``` 程式碼實作畫面(需有班級座號姓名浮水印): ![image](https://hackmd.io/_uploads/rJwHkXNL6.png) 執行結果: ![image](https://hackmd.io/_uploads/HyNEkQE8p.png) ### 1204專案練習 程式碼: ``` //資訊一乙30號陳品博 #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<<"程式設計實習 資訊一乙 30 陳品博\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','o','g','d','a','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; }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/rkU8Wm4Up.png) ## 心得感想(100字)(12/04): 今天的實習課老師教了很多很多東西,比如說:陣列的初始值設定等等。這讓我收穫良多 ## 12/18隨堂練習 ### Ch07-08 輸出字串與字元陣列大小(P.245) 程式碼: ``` //資訊一乙 30 陳品博 #include <iostream> using namespace std; int main() { char n1[]="john smith"; char n2[]={'J','O','H','N',' ','S','M','I','T','H'}; cout <<"name1[]大小為:"<<sizeof(n1)<<"\n" ; cout <<"name2[]大小為:"<<sizeof(n2)<<"\n" ; cout << "name1[]:"<<n1<<'\n'; cout << "name2[]:"<<n2<<'\n'; } ``` 程式碼實作畫面(需有班級座號姓名浮水印): ![image](https://hackmd.io/_uploads/S1IYsU6L6.png) 執行結果: ![image](https://hackmd.io/_uploads/r1IqsUpLp.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 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/HkZIewaLa.png) ![image](https://hackmd.io/_uploads/S1kDgDaUp.png) ## CH7課後練習(2023/12/18) ### 1.大樂透539是一種樂透型遊戲:(參考P.275) 您必須從01~39中任選6個號碼進行投注。開獎時,開獎單位將隨機開出六個號碼,這一組號碼就是該期大樂透的中獎號碼,也稱為「獎號」。您的六個選號中,如果有三個以上(含三個號碼)對中當期開出之六個號碼,即為中獎,並可依規定兌領獎金。 [大樂透中獎說明] (https://www.taiwanlottery.com.tw/lotto649/index.asp) 對中當期獎號之任三碼 普獎 對中當期獎號之任四碼 伍獎 對中當期獎號之任五碼 參獎 與當期六個獎號完全相同者 頭獎 程式碼: ``` //資訊一乙 30 陳品博 #include <iostream> #include <stdlib.h> #include <time.h> using namespace std; int main() { int a=0,n=0,b=0,s=0,d=0; int z[5],r[5]; cout << "本期今彩539的號碼是:"; for (int i = 0; i < 5; ++i) { r[b] = rand() % 20 + 1; cout << r[b] << " "; b++; } cout<<"\n投注號碼:"; for (int b = 0; b < 5; ++b) { cin >> z[a] ; a++; } for (int b = 0; b < 5; ++b) { cout << z[n] ; n++; } for (int b = 0; b < 5; ++b) { if(z[s]==r[s]) d++; s++; } cout <<"\n中了"<< d<< "個號碼"; srand((time(0))); } ``` 程式碼實作畫面(需有班級座號姓名浮水印): ![image](https://hackmd.io/_uploads/BJsDxjIva.png) 執行結果: ![image](https://hackmd.io/_uploads/HkB5lj8wa.png) ### 2.奇數或偶數:使用亂數函數 rand() 產生 5個1~20的整數,分別對奇數的數加總以及偶數的數加總。 *小提示: 奇數:if(num%2!=0) 程式碼: ``` #include <iostream> #include <stdlib.h> #include <time.h> using namespace std; int main() { srand((time(0))); int r; int oddSum = 0; int evenSum = 0; for (int i = 0; i < 5; ++i) { r = rand() % 20 + 1; cout << "1~20隨機的數" <<"["<<(i + 1)<<"]" << ":" << r << endl; if (r % 2 == 0) { evenSum += r; } else { oddSum += r; } } cout << "奇數的總和: " << oddSum << endl; cout << "偶數的總和: " << evenSum << endl; } ``` 程式碼實作畫面(需有班級座號姓名浮水印): ![image](https://hackmd.io/_uploads/r1ZWjDa8T.png) 執行結果: ![image](https://hackmd.io/_uploads/S1OfswTIp.png) ### 3.在「字元陣列」中放入10個英文字母「television」,然後將字母依小到大(a到z)順序從螢幕輸出。(參考P.277)(注意:字母須由小排到大!!) 輸出結果如下: [BkIifNT8T](https://hackmd.io/_uploads/HyviEP6Ia.png) 程式碼: ``` //資訊一乙 30 陳品博 #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/rJAR-jUwT.png) 執行結果: ![image](https://hackmd.io/_uploads/ryPTWjIDa.png) ## 心得感想(100字)(12/18): ## CH7課後練習(2023/12/25) ### 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 ``` 程式碼: ``` //資訊一乙 30 陳品博 #include<iostream> #include<time.h> #include<stdlib.h> #include<algorithm> using namespace std; int main(){ cout<<"輸入幾位學生"; int s; cin>>s; int a[s][5]; cout<<"輸入每位學生的成績"<<endl; for(int i = 0;i<s;i++){ int n; 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<s;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/Hy4TQjUvT.png) 執行結果: ![image](https://hackmd.io/_uploads/BkFi7oLPT.png) ### 2.在圖一的表格中,橫排稱為列(row),直排稱為行(column),以Xij來表示表格X中的第i列第j行的元素。如圖一中的X00=1、X12=6。(參考P.252) 請設計一個程式可執行以下工作: (1) 根據使用者輸入的數值設定表格大小(先輸入列,再輸入行) (2) 由使用者輸入數值填滿表格 (3) 輸入完成後,將整個表格印出 輸入 ``` 2 3 1 2 3 4 5 6 ``` 輸出 ``` iArray[0][0]=1 iArray[0][1]=2 iArray[0][2]=3 iArray[1][0]=4 iArray[1][1]=5 iArray[1][2]=6 ``` 程式碼: ``` //資訊一乙 30 陳品博 #include <iostream> using namespace std; int main() { int z[2][3]; for(int i = 0;i<2;i++){ for(int c = 0;c<3;c++){ cin>>z[i][c]; } } for(int i = 0;i<2;i++){ for(int c = 0;c<3;c++){ cout<<"iArray["<<i<<"]["<<c<<"]="<<z[i][c]<<endl; } } } ``` 程式碼實作畫面(需有班級座號姓名浮水印): ![image](https://hackmd.io/_uploads/SkX6aoLvT.png) 執行結果 ![image](https://hackmd.io/_uploads/SJopTsLDa.png) ### 3.請設計一程式可執行以下工作: (1) 輸入5個值並存入一維陣列a中 (2) 計算陣列a中5個值的總和,並印出 輸入 ``` 1 3 5 7 9 ``` 輸出 ``` 25 ``` 程式碼: ``` //資訊一乙 30 陳品博 #include <iostream> using namespace std; int main(){ int a[5],b; for(int i=0;i<5;i++){ cin>>a[i]; b=b+a[i]; } cout<<b; } ``` 程式碼實作畫面(需有班級座號姓名浮水印): ![image](https://hackmd.io/_uploads/Bk-sCj8P6.png) 執行結果 ![image](https://hackmd.io/_uploads/SyTiAiUPa.png) ### 4.請設計一程式可執行以下工作:(P.263)(P.267) (1) 宣告一個指標p,並使用new運算子動態配置一塊記憶體 (2) 輸入5個值,放到指標p所控制的5個連續記憶體位址 (3) 印出指標變數p (4) 找出5個值中的最大值,印出最大值及其所在的記憶體位址 輸入 ``` 5 10 15 1 0 ``` 輸出 ``` 指標變數p所指向的記憶體起點:0x2efc50 最大值為15 最大值的記憶體位址:0x2efc58 ``` 程式碼: ``` //資訊一乙 30 陳品博 #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/Hkq6x2LPa.png) 執行結果 ![image](https://hackmd.io/_uploads/rklpl2Lwp.png) ## 心得感想(100字)(12/25): ## 加分題練習