printf("ho_tari\n");
Rational4 (증감연산자) 본문
<main.cpp>
#include <iostream>
#include "rational.h"
int main() {
Rational r1; // 0
Rational r2(3); // 3/1
Rational r3(3,4); // 3/4
std::cout << "r1 : " << r1 << std::endl;
std::cout << "r1 + r2 : " << r1+r2 <<std::endl;
std::cout << "r2 * r3 : " << r2*r3 << std::endl;
std::cout << "r2 == r3 : " << (r2==r3) << std::endl;
std::cout << "r2 > r3 : " << (r2>r3) << std::endl;
std::cout << "r2 / r3 : " << (r2/r3) << std::endl;
r3++;
std::cout << "r3 : " << r3 << std::endl;
++r3;
std::cout << "r3 : " << r3 << std::endl;
}
<rational.h>
#ifndef RATIONAL_H
#define RATIONAL_H
#include <iostream>
class Rational {
friend std::ostream& operator<<(std::ostream& out, const Rational& rhs);
private:
int num_;
int den_;
public:
Rational(int num = 0, int den = 1);
Rational& operator+=(const Rational& rhs);
bool operator>(const Rational& rhs) const;
bool operator==(const Rational& rhs) const;
bool operator!=(const Rational& rhs) const;
const Rational operator+(const Rational& rhs) const;
const Rational operator-(const Rational& rhs) const;
const Rational operator*(const Rational& rhs) const;
const Rational operator/(const Rational& rhs) const;
const Rational& operator++();
const Rational operator++(int );
};
#endif
<rational.cpp>
#include "rational.h"
#include <cassert>
int GCD(int a, int b)
{
if (b == 0) return a;
else return GCD(b,a%b);
}
std::ostream& operator<<(std::ostream& out, const Rational& rhs)
{
out << "(" << rhs.num_ << "/" << rhs.den_ << ")";
return out;
}
Rational::Rational(int num, int den)
{
num_ = num;
den_ = den;
}
Rational& Rational::operator+=(const Rational &rhs)
{
num_ = num_ * rhs.den_ + den_*rhs.num_;
den_ = den_ * rhs.den_;
int gcd = GCD(num_,den_);
num_ /= gcd;
den_ /= gcd;
return *this;
}
bool Rational::operator>(const Rational &rhs) const
{
double result1 = (double)(num_)/(double)(den_);
double result2 = (double)(rhs.num_)/(double)(rhs.den_);
return result1>result2;
}
bool Rational::operator==(const Rational &rhs) const
{
int gcd1 = GCD(num_,den_);
int gcd2 = GCD(rhs.num_,rhs.den_);
return (num_/gcd1 == rhs.num_/gcd2 && den_/gcd1 == rhs.den_/gcd2);
}
bool Rational::operator!=(const Rational &rhs) const
{
return !(*this==rhs);
}
const Rational Rational::operator+(const Rational &rhs) const
{
int num1 = num_ * rhs.den_ + den_ * rhs.num_;
int den1 = den_ * rhs.den_;
int gcd = GCD(num1,den1);
return Rational(num1/gcd,den1/gcd);
}
const Rational Rational::operator-(const Rational &rhs) const
{
int num1 = num_ * rhs.den_ - den_ * rhs.num_;
int den1 = den_ * rhs.den_;
int gcd = GCD(num1,den1);
return Rational(num1/gcd,den1/gcd);
}
const Rational Rational::operator*(const Rational &rhs) const
{
int num1 = num_ * rhs.num_;
int den1 = den_ * rhs.den_;
int gcd = GCD(num1,den1);
return Rational(num1/gcd,den1/gcd);
}
const Rational Rational::operator/(const Rational &rhs) const
{
assert(rhs.num_!=0);
int num1 = num_ * rhs.den_;
int den1 = den_ * rhs.num_;
int gcd = GCD(num1,den1);
return Rational(num1/gcd,den1/gcd);
}
const Rational& Rational::operator++()
{
num_ /= GCD(num_,den_);
den_ /= GCD(num_,den_);
num_ += den_;
return *this;
}
const Rational Rational::operator++(int )
{
num_ /=GCD(num_,den_);
den_ /=GCD(num_,den_);
Rational result(*this);
num_ += den_;
return result;
}
<compile 결과>
'C++' 카테고리의 다른 글
add (0) | 2023.09.05 |
---|---|
Rational (라이브러리) (0) | 2023.09.05 |
Complex5 (타입캐스팅) (0) | 2023.09.04 |
Employee (0) | 2023.09.02 |
Complex4 (증감연산자) (0) | 2023.09.02 |