printf("ho_tari\n");

Rational (라이브러리) 본문

C++

Rational (라이브러리)

호타리 2023. 9. 5. 09:16

<main.cpp>

#include <iostream>
#include "rational.h"

int main() 
{
	Rational r1(3,5);
	Rational r2(2,7);

	std::cout << r1+r2 << std::endl;
	std::cout << r1-r2 << std::endl;
	std::cout << r1*r2 << std::endl;
	std::cout << r1/r2 << std::endl;
	std::cout << r1++ << std::endl;
	std::cout << ++r1 << std::endl;
	std::cout << (r1>r2) << std::endl;
	std::cout << r1+5 << std::endl;
	std::cout << r1*100 << std::endl;
    
	return 0;
}

 

<rational.h>

#ifndef RATIONAL_H
#define RATIONAL_H
#include <iostream>

class Rational 
{
friend std::ostream& operator<<(std::ostream& s,const Rational& r);
private:
	long num;
	long den;
	long gcd(long, long);

public:
	Rational(long num=0, long den=1);        //생성자
	Rational(const Rational &rhs);          // 복사생성자
    
	Rational& operator=(const Rational& rhs);  //치환연산자
	Rational& operator=(long rhs);

	long numerator() const {return num;}       //get num함수
	long denominator() const {return den;} 	 //get den함수

	Rational operator+() const {return *this;}   // +r1
	Rational operator-() const {return Rational(-num,den);} // -r1
	Rational invert() const {return Rational(den,num);}  // 역수

	const Rational& operator+=(const Rational &rhs); // +=연산
	const Rational& operator-=(const Rational &rhs); // -=연산
	const Rational& operator*=(const Rational &rhs); // *=연산
	const Rational& operator/=(const Rational &rhs); // /=연산

	const Rational& operator+=(long rhs);
	const Rational& operator-=(long rhs);
	const Rational& operator*=(long rhs);
	const Rational& operator/=(long rhs);

	const Rational& operator++();
	const Rational operator++(int);
	const Rational& operator--();
	const Rational operator--(int);

	const Rational operator+(const Rational& r);
	const Rational operator-(const Rational& r);
	const Rational operator*(const Rational& r);
	const Rational operator/(const Rational& r);

	bool operator==(const Rational& rhs);
	bool operator!=(const Rational& rhs);
	bool operator<=(const Rational& rhs);
	bool operator>=(const Rational& rhs);
	bool operator<(const Rational& rhs);
	bool operator>(const Rational& rhs);
};

#endif

 

<rational.cpp>

#include "rational.h"
#include <iostream>

std::ostream& operator<<(std::ostream& s, const Rational& r) 
{
	if (r.denominator() == 1L) s<<r.numerator();

	else 
    { 
		s<<r.numerator();
		s<<"/";
		s<<r.denominator();
	}

	return s;
}

inline Rational& Rational::operator=(long rhs) 
{
	num = rhs;
	den = 1;

	return *this;
}

inline double toDouble(const Rational& r) 
{
	return double(r.numerator())/r.denominator();
}

inline long trunc(const Rational& r) 
{
	return r.numerator() / r.denominator();
}

inline long floor(const Rational& r) 
{
	long q = r.numerator()/r.denominator();

	return (r.numerator() < 0 && r.denominator() != 1) ? --q : q;
}

inline long ceil(const Rational& r) 
{
	long q = r.numerator() / r.denominator();

	return (r.numerator() >=0 && r.denominator() != 1) ? ++q : q;
}

Rational::Rational(long n, long d) 
{
	if (d == 0L) 
    {
		std::cerr<<"Division by Zero" << std::endl;
		exit(1);
	}

	if (d<0L) {n = -n; d = -d;}

	if (n == 0L) 
    {
		num = 0L; den = 1L;
	} 
    else 
    {
		long g = gcd(n,d);
		num = n/g;
		den = d/g;
	}
}

Rational::Rational(const Rational& rhs) 
{
	num = rhs.num;
	den = rhs.den;
}

long Rational::gcd(long u, long v) 
{
	long a = labs(u), b = labs(v);
	long tmp;
    
	if (b>a) 
    {
		tmp = a;
		a = b;
		b = tmp;
	}

	for (;;) 
    {
		if (b == 0L) return a;

		else if (b == 1L) return b;

		else 
        {
		 tmp =b; b = a%b; a = tmp;
		}
	}
}

