forked from StevensDeptECE/CPE553-CPP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFraction2.cc
66 lines (57 loc) · 1.41 KB
/
Fraction2.cc
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
55
56
57
58
59
60
61
62
63
64
65
66
#include <iostream>
using namespace std;
class Ex {
private:
int linenum;
const char * filename;
public:
Ex(int linenum, const char* filename)
: linenum(linenum), filename(filename) {}
friend ostream& operator <<(ostream& s, const Ex& dbz) {
return s << dbz.filename << ": " << dbz.linenum;
}
};
class DivByZero : public Ex {
public:
DivByZero(int linenum, const char* filename) : Ex(linenum, filename){}
friend ostream& operator <<(ostream& s, const DivByZero& dbz) {
return s << "Divide by zero " << (Ex&)dbz;
}
};
class Fraction {
private:
int num, den;
int gcd(int a, int b) { // Euclid gcd(a,0) == a gcd(b, a % b)
if (b == 0)
return a;
return gcd(b, a % b);
}
public:
Fraction(int n, int d) : num(n), den(d) {
if (d == 0) {
throw DivByZero(__LINE__, __FILE__);
}
int g = gcd(n,d);
if (g > 1) {
num /= g;
den /= g;
}
}
friend Fraction operator +(Fraction a, Fraction b) {
return Fraction(a.num*b.den+a.den*b.num, a.den*b.den);
}
friend Fraction operator /(Fraction a, Fraction b) {
return Fraction(a.num*b.den, a.den*b.num);
}
};
int main() {
try {
Fraction a(1,2); // 1/2
Fraction b(1,2); // 1/3
Fraction c = a + b; // 2/4 --> 1/2
Fraction d(0,1);
Fraction e = c / d;
} catch (DivByZero e) {
cout << e << '\n';
}
}