# 113-2 NCKU Program Design-II Homework 5
:::warning
本次作業延續 Homework 3 的內容,將整個簡易版的 Online Judge 系統完成
請直接繼續使用你 Homework 3 的 code
:::
## Problem.cpp
### Problem.h
```cpp=
#include <iostream>
class Problem {
protected:
std::string problem_title;
std::string input_path;
std::string output_path;
int magic_number;
public:
Problem() = default;
Problem(std::string title, std::string input, std::string output, int magic_num);
std::string getTitle() const { return problem_title; }
};
class ProblemSystem : public Problem {
private:
std::vector<Problem> problem_list;
void adding_problem(Problem new_problem);
public:
void init(std::string &PROBLEM_DATA_PATH);
std::vector<Problem>* list_problem();
void newproblem_set(std::string PROBLEM_DATA_PATH);
};
```
### Subtask 1 - Constructor
請把 Constructor 完成,將對應的參數給予正確的 class member
```cpp=
Problem::Problem(std::string title, std::string input, std::string output, int magic_num) {
// title: problem_title
// input: input_path
// output: output_path
// magic: magic_number
}
```
### Subtask 2 - init
將 `Problem::init` 完成,此 Function 的功能為讀取 `PROBLEM_DATA_PATH` 作為檔案路徑,並將該檔案中的每一筆資料分割出 `title`、`input_path`、`output_path`、`magic_number`,並呼叫 `ProblemSystem::adding_problem` 傳入對應參數。
```cpp=
void ProblemSystem::init(std::string &PROBLEM_DATA_PATH) {
// TODO: Loading problem data from PROBLEM_DATA_PATH
std::ifstream file(PROBLEM_DATA_PATH);
if( !file.is_open() ) {
std::cerr << "Error\n";
std::cerr << "Please check your csv file in " << PROBLEM_DATA_PATH << "\n";
exit(1);
}
std::string readline;
while( getline(file, readline) ) {
std::string title, input_path, output_path, magic_num;
// TODO: spilt by char ',' (Hints: stringstream)
Problem new_problem(title, input_path, output_path, stoi(magic_num));
ProblemSystem::adding_problem(new_problem);
}
}
```
### Subtask 3 - list_problem
return `ProblemSystem::problem_list`
```cpp=
std::vector<Problem>* ProblemSystem::list_problem() {
// TODO: retrun the correct variable
}
```
### Subtask 4 - newproblem_set
引導使用者新增一個題目
:::warning
題目名稱可能會有空白字元,會產生某些問題,你必須做一些特別的處理 (如果你的程式沒有把這個問題處理掉,在題目名稱有空白字元的情況下會出現一些 bug)
:::
```cpp=
void ProblemSystem::newproblem_set(std::string PROBLEM_DATA_PATH) {
// write code here
try { // update problem data
std::ofstream info_out(PROBLEM_DATA_PATH);
if( !info_out ) {
throw std::runtime_error("Error: File does not exist - " + PROBLEM_DATA_PATH);
}
for( Problem problem : problem_list ) {
info_out << problem.getTitle() << "," << problem.getInput() << "," << problem.getOutput() << "," << problem.getNum() << "\n";
}
info_out.close();
}
catch (const std::exception& e) {
std::cerr << "Exception caught: " << e.what() << "\n";
}
}
```
#### Sample

### Subtask 5 - Submit code
:::warning
此部分的功能你可以自行決定要實作在哪,自己創檔案也可以
:::
接下來我們要將 Online Judge 的功能實作出來,要求使用者輸入題目編號與程式碼路徑,我們必須給予結果。
流程:
1. 請使用者輸入題目編號與程式碼路徑
2. 編譯使用者程式,如果編譯失敗則給予 `Compiler Error`
3. 讀取該題目的輸入,並丟給該使用者的程式,拿取輸出結果
4. 檢查輸出結果與該題正確答案是否一致,如果一致給予 `Accepted`,反之給予 `Wrong Answer`
5. 給予結果後,直接結束程式

### Subtask 6 - 將 Option 3 (List all problem) 實作完成
#### Example

### Subtask 7 - Random Problem (加分題)
請自行發揮要如何提供給使用者隨機題目的功能 (可以利用 Magic Number,也可以不用)
此部分會依照實作難易度與創意度加分 (0 ~ 20) 分
## Demo
此作業一樣採取 Demo 錄影的方式,錄影方式可以參考下方影片,下方影片也可以更了解這次要完成的功能。
範例影片與解說:https://youtu.be/gNk7LOw5tQ0