-
Notifications
You must be signed in to change notification settings - Fork 0
/
087_FractionClass.cpp
78 lines (53 loc) · 892 Bytes
/
087_FractionClass.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
#include<iostream>
#include<algorithm>
using namespace std;
class Fraction {
int n, d;
// Complete the class
public:
Fraction(int n, int d){
this->n=n;
this->d=d;
}
void add(Fraction &obj){
n=(n*obj.d)+(obj.n*d);
d*=obj.d;
int gcd=__gcd(n, d);
n/=gcd;
d/=gcd;
}
void mult(Fraction &obj){
d*=obj.d;
n*=obj.n;
int gcd=__gcd(n, d);
n/=gcd;
d/=gcd;
}
void print(){
cout<<n<<"/"<<d<<endl;
}
};
int main() {
//Write your code here
int num1,den1;
cin>>num1>>den1;
Fraction a1(num1,den1);
int choice,t,i;
cin>>t;
for(i=0;i<t;i++){
cin>>choice;
int num2,den2;
cin>>num2>>den2;
Fraction a2(num2,den2);
if(choice==1){
a1.add(a2);
a1.print();
}
if(choice==2){
a1.mult(a2);
a1.print();
}
}
return 0;
return 0;
}