const Rational& Rational::operator+=(const Rational& rhs) 
{
	long g1 = gcd(den, rhs.den);

	if (g1 == 1L) 
    {
		num = num*rhs.den + den*rhs.num;
		den = den*rhs.den;
	} 
    else 
    {
		long t = num * (rhs.den/g1) + (den/g1)*rhs.num;
		long g2 = gcd(t,g1);
		num = t/g2;
		den = (den/g1) * (rhs.den/g2);
	}

	return *this;
}

const Rational& Rational::operator-=(const Rational& rhs) 
{
	long g1 = gcd(den, rhs.den);

	if (g1 == 1L) 
    {
		num = num*rhs.den - den*rhs.num;
		den = den*rhs.den;
	} 
    else 
    {
		long t = num*(rhs.den/g1) - (den/g1) * rhs.num;
		long g2 = gcd(t,g1);
		num = t/g2;
		den = (den/g1) * (rhs.den/g2);
	}

	return *this;
}

const Rational& Rational::operator*=(const Rational& rhs) 
{
	long g1 = gcd(num,rhs.den);
	long g2 = gcd(den,rhs.num);
	num = (num/g1) * (rhs.num/g2);
	den = (den/g2) * (rhs.den/g1);
	
    return *this;
}

const Rational& Rational::operator/=(const Rational& rhs) 
{
	if (rhs.num == 0L) 
    {
		std::cerr << "Division by Zero" << std::endl;
		exit(1);
	}

	long g = gcd(rhs.num,rhs.den);
	num *= rhs.den/g;
	den *= rhs.num/g;

	if (den < 0L) 
    {
		num = -num;
		den = -den;
	}

	return *this;
}

const Rational& Rational::operator+=(long rhs) 
{
	num = num + den*rhs;

	return *this;
}

const Rational& Rational::operator-=(long rhs) 
{
	num = num - den*rhs;

	return *this;
}

const Rational& Rational::operator*=(long rhs) 
{
	long g = gcd(den,rhs);
	num *= rhs/g;
	den /= g;

	return *this;
}

const Rational Rational::operator+(const Rational &r)
{
	int num1 = num * r.den + den * r.num;
	int den1 = den * r.den;

	return Rational(num1,den1);	
}

const Rational Rational::operator-(const Rational &r)
{
	int num1 = num * r.den - den * r.num;
	int den1 = den * r.den;

	return Rational(num1,den1);	
}

const Rational Rational::operator*(const Rational &r) 
{
	int num1 = num * r.num;
	int den1 = den * r.den;

	return Rational(num1,den1);
}

const Rational Rational::operator/(const Rational &r) 
{
	int num1 = num * r.den;
	int den1 = den * r.num;

	return Rational(num1,den1);
}

const Rational& Rational::operator++() 
{
	return (*this +=1);
}

const Rational& Rational:: operator--() 
{
	return (*this -=1);
}

const Rational Rational::operator++(int ) 
{
	num /=gcd(num,den);
	den /=gcd(num,den);
	Rational result(*this);
	num += den;

	return result;
}

const Rational Rational::operator--(int ) 
{
	num/=gcd(num,den);
	den/=gcd(num,den);
	Rational result(*this);
	num -= den;

	return result;
}

bool Rational::operator==(const Rational& rhs) 
{
	return (this->num == rhs.numerator() && this->den == rhs.denominator());
}

bool Rational::operator!=(const Rational& rhs) 
{
	return (this->num != rhs.numerator() || this->den != rhs.denominator());
}

bool Rational::operator<(const Rational& rhs) 
{
	return (toDouble(*this) < toDouble(rhs));
}

bool Rational::operator>(const Rational& rhs) 
{
	return (toDouble(*this) > toDouble(rhs));
}

bool Rational::operator<=(const Rational& rhs) 
{
	return ((*this<rhs) || (*this==rhs));
}

bool Rational::operator>=(const Rational& rhs) 
{
	return ((*this>rhs)||(*this==rhs));
}

 

<compile 결과>

'C++' 카테고리의 다른 글

Rational3 (istream 입력 함수)  (0) 2023.09.05
add  (0) 2023.09.05
Rational4 (증감연산자)  (0) 2023.09.04
Complex5 (타입캐스팅)  (0) 2023.09.04
Employee  (0) 2023.09.02