printf("ho_tari\n");

Rational2 (여러연산자) 본문

C++

Rational2 (여러연산자)

호타리 2023. 9. 2. 12:42

<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; 
}

 

<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(const Rational &rhs);
	~Rational();

	Rational operator=(const Rational& rhs);
	Rational& operator+=(const Rational& rhs);

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

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

#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) 
{
	this->num = num;
	this->den = den;
}

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

Rational::~Rational() 
{

}

Rational Rational::operator=(const Rational &rhs) 
{
	this->num = rhs.num;
	this->den = rhs.den;

	return *this;
}

Rational& Rational::operator+=(const Rational &rhs) 
{
	this->num = this->num * rhs.den + this->den*rhs.num;
	this->den = this->den * rhs.den;

	int gcd = GCD(this->num,this->den);

	this->num /= gcd;
	this->den /= gcd;

	return *this;
}

bool Rational::operator>(const Rational &rhs) 
{
	double result1 = (double)(this->num)/(double)(this->den);
	double result2 = (double)(rhs.num)/(double)(rhs.den);

	return result1>result2;
}

bool Rational::operator==(const Rational &rhs) 
{
	int gcd1 = GCD(this->num,this->den);
	int gcd2 = GCD(rhs.num,rhs.den);

	return (this->num/gcd1 == rhs.num/gcd2 && this->den/gcd1 == rhs.den/gcd2);
}

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

Rational Rational::operator+(const Rational &rhs) 
{
	int num1 = this->num * rhs.den + this->den * rhs.num;
	int den1 = this->den * rhs.den;
	int gcd = GCD(num1,den1);

	return Rational(num1/gcd,den1/gcd);	
}

Rational Rational::operator-(const Rational &rhs) 
{
	int num1 = this->num * rhs.den - this->den * rhs.num;
	int den1 = this->den * rhs.den;
	int gcd = GCD(num1,den1);

	return Rational(num1/gcd,den1/gcd);	
}

Rational Rational::operator*(const Rational &rhs) 
{
	int num1 = this->num * rhs.num;
	int den1 = this->den * rhs.den;
	int gcd = GCD(num1,den1);

	return Rational(num1/gcd,den1/gcd);
}

Rational Rational::operator/(const Rational &rhs) 
{
	assert(rhs.num!=0);
	int num1 = this->num * rhs.den;
	int den1 = this->den * rhs.num;
	int gcd = GCD(num1,den1);

	return Rational(num1/gcd,den1/gcd);
}

 

<compile 결과>

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

Employee  (0) 2023.09.02
Complex4 (증감연산자)  (0) 2023.09.02
Rational (사칙연산)  (0) 2023.09.02
Complex2 (연산)  (0) 2023.09.02
Complex  (0) 2023.09.02