forked from eerick1997/reference
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfraccion.cpp
More file actions
123 lines (118 loc) · 3.06 KB
/
fraccion.cpp
File metadata and controls
123 lines (118 loc) · 3.06 KB
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
115
116
117
118
119
120
121
122
123
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
struct fraccion{
ll num, den;
fraccion(){
num = 0, den = 1;
}
fraccion(ll x, ll y){
if(y < 0)
x *= -1, y *=-1;
ll d = __gcd(abs(x), abs(y));
num = x/d, den = y/d;
}
fraccion(ll v){
num = v;
den = 1;
}
fraccion operator+(const fraccion& f) const{
ll d = __gcd(den, f.den);
return fraccion(num*(f.den/d) + f.num*(den/d), den*(f.den/d));
}
fraccion operator-() const{
return fraccion(-num, den);
}
fraccion operator-(const fraccion& f) const{
return *this + (-f);
}
fraccion operator*(const fraccion& f) const{
return fraccion(num*f.num, den*f.den);
}
fraccion operator/(const fraccion& f) const{
return fraccion(num*f.den, den*f.num);
}
fraccion operator+=(const fraccion& f){
*this = *this + f;
return *this;
}
fraccion operator-=(const fraccion& f){
*this = *this - f;
return *this;
}
fraccion operator++(int xd){
*this = *this + 1;
return *this;
}
fraccion operator--(int xd){
*this = *this - 1;
return *this;
}
fraccion operator*=(const fraccion& f){
*this = *this * f;
return *this;
}
fraccion operator/=(const fraccion& f){
*this = *this / f;
return *this;
}
bool operator==(const fraccion& f) const{
ll d = __gcd(den, f.den);
return (num*(f.den/d) == (den/d)*f.num);
}
bool operator!=(const fraccion& f) const{
ll d = __gcd(den, f.den);
return (num*(f.den/d) != (den/d)*f.num);
}
bool operator >(const fraccion& f) const{
ll d = __gcd(den, f.den);
return (num*(f.den/d) > (den/d)*f.num);
}
bool operator <(const fraccion& f) const{
ll d = __gcd(den, f.den);
return (num*(f.den/d) < (den/d)*f.num);
}
bool operator >=(const fraccion& f) const{
ll d = __gcd(den, f.den);
return (num*(f.den/d) >= (den/d)*f.num);
}
bool operator <=(const fraccion& f) const{
ll d = __gcd(den, f.den);
return (num*(f.den/d) <= (den/d)*f.num);
}
fraccion inverso() const{
return fraccion(den, num);
}
fraccion fabs() const{
fraccion nueva;
nueva.num = abs(num);
nueva.den = den;
return nueva;
}
double value() const{
return (double)num / (double)den;
}
string str() const{
stringstream ss;
ss << num;
if(den != 1) ss << "/" << den;
return ss.str();
}
};
ostream &operator<<(ostream &os, const fraccion & f) {
return os << f.str();
}
istream &operator>>(istream &is, fraccion & f){
ll num = 0, den = 1;
string str;
is >> str;
size_t pos = str.find("/");
if(pos == string::npos){
istringstream(str) >> num;
}else{
istringstream(str.substr(0, pos)) >> num;
istringstream(str.substr(pos + 1)) >> den;
}
f = fraccion(num, den);
return is;
}