printf("ho_tari\n");

Rational3 (여러연산자) 본문

C++

Rational3 (여러연산자)

호타리 2023. 8. 31. 15:37

<main.cpp>

#include <iostream>

#include "rational.h"



int main()

{

	Rational r1;         // 0/1

	Rational r2 = 1;     // 1/1

	//Rational r2(1);

	Rational r3(3, 4);   // 3/4

	Rational r4 = r3;

	

	//r1.setNum(r3.getNum());

	//r1.setNum(r3.getNum());

	r1 = r3;

	

	if (r1 == r3)

	{

		std::cout << "r1 and r3 are equal" << std::endl;

	}

	else

	{

		std::cout << "r1 and r3 are not equal" << std::endl;

	}

	

	r4 = r1 + r2;

	r4 = r1 - r2;

	r4 = r1 * r2;

	r4 = r1 / r2;

	

	r1 > r2;

	r4 += r1;

	

	//std::cout << "r1 : " << r1.getNum() << "/" << r1.getDen() << std::endl;

	//std::cout << "r2 : " << r2.getNum() << "/" << r2.getDen() << std::endl;

	//std::cout << "r3 : " << r3.getNum() << "/" << r3.getDen() << std::endl;

	//std::cout << "r4 : " << r4.getNum() << "/" << r4.getDen() << std::endl;

	std::cout << "r1 ; " << r1 << std::endl;

	std::cout << "r2 ; " << r2 << std::endl;

	std::cout << "r3 ; " << r3 << std::endl;

	std::cout << "r4 ; " << r4 << std::endl;

	

	return 0;

}

 

<rational.h>

#ifndef RATIONAL_H

#define RATIONAL_H

#include <iostream>



class Rational {

friend std::ostream& operator<<(std::ostream& out, const Rational& rhs);



public:

	//Rational();                        // default constructor

	//Rational(int num);

	Rational(int num = 0, int den = 1);

	Rational(const Rational& rhs);     // copy constructor

	~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);

	// <, >=, <=

	

	const Rational operator+(const Rational& rhs);

	const Rational operator-(const Rational& rhs);

	const Rational operator*(const Rational& rhs);

	const Rational operator/(const Rational& rhs);

	

	int getNum();

	int getDen();

	

	void setNum(int num);

	void setDen(int den);



private:

	int num;

	int den;

};



#endif

 

<rational.cpp>

#include "rational.h"

#include <cassert>



std::ostream& operator<<(std::ostream& out, const Rational& rhs)

{

	out << rhs.num << "/" << rhs.den;

	

	return out;

}



/*

Rational::Rational()

{

	this->num = 0;

	this->den = 1;

}



Rational::Rational(int num)

{

	this->num = num;

	this->den = 1;

}

*/



Rational::Rational(int num, int den)

{

	assert(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 = *this + rhs;

	

	return *this;

}



bool Rational::operator>(const Rational& rhs)

{

	return this->num * rhs.den > rhs.num * this->den;

}



bool Rational::operator==(const Rational& rhs)

{

	return this->num == rhs.num && this->den == rhs.den;

}



bool Rational::operator!=(const Rational& rhs)

{

	//return !(*this == rhs);

	return !this->operator==(rhs);

}



const Rational Rational::operator+(const Rational& rhs)

{

	Rational result(this->num * rhs.den + rhs.num * this->den, this->den * rhs.den);

	

	return result;

}



const Rational Rational::operator-(const Rational& rhs)

{

	Rational result(this->num * rhs.den + rhs.num * this->den, this->den * rhs.den);

	

	return result;

}



const Rational Rational::operator*(const Rational& rhs)

{

	Rational result(this->num * rhs.num, this->den * rhs.den);

	

	return result;

}



const Rational Rational::operator/(const Rational& rhs)

{

	Rational result(this->num * rhs.den, this->den * rhs.num);

	

	return result;

}



int Rational::getNum()

{

	return this->num;

}



int Rational::getDen()

{

	return this->den;

}



void Rational::setNum(int num)

{

	this->num = num;

}



void Rational::setDen(int den)

{

	this->den = den;

}

 

<compile 결과>

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

Empty (컴파일러가 자동생성하는 함수)  (0) 2023.09.01
String2  (0) 2023.09.01
String  (0) 2023.08.31
Queue  (0) 2023.08.31
Stack  (0) 2023.08.31