Try   HackMD

實習課第六次作業

tags: NTOU CSE C++ Programming

本次作業主題是 C++ 的模板。

教學文件和作業說明文件: https://hackmd.io/@kogiokka/ntou-cse-cpp-nav

範例程式和範例專案:Google Drive

參考資料

題目一、function template

用 function template 改寫函式,讓數列陣列 array 的類別可變化。

請改寫下列函式宣告,並撰寫函式的定義:

/**
 * 找出陣列中的最大值。
 *
 * @param array  儲存一個數列的陣列
 * @param size   陣列長度
 * @return       陣列中的最大值
 */
int max(int array[], int size);

對應的範例程式:

  • template_function.cpp

測試案例

案例1

  • int a1[] = {1, 2, 3, 5, 4};

案例2

  • double a2[] = {1.2, 3.4, 5.6, 2.3, 4.5};

題目二、以 template 改寫實習五的 stack (array 版本)

用 struct template 寫一個 stack(array 版本)。template 接受 2 個參數:

  1. stack data type
  2. stack 大小

利用 function template 定義下列函式:

函式名稱 回傳值型態 回傳值意義
push bool true 代表成功,false 代表失敗。
pop bool true 代表成功,false 代表失敗。
isEmpty bool true 代表成功,false 代表失敗。
isFull bool true 代表成功,false 代表失敗。
printStack void 無回傳值

對應的範例程式:

  • template_function.cpp
  • template_struct.cpp

測試案例

案例1

  • stack data type: int
  • stack 大小: 20

案例2

  • stack data type: float
  • stack 大小: 15

案例3

依序執行下列步驟:

  • isEmpty
  • isFull
  • push: 15 data (1 - 15)
  • isEmpty
  • isFull
  • printStack
  • pop: 15 data
  • isEmpty
  • isFull
  • printStack

作業上傳規定

格式:<學號>-<姓名>.zip,例如:11057022-江柏毅.zip

如果你是用 Visual Studio,請刪除下列資料夾再上傳:.vsoutDebugx64
使用其他 IDE 的同學也請刪掉建置資料夾再上傳,例如:buildout