# C++程式設計 僑光科技大學 資訊科技系 114學年度第2學期 開課班級:日資科114忠、114孝 上課時間:星期二第2-4節、第5-7節 課程學分:必修3學分 授課老師:高吉隆 ###### tags: `課程大綱` ---- ## 課程介紹 * 課程大綱 * 教學目標 * 每週目標 * 成績計算 ---- ### 課程大綱 * Chap1:操作環境、程式撰寫 * Chap2:變數、資料型態與運算子 * Chap3:選擇結構、Chap4:多向選擇結構 * Chap5:迴圈結構、Chap6:進階迴圈概念 * Chap7:陣列、Chap8:二維陣列 * Chap9:搜尋與排序 * Chap10:自訂函數與遞迴 * Chap11:字串處理 * Chap12:系統函數、檔案輸出 * Chap13:位址與指標、Chap14:結構 ---- ### 教學目標 * 透過C++程式語言認識程式撰寫的技巧 * 透過程式範例的閱讀與實作增進程式能力 * 透過AI工具協助初學者更快了解程式技巧 ---- |週| 章|前 9 週上課目標 |作業|考試| |-|-|--------------- |-|-| |1|1|操作環境、程式撰寫| |2|2|變數、資料型態與運算子| |3|3|選擇結構| |4|4|多向選擇結構|1-2章|1-2章 |5|5|迴圈結構|| |6|6|進階迴圈概念|3-4章|3-4章 |7|7|陣列 | |8| |移至06/13(畢業典禮) | |9| |期中考週 |5-6章|5-6章 ---- |週| 章|後 9 週上課目標 |作業|考試| |:-:|-|--------------- |-|-| |10|8 |二維陣列| |11|9 |搜尋與排序| |12|10|自訂函數與遞迴|7-8章|7-8章 |13|11|字串處理| |14|12|系統函數、檔案輸出|9-11章|9-11章 |15|13|位址與指標| |16|14|結構|| |17||期末考週|12-14章|12-14章 |18||期末補救週| ---- ## 評分標準 * 期中成績:44% * 上課演練:14%(2%/章) * 作業1-3:12%(4%/次) * 考試1-3:18%(6%/次) * 期末成績:56% * 點名紀錄:12%(1%/次) * 上課演練:14%(2%/章) * 作業4-6:12%(4%/次) * 考試4-6:18%(6%/次) ---- ### 教科書 * 名稱:C++程式設計入門(第二版) * 出版社:碁峰 * 作者:黃建庭 ![image](https://hackmd.io/_uploads/rkx3pEou-l.png =300x400) ---- ### 上課方式 * 上課講解:投影片教學 * 因書籍著作權因素,不提供投影片 * 課堂點名:配合學校政策(12%) * 依照學校系統課程缺曠紀錄計算 * 課中演練:隨堂範例說明(28%) * 下課前繳交線上平台隨堂範例練習 * 課後作業:上機程式測驗、課後評量(24%) * 於指定日期前繳交至僑光教學平台 * 隨堂考試:上機程式測驗、課後評量(36%) * 於指定日期上課時間考試 ---- ### 上課講解 * 投影片上課,但不提供投影片 * 教學、作業及考試的題目都出自書籍 * 為提高教學效果,建議同學都能買書 * 同學可共用1本書,或使用二手書 * 教科書統一訂購:原價490元 * 團購價:75折為368元 ---- ### 上課點名 * 依學校點名系統計分方式計算,佔總分10% * 若該門課無任何請假、缺曠,可拿100分 * 計分方式:3節x18週=54堂 * 原則上3節課只點1節,通常是第2或3節 * 點名缺曠每1節扣1.5點,請假扣1點 * 若無法到課,請務必請假,一律通過 * 累積達2/9即扣12點,即達<預警> * 若扣12點,約可拿78分 * 累積達1/3即扣18點,即達<扣考> * 若扣18點,約可拿67分 * 若扣超過18點,不再點名,分數為0 ---- ### 課中演練 * https://www.programiz.com/cpp-programming/online-compiler/ * 請至以下網址註冊或登入:[Code Judger](https://www.codejudger.com/) * 請使用OCU帳號: * 帳號:s111XXXXX@ocu.edu.tw * 密碼:初始值為19911223 * 進入後請修改密碼 ---- ### 課後作業 * 每2-3章1作業,每1位同學都寫10題 * 上機程式測驗:按題號寫1題 * 程式碼(30%):請於每行程式加上註解 * 出版社附程式範例:[下載](https://download.drmaster.com.tw/download/example/MM32203_examples.zip) * 執行結果截圖(20%):請紀錄執行過程 * 流程圖(20%):請繪製完整流程圖 * 第2題:課後評量,按題號寫1題 * 課本中的答案(30%):幾乎都找得到 * 第1-2、3-4、5-6、7-8、9-11、12-14章各交1次 * 每次作業佔5%:請以Word編輯,並轉成PDF檔 * 題目公告、作業繳交:請至[僑光教學平台](http://wis.ocu.edu.tw/mooc/index.php) ---- ### 階段考試 * 第1-2、3-4、5-6、7-8、9-11、12-14章各考1次 * 題目來源:上機程式測驗和課後評量 * 題目類型:滿分120分 * 4選1單選題:約80分 * 如40題,每題2分 * 程式填空題:約40分 * 如4題,每題10分 * 依班上平均分數每人加相同分數至平均75左右 * 超過100分,以100分計算,超過部分累積他次 --- ## Visual Studio Code 安裝 * 安裝VSC編輯器:[下載](https://vscode.download.prss.microsoft.com/dbazure/download/stable/072586267e68ece9a47aa43f8c108e0dcbf44622/VSCodeUserSetup-x64-1.109.5.exe)後,並安裝 * 安裝C++編譯器 TDM-GCC:[下載](https://github.com/jmeubank/tdm-gcc/releases/download/v10.3.0-tdm64-2/tdm64-gcc-10.3.0-2.exe)後,並安裝 * 選擇Create,並選擇MinGW-w64 * 路徑選擇至:C:\TDM-GCC-64 * 重新啟動VSC,建立新目錄:C:\C++ * 新增檔案,輸入以下程式碼,儲存為A.cpp ```c= #include<iostream> int main() { std::cout << "Hello World!"; return 0; } ``` * 按【F5】執行,選擇C++(GDB/LLDB), * 選擇C:\TDM-GCC-64\bin\g++.exe ---- ## 教學示範 * 網址:[http://gg.gg/12leyf](https://hackmd.io/_5vr5boOTxeFmn6ikN_bjg) ---- ### 課中演練 CH01_01.cpp * 請使用C++程式輸出以下三行字串! 我在僑光科技大學 資訊科技系的 第一個C++語言程式 ```c= #include <iostream> using namespace std; int main() { cout << "我在僑光科技大學" << endl; cout << "資訊科技系的" << endl; cout << "第一個C++語言程式"; return 0; } ``` --- ### 課中演練 CH02_01.cpp * 請於程式中宣告2個整數變數: 1. 變數名稱為a,無初始值。 2. 變數名稱為b,初始值設為12。 * 並將以上2變數顯示如下: * 變數a=0 * 變數b=12 ```c= #include <iostream> using namespace std; int main() { int a; int b = 12; cout << "變數a=" << a << endl; cout << "變數b=" << b; return 0; } ``` ---- ### 課中演練 CH02_02.cpp 請於程式中宣告2個變數,並顯示所佔位元組: | 類型 | 變數名稱 | 初始值 | |-|-|-| | 短整數 | number1 | 0200(八進位) | | 整數 | number2 | 0x33f(十六進位) | 並將以上2變數顯示如下: 短整數=128所佔位元組:2 整數=831所佔位元組:4 ---- ```c= #include<iostream> using namespace std; int main() { short int number1=0200;//宣告短整數變數,並以八進位數設定其值 int number2=0x33f;//宣告整數變數,並以十六進位數設定其值 cout<<"短整數="<<number1<<"所佔位元組:"<<sizeof(number1)<<endl; cout<<"整數="<<number2<<"所佔位元組:"<<sizeof(number2)<<endl; return 0; } ``` ---- ### 課中演練 CH02_03.cpp 請於程式中宣告2個變數,並顯示所佔位元組: 1. 類型為單精確度浮點數,變數名稱為Num1,初始值設為1.742f 2. 類型為倍精確度浮點數,變數名稱為Num2,初始值設為4.159 並將以上2變數顯示如下: 單精確度浮點數=1.742所佔位元組:4 倍精確度浮點數=4.159所佔位元組:8 ---- ```c= #include <iostream> using namespace std; int main() { float Num1 = 1.742f; // 宣告float變數 double Num2 = 4.159; // 宣告double變數 cout<<"單精確度浮點數="<<Num1<<"所佔位元組="<<sizeof(Num1)<<endl; cout<<"雙精確度浮點數="<<Num2<<"所佔位元組="<<sizeof(Num2)<<endl; return 0; } ``` ---- ### 課中演練 CH02_04.cpp 請於程式中宣告2個變數,並顯示所佔位元組: 1. 類型為字元,變數名稱為Char1,初始值設為0xA 2. 類型為字元,變數名稱為Char2,初始值設為'\\' 並將以上2變數顯示如下: 字元= 所佔位元組:1 字元=\\所佔位元組:1 ---- ```c= #include <iostream> using namespace std; int main() { char Char1=0xA;//換行的十六進位ASCII碼 char Char2='\\';//反斜線的跳脫字元 cout<<"字元="<<Char1<<"所佔位元組:"<<sizeof(Char1)<<endl; cout<<"字元="<<Char2<<"所佔位元組:"<<sizeof(Char2)<<endl; return 0; } ``` ---- ### 課中演練 CH02_05.cpp 請於程式中宣告2個變數,並顯示所佔位元組: 1. 類型為布林,變數名稱為Bool1,初始值設為10 2. 類型為布林,變數名稱為Bool2,初始值設為5 > 8 並將以上2變數顯示如下: 布林=1所佔位元組:1 布林=0所佔位元組:1 ---- ```c= #include <iostream> using namespace std; int main() { bool Bool1= 10; bool Bool2= 5 > 8; cout<<"布林="<<Bool1<<"所佔位元組:"<<sizeof(Bool1)<<endl; cout<<"布林="<<Bool2<<"所佔位元組:"<<sizeof(Bool2)<<endl; return 0; } ``` --- ### 課中演練 CH03_01.cpp 請於程式中宣告2個整數變數,並分別用算術運算子顯示各種運算結果: 1. 變數名稱為A,初始值設為21 2. 變數名稱為B,初始值設為6 並將以下2個運算結果顯示如下: A=21, B=6 A/B=3 A%B=3 ---- ```c= #include<iostream> using namespace std; int main() { int A = 21, B = 6; cout << "A=" << A << ", B=" << B << endl; cout << "A/B=" << A/B << endl; cout << "A%B=" << A%B << endl; return 0; } ``` --- ### 課中演練 CH03_02.cpp 請於程式中宣告2個整數變數,並分別用關係運算子顯示各種運算結果: 1.變數名稱為A,初始值設為11 2.變數名稱為B,初始值設為15 並將以下2個運算結果顯示如下: A=11, B=15 A!=B=1 A>=B=0 ---- ```c= #include<iostream> using namespace std; int main() { int A = 11, B = 15; cout << "A=" << A << ", B=" << B << endl; cout << "A!=B=" << (A!=B) << endl; cout << "A>=B=" << (A>=B); return 0; } ``` ---- ### 課中演練 CH03_03.cpp 請於程式中宣告2個整數變數,並分別用位元運算子顯示各種運算結果: 1.變數名稱為A,初始值設為13 2.變數名稱為B,初始值設為57 並將以下2個運算結果顯示如下: A=13, B=57 A&B=9 A|B=61 ---- ```c= #include<iostream> using namespace std; int main() { int A=13, B=57; cout << "A=" << A << ", B=" << B << endl; cout << "A&B=" << (A&B) << endl; cout << "A|B=" << (A|B); return 0; } ``` ---- ### 課中演練 CH03_04.cpp 請於程式中宣告2個整數變數,並分別用遞增或遞減運算子顯示各種運算結果: 1.變數名稱為A,初始值設為5 2.變數名稱為B,無初始值 經過以下2個運算後,顯示A, B變數值: ```c= B=A++ B=++A ``` 將A, B值顯示如下: A=5, B=0 B=A++, A=6, B=5 B=++A, A=7, B=7 ---- ```c= #include<iostream> using namespace std; int main() { int A=5, B; cout << "A=" << A << ", B=" << B << endl; B=A++; cout << "B=A++, A=" << A << ", B=" << B << endl; B=++A; cout << "B=++A, A=" << A << ", B=" << B << endl; return 0; } ``` ---- ### 課中演練 CH03_06.cpp 請於程式中宣告4個整數變數,計算出成績的總分及平均,並使用強制資料型態轉換,顯示運算結果,含小數位數: 1.變數名稱為sorce1,初始值設為78 2.變數名稱為sorce2,初始值設為69 3.變數名稱為sorce3,初始值設為92 4.變數名稱為sum,初始值設為0 計算總分及平均後,顯示數值,並包括小數: sum=239 average=79.6667 ---- ```c= #include<iostream> using namespace std; int main() { int score1 = 78, score2 = 69, score3 = 92; int sum =0; sum = score1 + score2 + score3; cout << "sum=" << sum << endl; cout << "average=" << (float) sum/3; return 0; } ``` --- ### 課中演練 CH04_01.cpp ```c= #include <iostream> using namespace std; int main() { int score = 0; cout << "請輸入國文成績:" << endl; cin >> score; if(score >= 60) cout << "本科成績及格。" << endl; else cout << "本科成績不及格。" << endl; return 0; } ``` ---- ### 課中演練 CH04_02.cpp ```c= #include <iostream> using namespace std; int main() { int Score; cout << "輸入學生的分數:" << endl; cin >> Score; if (Score > 100) cout << "超過 100 分" << endl; else if (Score < 0) cout << "不會有負的分數吧" << endl; else if (Score >= 60) cout << "不錯喔,得到 " << Score << " 分"; else cout << "不太理想喔,只得了 " << Score << " 分"; return 0; } ``` ---- ### 課中演練 CH04_04.cpp ```c= #include <iostream> using namespace std; int main() { char select; cout << "(A)義大利" << endl; cout << "(B)巴黎" << endl; cout << "(C)日本" << endl; cout << "請輸入你要旅遊的地點:" << endl; cin >> select; switch(select) { case 'a': case 'A': cout<<"義大利5日遊$35000" << endl; break; case 'b': case 'B': cout<<"巴黎7日遊$40000" << endl; break; case 'c': case 'C': cout<<"日本5日遊$25000" << endl; break; default: cout<<"選項錯誤" << endl; } return 0; } ``` ---- ### 課中演練 CH04_05.cpp ```c= #include <iostream> using namespace std; int main() { int i, n, sum=0; cin >> n; for(i = 1; i <= n; i++) sum += i; cout << "1+2+...+" << n << "=" << sum; return 0; } ``` ---- ### 課中演練 CH04_06.cpp ```c= #include <iostream> using namespace std; int main() { int i, j; for (i = 1; i <= 9; i++) { for (j = 2; j <= 9; j++) { cout << j << '*' << i << '=' << j * i << ' '; } cout << endl; } return 0; } ``` ---- ### 課中演練 CH04_08.cpp ```c= #include <iostream> using namespace std; int main() { int sum = 0, i = 0, S = 0; cout << "請問連續整數加總到多少會超過S值,請輸入S值:" << endl; cin >> S; do { i ++; sum += i; } while(sum <= S) ; cout << "連續整數加總到" << i << "會超過" << S; return 0; } ``` --- ### 課中演練 CH05_01.cpp ```c= #include <iostream> using namespace std; int main() { int score[6] = {50, 80, 70}; for (int i = 3; i < 6; i++) { cout << "第" << i + 1 << "個元素為:" << endl; cin >> score[i]; } for (int i = 0; i < 6; i++) cout << "score[" << i << "]=" << score[i] << endl; return 0; } ``` ---- ### 課中演練 CH05_02.cpp ```c= #include <iostream> using namespace std; int main() { int score[4][5], fail[4] = {0}, sum[4] = {0}; for (int i = 0; i < 4; i++) { cout << "請輸入" << i + 1 << "號的國、英、數、自、社五科成績:" << endl; for (int j = 0; j < 5; j++) { cin >> score[i][j]; sum[i] += score[i][j]; if (score[i][j] < 60) fail[i] += 1; } } for (int i = 0; i < 4; i++) cout << i + 1 << "號的總分:" << sum[i] << ",平均:" << sum[i] / 5.0 << ",共有" << fail[i] << "科不及格" << endl; return 0; } ``` ---- ### 課中演練 CH05_05.cpp ```c= #include <iostream> using namespace std; int main() { char Str[6][30] = {"張繼 楓橋夜泊", "==============", "月落烏啼霜滿天", "江楓漁火對愁眠", "姑蘇城外寒山寺", "夜半鐘聲到客船"}; int i; for (i = 0; i < 6; i++) cout << Str[i] << endl; return 0; } ``` ---- ### 課中演練 CH05_06.cpp ```c= #include <iostream> #include <string> using namespace std; int main() { char ch[] = ", "; string firstname; //宣告字串類別 string lastname; //宣告字串類別 string input1("請輸入姓氏:"); string input2 = "請輸入名字:"; cout << input1 << endl; cin >> lastname; //輸入字串 cout << input2 << endl; cin >> firstname; //輸入字串 string fullname = firstname + ch + lastname; //以運算子作字串的串接 cout << "您的全名為:" << fullname << endl; return 0; } ``` ---- ### 課中演練 CH05_08.cpp ```c= #include <iostream> #include <string> using namespace std; int main() { string str1 = "Overseas Chinese University"; string key = ""; cout << "請在以下字串\"" << str1 << "\",要找的字串:" << endl; cin >> key; size_t pos = str1.find(key); if (pos != str1.npos) cout << "在字串的第" << pos << "個位置找到" << key << "字串"; else cout << "找不到!"; return 0; } ``` ---- ### 課中演練 CH07_02.cpp ```c= #include <iostream> using namespace std; int my_pow(int x, int r) { int i, sum = 1; for (i = 0; i < r; i++) sum = sum * x; return sum; } int main() { int x, r; cout << "請輸入兩個數字:" << endl; cin >> x; cin >> r; cout << x << "的" << r << "次方=" << my_pow(x, r) << endl; //呼叫my_pow()函數 return 0; } ```
{"metaMigratedAt":"2023-06-17T07:12:19.287Z","metaMigratedFrom":"Content","breaks":true,"description":"僑光科技大學 資訊科技系111學年度第1學期開課班級:日資科111孝上課時間:星期四第2-4節課程學分:必修3學分授課老師:高吉隆","title":"C++程式設計","contributors":"[{\"id\":\"9eed60a5-6546-4dfd-8445-07f81bcfde52\",\"add\":16748,\"del\":4668,\"latestUpdatedAt\":1772185587065}]"}
    918 views