-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfullratio.cpp
86 lines (67 loc) · 1.68 KB
/
fullratio.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
#include <iostream>
#ifndef CONTEST
#include "fullratio.hpp"
#endif
using namespace std;
std::ostream& operator << (std::ostream &out, const rational &x) {
out<<x.nom<<"/"<<x.den;
return out;
}
int rational::gcd(int a, int b) {
if(a<0) a=(-1)*a;
if(b<0) b=(-1)*b;
while(a>0 && b>0) {
if(a>b) a=a%b;
else b=b%a;
}
return a+b;
}
rational::rational (int n, int d) {
int mkd=gcd(n, d);
nom=n/mkd;
den=d/mkd;
if(den<0){
nom=(-1*nom);
den=(-1*den);
}
}
rational operator + (const rational &x, const rational &y) {
int arithm=((x.nom)*(y.den))+((y.nom)*(x.den));
int parono=((x.den)*(y.den));
int a=arithm;
int b=parono;
if(a<0) a=(-1)*a;
if(b<0) b=(-1)*b;
int c=rational::gcd(a,b);
return ( rational(arithm/c, parono/c) );
}
rational operator - (const rational &x, const rational &y) {
int arithm=((x.nom)*(y.den))-((y.nom)*(x.den));
int parono=((x.den)*(y.den));
int a=arithm;
int b=parono;
if(a<0) a=(-1)*a;
if(b<0) b=(-1)*b;
int c=rational::gcd(a,b);
return ( rational(arithm/c, parono/c) );
}
rational operator * (const rational &x, const rational &y) {
int arithm=((x.nom)*(y.nom));
int parono=((x.den)*(y.den));
int a=arithm;
int b=parono;
if(a<0) a=(-1)*a;
if(b<0) b=(-1)*b;
int c=rational::gcd(a,b);
return ( rational(arithm/c, parono/c) );
}
rational operator / (const rational &x, const rational &y) {
int arithm=((x.nom)*(y.den));
int parono=((x.den)*(y.nom));
int a=arithm;
int b=parono;
if(a<0) a=(-1)*a;
if(b<0) b=(-1)*b;
int c=rational::gcd(a,b);
return ( rational(arithm/c, parono/c) );
}