{%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>2023/09/18~2023/11/06 課堂作業(CH3~6)</center></h1> <h1><center>2023/09/18 課堂作業 (CH3變數與常數)</center></h1> ## ch03-12 各種字面常數表示法的應用 ```cpp= #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; } ```  執行結果:  ## ch03-18 讓使用者從鍵盤輸入資料 ```cpp= #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; } ```  執行結果:  ## ch03-19 換匯程式 ```cpp= #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 << "日幣\n"; } ```  執行結果:  ## ch03-20 交換兩變數的值 ```cpp= #include <iostream> using namespace std; int main() { int a = 10, b= 20; //宣告兩個 int 變數 a, b int temp; //宣告兩個暫存 int 變數 temp cout << "交換前 a= " << a << "\t b " << b << endl; temp = a; //把變數 a 的值指定給暫存變數 a = b; //把變數 b 的值指定給 a b = temp; //把變數暫存變數的值指定給 b cout << "交換後 a= " << a << "\t b " << b << endl; } ``` 執行結果:  ## ch03-21 大小寫轉換 ```cpp= #include <iostream> using namespace std; int main() { char upper = 'S', lower; //宣告兩個變數 lower = upper + 32; cout << upper << " 的小寫是 " << lower << endl; } ``` 執行結果:  ## 心得感想 <center>變數與常數是程式設計中非常重要的概念。理解它們的區別和用法可以幫助我們寫出更清晰、更易讀的程式碼。變數可以儲存資料,而常數則不能。變數的值可以被改變,而常數的值不能被改變。在程式設計中,我們應該儘可能使用常數,而不是變數。這樣可以提高程式的安全性、健壯性和可維護性。變數與常數練習題可以幫助我們鞏固對變數和常數的理解,並提高我們編寫程式碼的能力。</center> ## 實作練習 ### P74_1 ```cpp= #include<iostream> using namespace std; #define SIZE 10 //巨集變數 int main() { cout << SIZE ; } ```  執行結果:  ### P74_2 ```cpp= //資訊一乙21張家笛 #include<iostream> #include <string> using namespace std; int main() { int a = 10; double b = 101.7; char c = 'c'; cout << a << endl << b << endl << c ; } ```  執行結果:  ### P74_5 ```cpp= //資訊一乙21張家笛 #include<iostream> using namespace std; int main() { cout << "\"我正在學習\"C++\"語言\"" ; } ```  執行結果:  ### P74_7 ```cpp= //資訊一乙21張家笛 #include<iostream> using namespace std; int main() { int a = 10 , b = 20 , c = 30 , d ; cout << "交換前" << "a = " << a << "b = "<< b << "c = "<< c << endl ; d=a; a=b; b=c; c=d; cout << "交換後" << "a = " << a << "b = "<< b << "c = "<< c ; } ```  執行結果:  <h1><center>2023/09/25 課堂作業 (CH4運算子與運算式)</center></h1> ## Ch04-05 前置與後置遞增運算子 ``` #include <iostream> using namespace std ; int main() { int i = 3 , j = 3 ; cout << boolalpha; cout <<"(i == j): " << (i==j) << endl ; cout <<"(i > j): " << (i>j) << endl ; cout <<"(++i > j): " << (++i>j) << endl ; cout <<"(i-- < 3): " << (i-- < 3) << endl ; cout <<"(i != j): " << (i != j) << endl ; } ```  執行結果:  ## Ch04-06 關係運算子練習 ``` #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; } ```  執行結果:  ## Ch04-07 位元運算子練習 ``` #include <iostream> #include <iostream> using namespace std ; int main() { short i = 9 , j = 25 ; cout <<"(i >> 1): " << (i>>1) << endl ; cout <<"(i << 1): " << (i<<1) << endl ; cout <<"(~i = )" << (~i) << '\n' <<endl ; cout << "i =" << i << "\tj =" << j << endl; cout <<"(i & j): " << (i & j) << endl ; cout <<"(i | j): " << (i | j) << endl ; cout <<"(i ^ j): " << (i ^ j) << endl ; } //vscode 20230925 1206221 ```  執行結果:  ## Ch04-08 位元運算子練習 ``` #include <iostream> using namespace std ; int main() { int i = 9 , j = 25 ; cout << boolalpha; cout <<"i >> 1 = " << (i >> 1) << "\n" ; cout <<"i << 1 = " << (i << 1) << "\n" ; cout << "~i = " << (~i) << "\t~j = " << (~j) << '\n'; cout <<"i & j = " << (i & j) << "\n" ; cout <<"i | j = " << (i | j) << "\n" ; cout <<"i ^ j = " << (i ^ j) << "\n" ; } //vscode 20230925 1206221 ```  執行結果:  ## 應用實例練習(P.98) ### Ch04-15 模擬換幣機 ``` #include <iostream> using namespace std; int main() { int money,ten,five,one,fifty; cout << "輸入金額"; cin >> money; fifty = money/50; ten = (money%50)/10; five = (money%10)/5; one = (money%10)%5; cout << "共可換" << "五十元:" << fifty << "個\t十元:" << ten << "個\t五元:" << five << "個\t一元:" << one <<"個"; } //vscode 20230925 1206221 ```  執行結果:  ### Ch04-16 華氏溫度轉攝氏溫度  ##Ch04-16 華氏溫度轉攝氏溫度 ``` #include<iostream> using namespace std; #define SIZE 10 //巨集變數 int main() { cout << SIZE ; } ```  執行結果:  ### Ch04-18 成績計算程式 程式碼 ```cpp= #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; } ``` 執行結果:  ## 實作練習 ### 攝氏溫度轉華氏溫度 程式碼 ```cpp= #include <iostream> using namespace std; int main() { float C, F; cout << "請輸入攝氏的溫度: "; cin >> C; F = C * 9 / 5 +32; cout << "換算成華氏溫度為 " << F << " 度"; } ``` 執行結果:  ### 自動販售機(幣值轉換練習) 程式碼 ```cpp= #include <iostream> using namespace std; int main() { int money,ft,ten,five,one; cout << "請輸入車票金額: "; cin >> money; ft = money /50; ten = (money%50)/ 10; five = (money%10)/5; one = (money%10)%5; cout << "需要零錢:" << "五十圓" << ft << "個" << "拾圓" << ten << "個" << "伍圓" << five << "壹圓" << one << "個"; } ``` 執行結果  ## 心得感想 C\+\+運算子與運算式是程式語言中非常重要的部分,它決定了程式碼的執行順序和結果。 運算子包括算術運算子、邏輯運算子和賦值運算子等等,這些運算子可以對變數和常數進行各種運算。 運算式是由運算子、變數和常數組成的表達式,它可以表示一個數值、一個布林值或一個物件。 C\+\+運算子與運算式是程式設計的基礎,掌握好運算子和運算式是編寫出正確和高效的程式碼的關鍵。 <h1><center>2023/10/16 課堂作業 (CH5流程控制)</center></h1> ## 1. if 條件判斷控制 ### 語法 - if條件式 ```cpp= if ( 條件運算式 ) { 如果條件成立時做什麼... } ``` ### 語法 - if-else條件式 ```cpp= if( 條件運算式 ) { 如果條件成立時做什麼... } else { 否則做什麼... } ``` ### 語法 - 巢狀if…else ```cpp= if (條件運算式1){ if (條件運算式2) 程式區塊1 else 程式區塊2 } else{ if (條件運算式3) 程式區塊3 else 程式區塊4 } ``` ### 語法 - if…else if ```cpp= if (條件運算式1){ {程式區塊1} // 條件運算式1成立時 else if (條件運算式2) {程式區塊2} // 條件運算式2成立時 else if (條件運算式n) {程式區塊n} // 條件運算式n成立時 else {程式區塊m} // 其他狀況 } ``` ### 語法 - 條件運算子 ``` (條件運算式) ? (敘述1) : (敘述2) ``` ## 2.switch多條件分支敘述 ```cpp= switch(條件運算式){ case 條件值 1: 程式區塊1 break; case 條件值 2: 程式區塊2 break; case 條件值 N: 程式區塊N break; default: } ``` ## 3.迴圈 ### 語法 - for ```cpp= for(初始運算式; 條件運算式; 控制運算式){ 迴圈內的敘述; } ``` ### 語法 - while ```cpp= while(條件運算式){ 迴圈內的敘述 } ``` ### 語法 - do while ```cpp= do{ 敘述... // 先執行一次迴圈 }while(條件運算式); // 再檢查條件運算式是否成立 ``` ### 語法 - for ```cpp= for(初始運算式; 條件運算式; 控制運算式){ 迴圈內的敘述; } ``` ## 綜合練習 ### 1.夏季電費計算  ``` 夏季電費,請輸入用電度數:800 本月電費為:2836.8 元 ``` ``` 夏季電費,請輸入用電度數:600 本月電費為:1773.8 元 ``` ```cpp= 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,才顯示總分。 ```cpp= #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比較運算式,判斷使用者輸入的數值為奇數或偶數 ```cpp= #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整除的數值之總和。並請加上可讓使用者決定是否再算一次的功能。 ```cpp= #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折,若不加入會員則以原價購買商品。 ```cpp= #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分為及格),並將其輸出 ```cpp= #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 ``` ```cpp= #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 ``` ```cpp= #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歲(含)以上:各級影片)。 ```cpp= #include<iostream> using namespace std; int main() { int age_range; cout << "請輸入年齡(0~120)"; cin >> age_range; switch(age_range){ case 0 ... 5: cout << "可看普遍級" ; break; case 6 ... 11: cout << "可看普遍級 保護級" ; break; case 12 ... 17: cout << "可看限制級以外ㄉ影片"; break; case 18 ... 120: cout << "全部都能看!"; break; default: cout << "0~120聽不懂逆"; } } ```  ### 05-27 猜數字遊戲(自行設計) 下列程式為一個猜數字遊戲(假設答案為7),遊戲會發出提示訊息要使用者輸入 所要猜的數字(1~10),若猜錯,遊戲會繼續進行,直到猜對為止。 若使用者未猜到答案時,要給予使用者提示(再大一點或再小一點),直到使用者猜對為止,該如何改寫程式呢? (提示:在while迴圈中加入if條件判斷敘述。) 程式碼: ```cpp= #include<iostream> #include <stdlib.h> #include <ctime> using namespace std; int main() { srand(time(NULL)); int x = rand()%10; int num; num = 0; while (num != x) { cout <<"請輸入猜測的數字"; cin >> num; if (num > x) cout << "太大了"; else if (num < x) cout << "太小了"; else cout << "猜中了是" <<x; } } ``` 執行畫面:  ### 05-28 2的n次方 ```cpp= #include<iostream> #include<math.h> using namespace std; int main() { int x; cout <<"請輸入要算的2次方"; cin >> x; cout << pow(2,x); } ``` 執行畫面:  ## 課後練習 ### 1.心理測驗 (if…else if 應用) ``` 發生船難,你選擇留下什麼? 選擇(1)留下老人和孕婦 選擇(2)留下老人和嬰兒 選擇(3)留下老人和金銀珠寶 選擇(4)留下孕婦和嬰兒 選擇(5)留下孕婦和金銀珠寶 選擇(6)留下嬰兒和金銀珠寶 輸入各選擇後回應如下: (1)你的情感很細膩,善於觀察細節。在感情中,相比於戀人的甜言蜜語,你更在乎對方實際的行動。 (2)在感情中,你不僅洞察力超好,第六感也很強,越是親近的人這種直覺越敏銳,所以另一半對你很難有所隱藏,因為你可以憑借著蛛絲馬跡得到你想要知道的消息。 (3)你是個典型的顏控,在擇偶時很注重另一半的外表和身高。 (4)面對感情你很承盾,一方面你很感性,渴望浪漫熱烈的愛情;另一方面你又很理性,明白現實的殘酷和金錢的重要性 (5)在感情方面你很挑剔,所以很難遇到心動的對象。不過在戀愛時你卻很專一,一旦喜歡上某個人,你就會全心全意的對他好,所以和你談戀愛是一件很幸福的事。 (6)在感情中你很缺乏安全感,有時候會忍不住通過試探或考驗的方式去確認對方是否愛你。 其他輸入: 亂輸入,活該一輩子舔狗命!!! ``` 執行畫面:  ```cpp= //資訊一乙 21 張家笛 #include <iostream> using namespace std; int main() { cout << "發生船難,你選擇留下什麼?\n"; cout << "選擇(1)留下老人和孕婦\n"; cout << "選擇(2)留下老人和嬰兒\n"; cout << "選擇(3)留下老人和金銀珠寶\n"; cout << "選擇(4)留下孕婦和嬰兒\n"; cout << "選擇(5)留下孕婦和金銀珠寶\n"; cout << "選擇(6)留下嬰兒和金銀珠寶\n"; int a; cin >> a; if (a==1) cout << "你的情感很細膩,善於觀察細節。在感情中,相比於戀人的甜言蜜語,你更在乎對方實際的行動。"; else if (a==2) cout << "在感情中,你不僅洞察力超好,第六感也很強,越是親近的人這種直覺越敏銳,所以另一半對你很難有所隱藏,因為你可以憑借著蛛絲馬跡得到你想要知道的消息。"; else if (a==3) cout << "你是個典型的顏控,在擇偶時很注重另一半的外表和身高。"; else if (a==4) cout << "面對感情你很承盾,一方面你很感性,渴望浪漫熱烈的愛情;另一方面你又很理性,明白現實的殘酷和金錢的重要性"; else if (a==5) cout << "在感情方面你很挑剔,所以很難遇到心動的對象。不過在戀愛時你卻很專一,一旦喜歡上某個人,你就會全心全意的對他好,所以和你談戀愛是一件很幸福的事。"; else if (a==6) cout << "在感情中你很缺乏安全感,有時候會忍不住通過試探或考驗的方式去確認對方是否愛你。"; else cout << "搞耍人??"; } ``` ### 2.判斷座標(x,y)在第幾象限。 (巢狀if…else 應用) 下列程式將讓使用者輸入x、y值,判斷(x, y)在第幾象限。若x與y皆為正數是在第一象限;皆為負數是在第三象限;若x為正數、y為負數,在第四象限;若x為負數、y為正數,則在第二象限。 執行畫面:  ```cpp= //資訊一乙 21 張家笛 #include <iostream> using namespace std; int main() { float x,y; cout << "請輸入x值(不可輸入0):"; cin >> x; cout << "請輸入y值(不可輸入0):"; cin >> y; if (x>0) if(y>0) cout << "(" << x << "," << y << ") 在第一象限"; else cout << "(" << x << "," << y << ") 在第四象限"; else if(y<0) cout << "(" << x << "," << y << ") 在第三象限"; else cout << "(" << x << "," << y << ") 在第二象限"; } ``` ### 3.用*組成三角形(for迴圈的應用) 輸入層數n即輸出層的*三角形。 執行畫面: ``` 請輸入層數:4 * ** *** **** **** *** ** * * ** *** **** **** *** ** * ``` 程式碼: ```cpp= #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; } } ``` ### 4.十二生肖查詢(switch…case的應用) 執行畫面:  ```cpp= //資訊一乙 21 張家笛 #include <iostream> using namespace std; int main() { int yy; cout << "請輸入民國(年):"; cin >> yy; switch (yy%12) { 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; } } ``` ### 5.閏年 除以 4 能整除,且除以 100 不能整除 如果剛好是 100 的倍數,且除以 400 能整除 舉例來說 2000 年是 100 的倍數且除以 400 能整除,所以是 2000 年是閏年,例如 2100 年雖然是 4 的倍數,但除以 100 能整除,所以 2100 年是平年。 執行畫面: ``` 請輸入一個西元年:2000 2000年是閏年 ``` ``` 請輸入一個西元年:1990 1990年非閏年 ``` 程式碼: ```cpp= #include <bits/stdc++.h> #include <bits/stdc++.h> using namespace std; int main(){ int y; cout<<"請輸入一個西元年:"; while(cin >> y){ if(y % 4 == 0 && y % 100 != 0) cout <<y<< "年是閏年\n"; else if(y % 400 == 0) cout <<y<< "年是閏年\n"; else cout <<y<< "年非閏年\n"; } } ``` ### 6.重複計算BMI(while 迴圈)(P.129)(必考) 執行畫面:  程式碼: ```cpp= #include <iostream> using namespace std; int main() { char go_again = 'Y'; float height, weight; while (go_again == 'Y' || go_again == 'y') cout<<"請輸入身高(cm):"; cin >>height; cout<<"請輸入體重(kg)"; cin >>weight; cout<<"你的BMI為:"<<weight/(height*height)*10000 <<endl; cout<<"do u want again?"<<"\n[y/n]"; cin>>go_again; return 0; } ``` ### 7. 計算兩整數的最大公因數(while 迴圈)(P.131)(20231023) 執行畫面:  程式碼: ```cpp= #include <iostream> using namespace std; int main() { int num1,num2,temp; cout<<"計算兩整數最大公因數\n"; cout<<"請輸入第一個數字:"; cin >>num1; cout<<"請輸入第二個數字:"; cin >>num2; while (num2!=0) { temp = num1%num2; num1 = num2; num2 = temp; } cout<<"最大公因數是:" << num1; } ``` ### 8. 密碼檢驗程式(P.133)(自行設計)(20231023) 請設計一個密碼輸入次數不可大於三次的密碼檢驗程式,密碼=123,當密碼輸入正確請顯示:歡迎進入本系統。密碼輸入錯誤3次,無法登入。 執行畫面:  程式碼: ```cpp= #include <iostream> using namespace std; int main() { int pwd, jst = 0; do { cout << "請輸入密碼: "; cin >> pwd; if (pwd != 123) { ++jst; cout << "已錯誤" << jst << "次" << endl; } if (jst >= 3) { cout << "密碼輸入錯誤3次,無法登入"; break; } } while (pwd != 123); if (pwd == 123) { cout << "歡迎進入本系統"; } return 0; } ``` ## 心得感想 透過學習C++的流程控制,我深刻體會到它在程式結構中的核心作用。if、else、for、while等結構的靈活運用,讓我能更有效地控制程式的執行流程。這種程式邏輯的掌握,使得我能夠更有力地處理不同情境,提升了程式碼的可讀性和效率。 <h1><center>2023/11/06 課堂作業 (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; } } ``` ## 心得感想 學習C++函式讓我體會到模組化的程式設計之美。透過函式,我能將程式分解成獨立且可重複使用的模塊,提升代碼的結構性和可維護性。函式的引入不僅使得程式更易讀,也提供了更好的程式邏輯組織方式,讓開發更有效率。我深刻體會到函式的價值,它為程式設計帶來了更高層次的抽象和可擴展性。 <h1><center>2023/12/04 課堂作業 (CH7陣列與指標)</center></h1> ## 隨堂練習 ### 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): 學習C++陣列與指標使我深刻體會到資料結構的重要性。陣列提供了有序的資料存放方式,而指標則能靈活地操作記憶體位置。這兩者的結合,使得我在處理大量資料時更加靈活高效。透過指標,我能夠直接存取記憶體位址,提高程式效能。陣列與指標的運用不僅擴展了我的程式能力,也加深了對資料處理的理解。
×
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