Creative Code

main.cpp(rational클래스 여러 연산자) 본문

C++ Programming

main.cpp(rational클래스 여러 연산자)

빛하루 2023. 8. 31. 12:09

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

}

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

main.cpp(string2)  (0) 2023.09.01
main.cpp(string)  (0) 2023.08.31
main.cpp(complex클래스 여러 연산자)  (0) 2023.08.31
main.cpp(Rational 사칙연산)  (0) 2023.08.30
main.cpp(complex 연산)  (0) 2023.08.30