# (三) 05-07 (五) 06-08 程式設計 ###### tags: `111-1` `MCU課程筆記`、`程式設計`、`大一上` 章節 [TOC] ## 0907(三) 第一週 第1堂 教科書 ``` C++ 教學手冊(第三版) ISBN 9789577179371 ``` 成績計算 期中 30% + 其他(上機考、實習、小考…) 70% 課前先完成 Moodle 上的題目、影片 ### 電腦系統簡介 1. 硬體:看得到、摸得到的電腦設備 2. 軟體:看不到、摸不到但可由硬體顯示 > 終わる --- ## 0909(五) 第一週 第2堂 實習課 沒有紀錄 > 終わる --- ## 0914(三) 第二週 第3堂 ### 電腦系統簡介 硬體五大單元: - `Control Unit` - `ALU` - `Memory Unit` - `Input/Output Unit` 電腦的記憶體可分為主記憶體(Main memory)和輔助性記憶體(Seconary memory, 儲存裝置) - 主記憶體 CPU 可直接存取,可分為 RAM 與 ROM 兩種 1. RAM (Random access memory, 隨機存取記憶體) 為主記憶體中比例最高的記憶體,用於暫時存放位於輔助性記憶體中的資料 2. ROM (Read only memory, 唯讀記憶體) 用於存放開機程式 > 終わる --- ## 0916(五) 第二週 第4堂 實習課 Coding Frenzy 使用與安裝教學 - 前往 [瘋狂程設:自動閱卷的程式設計上機考試題庫暨考試系統](http://coding-frenzy.arping.me/) 下載檔案,並解壓縮至 `C:/` <!-- - 下載 MinGW 軟體,並架置 gcc 與 g++ 的編譯環境與設置環境變數 --> > 終わる --- ## 0921(三) 第三週 第5堂 實習課 - Git 使用教學 - 簡報:[mcu_sharing_git_101.pdf - Google 雲端硬碟](https://drive.google.com/file/d/1MBM2dmMyXcKmBNa0-L0VLTaf1lxU-4qH/view) <!-- {%pdf https://gitlab.com/test-repo8200/Test/-/raw/b9aa3c1fbe877951fa74c0c85d205b6cbe6338e7/mcu_sharing_git_101.pdf %} --> - C++ 基本 I/O 教學 > 終わる --- ## 0923(五) 第三週 第6堂 實習課 作業:跳脫字元、溫度換算 > 終わる --- ## 0928(三) 第四週 第7堂 ### 1.3 程式的規劃與實作 程式編譯過程中產生的檔案類型 ``` source file 原始檔(原始碼) .c .cpp file object file 目的檔(機器碼) .obj file execute file 執行檔 .exe file ``` 錯誤類型 - semantic error(語意錯誤):撰寫者在規劃程式時產生的錯誤。 - syntex error(語法錯誤):程式語言規範的語法的錯誤。如:未加分號、錯字等 變數可視為記憶體空間 <!-- 課本第一章流程圖教到變數型態int --> > 終わる --- ## 0930(五) 第四週 第8堂 實習課 流程圖繪製、C++格式化輸出 ### 流程圖符號 [![](https://i.imgur.com/WYycbtM.jpg)](https://i.imgur.com/WYycbtM.jpg) [![](https://i.imgur.com/hn7bJ8I.jpg)](https://i.imgur.com/hn7bJ8I.jpg) ###### [高師大 運算思維與程式設計、演算法及流程圖_公版教材](http://webnas.bhes.ntpc.edu.tw/wordpress/wp-content/uploads/2019/10/%E7%A8%8B%E5%BC%8F%E6%B5%81%E7%A8%8B%E5%9C%96%E6%95%99%E5%AD%B8%E5%85%AC%E7%89%88%E6%95%99%E6%9D%90%E4%BF%AE%E8%A8%82%E7%89%88v1.pdf) --- ### `iomanip`(input-output manipulators, I/O 操作子) - C++ 中用於操縱**字串顯示格式**與**數字進位顯示格式**的標頭檔 1. 字串格式 - `setw(int)`:設定目前輸出行的寬度 - `cout.width(int)`:設定目前輸出行的寬度 - `setfill(char)`:設定目前輸出行的填充內容 - `cout.fill(char)`:設定目前輸出行的填充內容 :::spoiler 範例 A ```cpp= // iomanip_header_example-a.cpp #include <iostream> #include <iomanip> using namespace std; int main(void) { cout << "\n"; cout << "123456789012" << endl; cout << "Hello Word" << endl; cout << setw(11) << "Excel" << endl; // 設定輸出行的預設寬度(自動填滿) cout << "\n"; cout << "1048,576" << endl; cout << setw(8) << setfill('-') << "1,024" << endl; // 設定預設寬度為 8,並選擇用 - 填充 cout << "\n\n"; cout << "Hello" << endl; cout.width(11); cout.fill(' '); cout << "World" << endl; // 另一種方法 cout << "\n"; return 0; } ``` ::: 範例 A 輸出結果: ```= 123456789012 Hello Word Excel 1048,576 ---1,024 Hello World ``` --- 2. 數字格式 - `setbase(int)`:設定輸出基數 - `cout.setf(ios::<base>, ios::basefield)`:指定進位方式來設定基數 :::spoiler 範例 B ```cpp= // iomanip_header_example-b.cpp #include <iostream> #include <iomanip> using namespace std; int main(void) { cout << "\n"; cout << "convert dec(26) to Binary: " << setbase(2) << 26 << endl; // 設定基數為 2 cout << "convert dec(26) to Octal: " << setbase(8) << 26 << endl; // 設定基數為 8 cout << "convert dec(26) to Hexadecimal: " << setbase(16) << 26 << endl; // 設定基數為 16 cout << "\n\n"; // cout.setf(ios::bin, ios::basefield); // cout << "convert dec(26) to Binary: " << 26 << endl; // 設定基數為 2,不知道為甚麼會報錯 cout.setf(ios::oct, ios::basefield); cout << "convert dec(26) to Octal: " << 26 << endl; // 設定基數為 8 cout.setf(ios::hex, ios::basefield); cout << "convert dec(26) to Hexadecimal: " << 26 << endl; // 設定基數為 16 // 另一種方法 cout << "\n"; return 0; } ``` ::: 範例 B 輸出結果: ```= convert dec(26) to Binary: 26 convert dec(26) to Octal: 32 convert dec(26) to Hexadecimal: 1a convert dec(26) to Octal: 32 convert dec(26) to Hexadecimal: 1a ``` --- 作業:使用流程圖設計判斷成績是否及格的程式、格式化輸出字串與數字 <!-- 格式化輸出and流程圖 --> > 終わる --- ## 1005(三) 第五週 第9堂 ### 第三章 變數與資料處理 - Variable definition(變數宣告):在記憶體中配置一塊空間 - Variable assignment(設定變數):將資料更新到記憶體中 - Data type(資料型態):記憶體空間的特性 --- 常用 ASCII code table | Char | Dec | Hex | |:----:|:---:|:----:| | '0' | 48 | 0x30 | | 'A' | 65 | 0x41 | | 'a' | 97 | 0x61 | --- char 型態也可視為一個範圍為 0~255 的整數型態變數 - ASCII(American Standard Code for Information Interchange, 美國資訊交換標準碼) - ANSI(American National Standards Institue, 美國國家標準協會) - 跳脫字元(escape character) ##### 參考資料 ###### C++ 資料型態表:[Data Type Ranges | Microsoft Learn](https://learn.microsoft.com/en-us/cpp/cpp/data-type-ranges?view=msvc-170) > 終わる --- ## 1007(五) 第五週 第10堂 實習課 ### `iomanip`(I/O 操作子) — 浮點數操作 - `cout.precision(int)`:設定顯示的位數(浮點數精確度) - `cout.precision()`:顯示設定的浮點數精確度 - `cout.setf(ios::showpoint)`:設定顯示浮點數 - `cout.setf(ios::fixed, ios::floatfield)`:設定以小數顯示浮點數 - `cout.setf(ios::scientific, ios::floatfield)`:設定以指數顯示浮點數 - `cout.setf(ios::left/right, ios::adjustfield)`:設定輸出的對齊方向(左/右) - `cout.setf(ios::showpos)`:設定在前方顯示正號 :::spoiler 範例 C ```cpp= // iomanip_header_example-c.cpp #include <iostream> #include <iomanip> using namespace std; int main(void) { cout << "\n"; float pi = 3.14159; // cout.setf(ios::showpos); // 設定顯示正號 cout.setf(ios::left, ios::adjustfield); // 設定輸出對齊方向 cout << std::noshowpos << "precision: " << cout.precision() << endl; // 輸出浮點數精確度,且不顯示正號 cout << std::showpos << setfill('-') << setw(10) << pi << endl; cout << "\n"; cout.precision(3); cout << std::noshowpos << "precision: " << cout.precision() << endl; cout << std::showpos << setfill('-') << setw(10) << pi << endl; cout << "\n"; return 0; } ``` ::: 範例 C 輸出結果 ```= precision: 6 +3.14159-- precision: 3 +3.14----- ``` 作業:輸出浮點數與浮點數相減後的值、對齊與填滿輸出行、以 ASCII 碼輸出字串 > 終わる --- ## 1012(三) 第六週 第11堂 ### 第四章 運算子、運算式與敘述 運算式(expression)由運算元(operand)和運算子(operator)組成 #### 一元運算子 - +:取正數 - -:取負數 - !:NOT #### 計算運算子 - \+ - \- - \* - \/ #### 邏輯運算子 - &&:AND - ||:OR - !:NOT #### 關係運算子 - \> - < - \>= - <= - == - != #### 位元運算子 - ~:位元 NOT #### 括號運算子 - ():改變優先順序 --- #### if statement ``` if (boolean expression) { sequence of statements } ``` #### if-else statement ``` if (boolean expression) { sequence of statements } else { ... } ``` #### else if statement ``` if (boolean expression) { sequence of statements } else if (boolean expression) { ... } else { ... } ``` ##### 參考資料 ###### C++ Operator 優先順序:[C++ 內建運算子、優先順序和關聯性 | Microsoft Learn](https://learn.microsoft.com/zh-tw/cpp/cpp/cpp-built-in-operators-precedence-and-associativity?view=msvc-170#c-operator-precedence-and-associativity-table) ###### 敘述式 statement:[Conditionals and Loops](https://introcs.cs.princeton.edu/java/13flow/) > 終わる --- ## 1015(五) 第六週 第12堂 實習課 作業:計算運算子與平方、求立方與使用半徑求圓面積、使用半徑求球體積與求矩形周長 > 終わる --- ## 1019(三) 第七週 第13堂 ### 第五章 選擇性敘述與迴圈 程式語言的結構(structure) 1. 循序性結構(sequence structure) 2. 選擇性結構(selection structure) 3. 重複性結構(iteration structure) #### for loops statement 條件式結果為 True 時則執行,否則結束迴圈 ```! A: declare and initialize a loop control variable B: loop-continuation condition C: increment for (A; B; C) { ... } ``` ## 1021(五) 第七週 第14堂 實習課 for 迴圈複習、while 迴圈簡介 #### while loops statement 條件式結果為 True 時則執行,否則結束迴圈 ```! while (loop-continuation condition) { ... } ``` --- 作業:判斷一數為偶數或奇數、計算 BMI > 終わる --- ## 1026(三) 第八週 第15堂 #### do while loops statement 先執行內容,再進行判斷。若判斷結果為 True 則重新執行,否則結束迴圈 ```! do { ... } while (expression); ``` #### break statement 與 if statement 搭配使用,用於中斷目前的迴圈 #### continue statement 與 if statement 搭配使用,用於跳過目前的迴圈,重新進行判斷 #### switch statement 使用字元、數字進行條件判斷的敘述式,運算式結果不可為布林值 ```! switch (expression) { case x: ... break; case y: ... break; case z: ... break; default: ... } ``` condition 條件 expression 運算 **\*\*\* char 型態資料與整數進行運算時,會轉換為整數型態 \*\*\*** 範例 ```!cpp int main(void) { char PI = 3; cout << "'" << PI << "' "; cout << "'" << int(PI) << "' "; cout << "'" << PI+0.14 << "' "; return 0; } ``` 輸出結果 ```! '' '3' '3.14' ``` > 終わる --- ## 1029(五) 第八週 第16堂 實習課 --- ## 1102(三) 第九週 段考週 > --- ## 1104(五) 第九週 段考週 > --- ## 1109(三) 第十週 第17堂 ### 第六章 函數 ```cpp 傳回資料型態 傳入資料型態 | | int add (int, int); | 函數名稱 ``` example ```cpp void cowsay (string); ``` > 終わる --- ## 1111(五) 第十週 第18堂 實習課 寫題目,while loop, for loop, switch case > 終わる --- ## 1116(三) 第十一週 第19堂 ### 第六章 函數 Actual Parameter:在 main function中使用的資料 Formal Parameter:在定義 function 時,function 內使用的變數 call by value:將資料的值傳進 function 中 call by pointer(call by address):將資料的位址傳進 function 中 --- 使用 function 會使執行效能降低 $\because$ 需在 function 間切換 -> 節省記憶體 **inline function**:將 function 在 main function 中還原 -> 提升效能,增加記憶體使用