#include <iostream>
#include <numeric> // for std::gcd
class Rational {
private:
int numerator;
int denominator;
void simplify() {
int gcd = std::gcd(numerator, denominator);
numerator /= gcd;
denominator /= gcd;
if (denominator < 0) {
numerator = -numerator;
denominator = -denominator;
}
}
public:
Rational(int num = 0, int denom = 1) : numerator(num), denominator(denom) {
if (denominator == 0) throw std::invalid_argument("Denominator cannot be zero");
simplify();
}
// 사칙연산
Rational operator+(const Rational& other) const {
return Rational(numerator * other.denominator + other.numerator * denominator,
denominator * other.denominator);
}
Rational operator-(const Rational& other) const {
return Rational(numerator * other.denominator - other.numerator * denominator,
denominator * other.denominator);
}
Rational operator*(const Rational& other) const {
return Rational(numerator * other.numerator, denominator * other.denominator);
}
Rational operator/(const Rational& other) const {
if (other.numerator == 0) throw std::invalid_argument("Division by zero");
return Rational(numerator * other.denominator, denominator * other.numerator);
}
// 단항 연산자
Rational operator+() const {
return *this;
}
Rational operator-() const {
return Rational(-numerator, denominator);
}
// 복합 대입 연산자
Rational& operator+=(const Rational& other) {
*this = *this + other;
return *this;
}
Rational& operator-=(const Rational& other) {
*this = *this - other;
return *this;
}
Rational& operator*=(const Rational& other) {
*this = *this * other;
return *this;
}
Rational& operator/=(const Rational& other) {
*this = *this / other;
return *this;
}
// 비교 연산자
bool operator==(const Rational& other) const {
return (numerator == other.numerator) && (denominator == other.denominator);
}
bool operator!=(const Rational& other) const {
return !(*this == other);
}
bool operator<(const Rational& other) const {
return (numerator * other.denominator) < (other.numerator * denominator);
}
bool operator>(const Rational& other) const {
return other < *this;
}
bool operator<=(const Rational& other) const {
return !(other < *this);
}
bool operator>=(const Rational& other) const {
return !(*this < other);
}
// 입출력 연산자
friend std::ostream& operator<<(std::ostream& os, const Rational& r) {
os << r.numerator;
if (r.denominator != 1) os << "/" << r.denominator;
return os;
}
friend std::istream& operator>>(std::istream& is, Rational& r) {
char slash;
is >> r.numerator >> slash >> r.denominator;
if (r.denominator == 0) throw std::invalid_argument("Denominator cannot be zero");
r.simplify();
return is;
}
};
int main() {
Rational r1(1, 2), r2(1, 3);
std::cout << r1 << " + " << r2 << " = " << (r1 + r2) << std::endl;
std::cout << r1 << " - " << r2 << " = " << (r1 - r2) << std::endl;
std::cout << r1 << " * " << r2 << " = " << (r1 * r2) << std::endl;
std::cout << r1 << " / " << r2 << " = " << (r1 / r2) << std::endl;
r1 += r2;
std::cout << "After r1 += r2, r1 = " << r1 << std::endl;
std::cout << "Enter a rational number (e.g., 3/4): ";
Rational r3;
std::cin >> r3;
std::cout << "You entered: " << r3 << std::endl;
return 0;
}