{%hackmd theme-dark %} <h1><center><img src = "https://i.imgur.com/thmVmX6.png?w=1000" height = 50> 作業系列 <img src = "https://i.imgur.com/thmVmX6.png?w=1000" height = 50><br>20231106 課堂作業(CH6函式)</center></h1> ## 隨堂練習 ### Ch06-21 以時間亂數種子產生5個200~600的亂數 程式碼: ```cpp= #include <iostream> using namespace std; int main() { srand(time(0)); for (int i=0;i<8;i++) cout << (rand() %401)+200<<"\n"; } ``` 執行結果:  ### Ch06-26 猜數字遊戲 程式碼: ```cpp= #include <iostream> #include <cstdlib> #include <ctime> using namespace std; int main() { srand(time(0)); int number = rand() % 10 + 1; int guess = 0; cout << "猜猜看數字是多少?(1~10)" << endl; while (guess != number) { cin >> guess; if (guess > number) { cout << "猜錯了,再小一點" << endl; } else if (guess < number) { cout << "太小了,再大一點" << endl; } else { cout << "猜對了YA" << endl; } } return 0; } ``` 執行結果:  ### Ch06-26 猜拳遊戲 程式碼 ```cpp= #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); } ``` 執行結果  ### Ch06-28 猜點數遊戲 程式碼 ```cpp= #include <iostream> #include <stdlib.h> #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; } ``` 執行結果  ### Ch06-24 查詢語音費率函式 程式碼 ```cpp= #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); } ``` 執行結果  ### Ch06-27 階乘函式 程式碼 ```cpp= #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; } ``` 執行結果  ## 課後練習 ### 1.組成三角形 ### 2.遞迴 ### 3.專案練習 ```cpp= #include <stdio.h> #include <iostream> #include <stdlib.h> #include <time.h> using namespace std; void p01(); void p02(); void p03(); void p04(); void p05(); void p06(); int number(); void game(int play); int dice(); void bill (int o02); double ans(int n); int tsqar(int x,int y,int z); int main() { string menuItem[]= { "[1]猜數字遊戲", "[2]猜拳遊戲", "[3]猜點數遊戲", "[4]查詢語音費率函式", "[5]階乘函式", "[6]組成三角形", "[7]遞迴", "[0]離開" }; int i; int selMenu=99; while(selMenu!=0) { system("chcp 950"); //編碼 system("cls"); //清除畫面 cout<<"程式設計實習 資訊一乙 21 張家笛\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: ; break; } system("PAUSE"); cout<<"\n"; } } int number() { int num=(rand() % 10)+1; return num; } void p01(){ srand(time(0)); int a , b; b = number(); while(a != b){ cout<<"猜數字(1~10):"; cin>>a; if(a > b) cout<<"猜錯了,再小一點\n"; else if(a < b) cout<<"猜錯了,再大一點\n"; } cout<<"猜對了!YA!"; } void p02(){ int play; srand(time(0)); cout << "請出拳(1.剪刀 2.石頭 3.布)"; cin >> play; game(play); } void game(int play){ int cou = (rand() % 3)+1; if (cou==play){ cout << "平手"; } else{ if (play==1){ if (cou==2) cout << "電腦出石頭lose"; else cout << "電腦出布win"; } else if (play==2){ if (cou==3) cout << "電腦出布lose"; else cout << "電腦出剪刀win"; } else if (play==3){ if (cou==1) cout << "電腦出剪刀lose"; else cout << "電腦出石頭win"; } } } 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; } } int dice() { int o01; o01=(rand()%6)+1; return o01; } void p04(){ int o02; cout << "請輸入要查詢的方案(196 396 796)\n"; cin >> o02; bill(o02); } void bill (int o02){ switch(o02){ case 196: cout << "語音費率每秒 : 0.0869元"; break; case 396: cout << "語音費率每秒 : 0.0782元"; break; case 796: cout << "語音費率每秒 : 0.0696元"; break; default: cout << "無此費率 請重新查詢"; } } void p05(){ while(true){ cout << "請輸入50以內的整數(輸入0可以停止)\n"; int num=0; cin >> num; if (num == 0) break; cout << num << "! = " << ans(num) << "\n"; } } double ans(int n) { double fact; for(fact = 1; n > 0 ; n-- ) fact = fact * n; return fact; } void p06(){ int t1,t2,t3; cout << "請輸入三角形的第一個邊"; cin >> t1; cout << "請輸入三角形的第二個邊"; cin >> t2; cout << "請輸入三角形的第三個邊"; cin >> t3; if (tsqar(t1,t2,t3)==1) cout << "是三角形\n"; else cout << "不是三角形\n"; } int tsqar(int x,int y,int z){ int sqar; if ((x+y>z) and (x+z>y) and (y+z>x)){ sqar=1; return sqar; } } ``` # --------------------------------------------------- <h1><center><img src = "https://i.imgur.com/thmVmX6.png?w=1000" height = 50> 作業系列 <img src = "https://i.imgur.com/thmVmX6.png?w=1000" height = 50><br>20231204 課堂作業(CH7陣列與指標)</center></h1>20231204 課堂作業(CH7陣列與指標) ## 隨堂練習 ### Ch07-03 請使用者輸入7個數字,找出最大值 程式碼 ```cpp= #include<iostream> #define SIZE 7 using namespace std; int main() { int numbers[SIZE]; cout << "請輸入7個數字,程式將找到最大值" << endl; for(int i = 0;i < SIZE;i++){ cout << "請輸入第 " << (i+1) << " 個數字:"; cin >> numbers[i]; } int MAX = numbers[0]; for (int i = 0;i <SIZE;i++) if(numbers[i] > MAX) MAX = numbers[i]; cout << "在輸入的數字中,數值最大的是 " << MAX; } ```  ### Ch07-04 氣泡排序法 程式碼 ```cpp= #include<iostream> #define SIZE 6 using namespace std; int main() { char array[SIZE]={'C','h','a','k','r','a'}; cout << "排序前:"; for (int i = 0;i < SIZE;i++) cout << array[i]; for(int i = 1;i < SIZE;i++) for(int j = 0;j < SIZE-1;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]; } ```  ### Ch07-05 選擇排序法 程式碼 ```cpp= #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] << " "; } ```  ### Ch07-06 循序搜尋法 程式碼 ```cpp= #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 << "不在陣列中"; } ```  ### Ch07-07 二元搜尋法 程式碼 ```cpp= #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 << "不在陣列中"; } ``` 執行結果  ### 1204專案練習 程式碼 ``` ``` ## 心得感想(100字)(11/06): 今天第七章的陣列與指標有很多搜尋排序的練習, 就讓我聯想到資訊科技也有教到 還有以前國中也有上道, 經過了這麼多次的練習之後應該也會更加熟悉了吧! 這次的練習主要最容易出問題 打錯的地方還是數字, 很容易不小心被繞進去, 把-1打成+1之類的。 ## 隨堂練習 ### Ch07-08 輸出字串與字元陣列大小(P.245) 程式碼 ```cpp= #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) << endl; cout << "name2[]大小為:" << sizeof(name2) << endl; cout << "name1[]:" << name1 << endl; cout << "name2[]:" << name2 << endl; } ``` 執行結果  ### Ch07-27 以指標實作氣泡排序法(P.277) 程式碼 ```cpp= #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; } ``` 執行結果  ## CH7課後練習(2023/12/18) ### 1.大樂透539是一種樂透型遊戲:(參考P.275) ```cpp= #include<iostream> #include<time.h> #include<stdlib.h> using namespace std; int main(){ srand(time(0)); int num[5],guess[5],count = 0; cout<<"本期樂透號碼開出順序:"; for(int i=0;i<5;i++) { num[i]=rand()%39+1; for(int j=0;j<i;j++) { if(num[j]==num[i]) { num[i]=rand()%39+1; j=0; } } cout<<num[i]<<" "; } cout<<"\n號碼排序:"; for(int i=0;i<5;i++){ for(int j=i;j<5;j++){ if(num[i]>num[j]){ int temp=num[i];num[i]=num[j];num[j]=temp; } } cout<<num[i]<<" "; } cout<<endl; for(int i=0;i<5;i++) { cout<<"請輸入號碼"<<i+1<<":"; cin>>guess[i]; for(int j=0;j<5;j++){ if(guess[i]==num[j]){ count++; break; } } } cout<<"總共中了"<< count<<"個號碼,恭喜獲得"; switch(count){ case 5: cout << "頭獎"; break; case 4: cout << "貳獎"; break; case 3: cout << "參獎"; break; case 2: cout << "肆獎"; break; default: cout << "槓龜"; break; } } ```  ### 2.奇數或偶數:使用亂數函數 rand() 產生 5個1~20的整數,分別對奇數的數加總以及偶數的數加總。 程式碼: ```cpp= #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; } ```  ### 3.在「字元陣列」中放入10個英文字母「television」,然後將字母依小到大(a到z)順序從螢幕輸出。(參考P.277)(注意:字母須由小排到大!!) 程式碼: ```cpp= #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]; } } ```  ## 4.請使用二維陣列存取學生的多科成績,並輸出班級成績單。 ``` 輸出結果如下: 輸入每位學生的成績(依序為國、英、數、基電、物理,以空格區隔) 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 ``` 程式碼: ```cpp= #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; } } ```  ## 5.在圖一的表格中,橫排稱為列(row),直排稱為行(column),以Xij來表示表格X中的第i列第j行的元素。如圖一中的X00=1、X12=6。 ``` 請設計一個程式可執行以下工作: (1) 根據使用者輸入的數值設定表格大小(先輸入列,再輸入行) (2) 由使用者輸入數值填滿表格 (3) 輸入完成後,將整個表格印出 ``` 程式碼: ```cpp= #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; } } } ```  ## 6.請設計一程式可執行以下工作: ``` (1) 輸入5個值並存入一維陣列a中 (2) 計算陣列a中5個值的總和,並印出 ``` 程式碼: ```cpp= #include <iostream> using namespace std; int main(){ int a[5],sum = 0; for(int i=0;i<5;i++){ cout<<"數值"<<i+1<<":"; cin>>a[i]; sum+=a[i]; } cout<<"總和:"<< sum; } ```  ## 課外補充:sort 排序用法(超好用速解法) ``` 加入標頭檔 #include <algorithm> 升序:sort(begin, end); 降序:reverse(begin, end); ``` 程式碼: ```cpp= #include <iostream> #include <algorithm> using namespace std; int main(){ int a[5]; cout<<"請輸入5個數字:"; for(int i=0;i<5;i++) { cin>>a[i]; } sort(a,a+5); cout<<"由小排到大:"; for(int i=0;i<5;i++) { cout<<a[i]<<" "; } reverse(a,a+5); cout<<"\n由大排到小:"; for(int i=0;i<5;i++) { cout<<a[i]<<" "; } } ```  ## 7.請設計一程式可執行以下工作: ``` (1) 宣告一個指標p,並使用new運算子動態配置一塊記憶體 (2) 輸入5個值,放到指標p所控制的5個連續記憶體位址 (3) 印出指標變數p (4) 找出5個值中的最大值,印出最大值及其所在的記憶體位址 ``` 程式碼 ```cpp= #include<iostream> #include<algorithm> using namespace std; int main(){ cout<<"請輸入五個數值:"; int *p = new int[5];//宣告一個指標p,並使用new運算子動態配置一塊記憶體 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; } ```  ## 心得感想(100字)(12/18):
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up