-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFraction.cpp
More file actions
54 lines (51 loc) · 1 KB
/
Copy pathFraction.cpp
File metadata and controls
54 lines (51 loc) · 1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
// 20190324_ex3.cpp: 主要專案檔。
#include "stdafx.h"
#include <iostream>
using namespace std;
class FRACTION{
private:
int num;
int den;
public:
//FRACTION();
FRACTION(int m = 0, int n = 1); //利用參數的預設值 減少多載的撰寫
FRACTION add(FRACTION &);
FRACTION sub(FRACTION &);
FRACTION mul(FRACTION &);
FRACTION div(FRACTION &);
void show();
};
int main()
{
FRACTION a(1,2), b(2,3), c, d(5);
a.add(b).show();
b.sub(a).show();
system("pause");
return 0;
}
/* 普
FRACTION::FRACTION(){
num = 0;
den = 1;//兩個 指定運算子
}
*/
//FRACTION::FRACTION():num(0),den(1){} //佳 優
FRACTION::FRACTION(int m, int n){
num = m;
den = n;
}
FRACTION FRACTION::add(FRACTION &b){
FRACTION temp;
temp.den = this->den * b.den;
temp.num = this->num * b.den + b.num * this->den;
return temp;
}
FRACTION FRACTION::sub(FRACTION &b){
FRACTION temp;
temp.den = this->den * b.den;
temp.num = this->num * b.den - b.num * this->den;
return temp;
}
void FRACTION::show(){
cout << this->num << '/' << this->den << endl;
}