# C++ 練習題
###### tags: `tutor` `C++`
本文所有內容與資料皆由本人蒐集與撰寫,轉載請註明出處。
- [C++ 教學講義](https://hackmd.io/C7kIxIuJQImM_x64fhuM_A?view)
- 亦可參考 [ZeroJudge](https://zerojudge.tw/Problems)、[Leetcode](https://leetcode.com/problemset/all/)
## If-else 與基本語法練習
- BMI 計算機
撰寫一個程式,讀入使用者的身高、體重
並判斷使用者為過輕、正常、或異常
若異常則再判斷為過重、輕度、中度、或重度
BMI = 體重(公斤) / 身高^2(公尺)

- 三數比大小
撰寫一個程式,讀入三個數字
兩兩比對,判斷三個數字大小,並輸出結果
舉例:
輸入3、6、4,輸出6>4>3
輸入6、6、5,輸出6=6>5
## 迴圈練習
- 菱形
給定輸入整數n,印出高度為2n的菱形
可以用任何符號代替星號,其他的地方為空白

- 九九乘法表(Hint:雙重迴圈)
印出九九乘法表,順序為:1x1=1,1x2=2,1x3=3 ... 9x9=81
## 函數
- 歐幾里得距離(Hint:函數預設值)
撰寫一個函數,輸入為 [x1,y1] 或 [x1,y1], [x2,y2]
當只傳入一組座標時就計算其與原點距離
傳入兩組座標時計算該兩點距離
例:
輸入為 [x1,y1] ,回傳該點與原點距離
輸入為 [x1,y1] 與 [x2,y2],回傳該兩點距離
```cpp=
double euclidean_distance(int x1, int y1, int x2 = 0, int y2 = 0){
// Your implementation here
}
```
- 歐幾里得距離延伸:比較與原點的遠近(Hint:在函數內使用函數)
使用上題的函數,撰寫一個函數,輸入為 [x1,y1] 與 [x2,y2]
回傳 True 若第一點較近或一樣近,False 若第二點較近
```cpp=
bool compare_distance(int x1, int y1, int x2, int y2){
// Your implementation here
// You should invoke euclidean_distance()
}
```
## Struct
- 將歐幾里得函數中,(x, y) 整理成一個結構,並針對函數做相應的修改
結果應該會長得像這樣:
```cpp=
struct point{
// Your implementation here
}
double euclidean_distance(point p1, point p2 = {0,0}){
// Your implementation here
}
point compare_distance(point p1, point p2){
// Your implementation here
// You should invoke euclidean_distance()
// Return the closer one, if equal return p1
}
```
## Pointers
- 實作以下函式,並實際使用以測試正確性:
```cpp=
int findMax(int* arr, int size){
// This function finds maximum item in an array, returns its index
}
int findElement(int* arr, int size, int target){
// This function finds target in an array, returns its index
}
void copyArray(int* source, int* destination, int size){
// This function copy the source array to destination array, returns nothing
}
int* copyArrayNew(int* source, int size){
// This function copy the source array to new array then returns it
}
```
## Zero Judge
- [初學者小小訓練(共 6 題)](https://zerojudge.tw/ShowContest?contestid=8055)
## Class
- 圖形練習
* 建立一個基本幾何圖形(Shape)類別,具有一個成員變數 area 用來存儲圖形的面積
* 在父類別幾何圖形(Shape)中,定義一個成員函數 calculateArea(),子類別將覆寫此函數以計算各自的面積
* 衍生出兩個子類別,分別為 Circle(圓)和 Square(正方形),繼承自幾何圖形(Shape)類別,並覆寫 calculateArea() 函數以計算各自的面積
* 在父類別幾何圖形(Shape)中,實作一個成員函數 getArea() 用於獲取面積
* 使用封裝,限制直接存取圓的半徑和正方形的邊長與圖形面積
解答:
```cpp=
#include <iostream>
#include <cmath>
using namespace std;
const double PI = 3.14159265359;
class Shape {
protected:
double area; // 面積
public:
virtual void calculateArea() = 0;
double getArea() const {
return area;
}
};
class Circle : public Shape {
private:
double radius; // 半徑
public:
Circle(double radius) : radius(radius) {}
void calculateArea() override {
this->area = PI * radius * radius;
}
double getRadius() const {
return radius;
}
};
class Square : public Shape {
private:
double sideLength; // 邊長
public:
Square(double sideLength) : sideLength(sideLength) {}
void calculateArea() override {
this->area = sideLength * sideLength;
}
double getSideLength() const {
return sideLength;
}
};
int main() {
Circle circle(5.0);
Square square(4.0);
circle.calculateArea();
square.calculateArea()
cout << "Circle Area: " << circle.getArea() << endl;
cout << "Circle Radius: " << circle.getRadius() << endl;
cout << "Square Area: " << square.getArea() << endl;
cout << "Square Side Length: " << square.getSideLength() << endl;
return 0;
}
```