-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathFraction(Class).cpp
114 lines (101 loc) · 1.38 KB
/
Fraction(Class).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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#include<bits/stdc++.h>
using namespace std;
#define LL unsigned long long
#define mp make_pair
#define pb push_back
#define gc getchar_unlocked
#define pc putchar_unlocked
LL gcd(LL A,LL B)
{
if(B==0)
return A;
else return gcd(B,A%B);
}
class Fraction
{
LL N,D;
public:
Fraction()
{
N=0;D=1;
}
Fraction(LL A,LL B)
{
N=A;
D=B;
}
LL getNumerator()
{
return N;
}
LL getDenominator()
{
return D;
}
friend Fraction operator +(Fraction A,Fraction B)
{
Fraction C;
C.D=A.D*B.D;
C.N=A.N*B.D+A.D*B.N;
int G=gcd(C.N,C.D);
C.D/=G;
C.N/=G;
return C;
}
friend Fraction operator -(Fraction A,Fraction B)
{
Fraction C;
C.D=A.D*B.D;
C.N=A.N*B.D-A.D*B.N;
int G=gcd(C.N,C.D);
C.D/=G;
C.N/=G;
return C;
}
friend Fraction operator *(Fraction A,Fraction B)
{
Fraction C;
C.D=A.D*B.D;
C.N=A.N*B.N;
int G=gcd(C.N,C.D);
C.D/=G;
C.N/=G;
return C;
}
friend Fraction operator /(Fraction A,Fraction B)
{
Fraction C;
C.D=A.D*B.N;
C.N=A.N*B.D;
int G=gcd(C.N,C.D);
C.D/=G;
C.N/=G;
return C;
}
friend istream &operator>>(istream &din,Fraction &A)
{
din>>A.N>>A.D;
return din;
}
friend ostream &operator<<(ostream &dout,Fraction &A)
{
if(A.N==0)
{
dout<<"0"<<endl;
}
else
{
dout<<A.N<<"/"<<A.D<<endl;
}
return dout;
}
};
int main()
{
Fraction A,B;
cin>>A;
cin>>B;
Fraction C=A+B;
cout<<C;
return 0;
}