forked from Sheeba02M/Hacktoberfest-2021
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Overload.cpp
91 lines (85 loc) · 2.35 KB
/
Overload.cpp
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#include <iostream>
using namespace std;
class Fraction {
int num;
int denom;
public:
Fraction() {
}
Fraction(int num, int denom) {
this -> num = num;
this -> denom = denom;
}
void simplify() {
int gcd = 1;
int j = min(num, denom);
for(int i = 1 ; i <= j ; i++)
if(num % i == 0 && denom % i == 0)
gcd = i;
num /= gcd;
denom /= gcd;
}
friend Fraction operator+(Fraction const &, Fraction const &);
Fraction operator-(Fraction const &f) {
int lcm = denom * f.denom;
int x = lcm / denom;
int y = lcm / f.denom;
int n = x * num - (y * f.num);
Fraction fNew(n, lcm);
fNew.simplify();
return fNew;
}
Fraction operator*(Fraction const &f) {
int lcm = denom * f.denom;
int n = num * f.num;
Fraction fNew(n, lcm);
fNew.simplify();
return fNew;
}
Fraction operator/(Fraction const &f) {
int lcm = denom * f.num;
int n = num * f.denom;
Fraction fNew(n, lcm);
fNew.simplify();
return fNew;
}
friend ostream &operator<<(ostream &out, Fraction const &f);
friend istream &operator>>(istream &in, Fraction &f);
};
Fraction operator+(Fraction const &f1, Fraction const &f2) {
int lcm = f1.denom * f2.denom;
int x = lcm / f1.denom;
int y = lcm / f2.denom;
int n = x * f1.num + (y * f2.num);
Fraction fNew(n, lcm);
fNew.simplify();
return fNew;
}
ostream &operator<<(ostream &out, Fraction const &f) {
out<<f.num<<" / "<<f.denom<<endl;
return out;
}
istream &operator>>(istream &in, Fraction &f) {
in>>f.num>>f.denom;
return in;
}
int main() {
Fraction f1, f2;
cout<<"Enter the numerator and the denominator of the first fraction(F1): ";
cin>>f1;
cout<<"Enter the numerator and the denominator of the second fraction(F2): ";
cin>>f2;
Fraction f3 = f1 + f2;
cout<<"F1 + F2 = ";
cout<<f3;
f3 = f1 - f2;
cout<<"F1 - F2 = ";
cout<<f3;
f3 = f1 * f2;
cout<<"F1 * F2 = ";
cout<<f3;
f3 = f1 / f2;
cout<<"F1 / F2 = ";
cout<<f3;
return 0;
}