-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfraction.h
More file actions
153 lines (135 loc) · 3.35 KB
/
fraction.h
File metadata and controls
153 lines (135 loc) · 3.35 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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#ifndef FRACTION_H
#define FRACTION_H
#include <iostream>
#include <stdexcept>
#include <string>
#include <cmath>
#include <compare>
// 2021136089 이관우
class Fraction
{
private:
int n{0};
int d{1};
public:
explicit Fraction() = default;
explicit Fraction(int n, int d = 1)
{
if (d == 0)
throw std::invalid_argument("분모는 0이 될 수 없음");
if ((n >= 0 && d < 0) || (n < 0 && d < 0))
{
this->n = -n;
this->d = -d;
}
}
virtual ~Fraction() = default;
int numerator() const noexcept { return n; }
int denominator() const noexcept { return d; }
explicit operator std::string() const noexcept
{
if (n % d == 0)
return std::to_string(n / d);
else
return std::to_string(n) + "/" + std::to_string(d);
}
explicit operator double() const noexcept
{
return static_cast<double>(n) / d;
}
Fraction operator-() const
{
return Fraction(-n, d);
}
Fraction operator+(const Fraction &other) const
{
int num = n * other.d + other.n * d;
int den = d * other.d;
Fraction result(num, den);
result.reduction();
return result;
}
Fraction operator-(const Fraction &other) const
{
return operator+(Fraction(-other.n, other.d));
}
Fraction operator*(const Fraction &other) const
{
int num = n * other.n;
int den = d * other.d;
Fraction result(num, den);
result.reduction();
return result;
}
Fraction operator/(const Fraction &other) const
{
if (other.n == 0)
throw std::invalid_argument("0으로 나눌 수 없음");
return operator*(Fraction(other.d, other.n));
}
auto operator<=>(const Fraction &other) const
{
return (static_cast<double>(*this) <=> static_cast<double>(other));
}
bool operator==(const Fraction &other) const
{
return operator<=>(other) == 0;
}
private:
int gcd(int a, int b) const noexcept
{
return (b == 0) ? a : gcd(b, a % b);
}
void reduction() noexcept
{
int g = gcd(std::abs(n), d);
n /= g;
d /= g;
}
friend std::ostream &operator<<(std::ostream &os, const Fraction &o);
friend std::istream &operator>>(std::istream &is, Fraction &f);
};
std::ostream &operator<<(std::ostream &os, const Fraction &o)
{
os << static_cast<std::string>(o);
return os;
}
std::istream &operator>>(std::istream &is, Fraction &f)
{
std::string input;
is >> input;
size_t pos = input.find('/');
if (pos != std::string::npos)
{
try
{
f.n = std::stoi(input.substr(0, pos));
f.d = std::stoi(input.substr(pos + 1));
if (f.d == 0)
throw std::invalid_argument("분모는 0이 될 수 없음");
if ((f.n >= 0 && f.d < 0) || (f.n < 0 && f.d < 0))
{
f.n = -f.n;
f.d = -f.d;
}
}
catch (const std::exception &e)
{
is.setstate(std::ios::failbit);
}
}
else
{
try
{
f.n = std::stoi(input);
f.d = 1;
}
catch (const std::exception &e)
{
is.setstate(std::ios::failbit);
}
}
return is;
}
#endif