Creative Code

main.cpp(Rational 사칙연산) 본문

C++ Programming

main.cpp(Rational 사칙연산)

빛하루 2023. 8. 30. 16:33

※main.cpp파일

#include <iostream>

#include "rational.h"



int main(){

	Rational r1(3,4);

	Rational r2(3);

	Rational r3;

	

	r3 = r1;

	

	if (r1 == r3) {

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

	} else {

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

	}

	

	std::cout << r1.number() << "/" << r1.deno() << std::endl;

	std::cout << r3.number() << "/" << r3.deno() << std::endl;

	

	r2 = r1+r3;

	std::cout << r2.number() << "/" << r2.deno() << std::endl;

	r2 = r1-r3;

	std::cout << r2.number() << "/" << r2.deno() << std::endl;

	r2 = r1*r3;

	std::cout << r2.number() << "/" << r2.deno() << std::endl;

	r2 = r1/r3;

	std::cout << r2.number() << "/" << r2.deno() << std::endl;

	

}

 

※rational.h파일

#ifndef RATIONAL_H

#define RATIONAL_H



class Rational{

public:

	Rational(int num, int den);//생성자

	Rational(int num);

	Rational();

	~Rational();//소멸자

	int number();//get

	int deno();

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

	void number(int num);

	void deno(int num);

	//set



private:

	int num;   // 분자

	int den;   // 분모



};

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

}



Rational::Rational(int num, int den)

{

	assert(den!=0);

	this ->num = num;

	this ->den = den;

}



Rational::Rational(int num)

{

	this->num = num;

	this->den = 1;

}



Rational::Rational()

{

	this->num = 0;

}



Rational::~Rational()

{

}



int Rational::number()

{

	return this->num;

}



int Rational::deno()

{

	return this->den;

}



void Rational::number(int num)

{

	this->num = num;

}



void Rational::deno(int den)

{

	assert(den!=0);

	this->den = den;

}



void Rational::operator=(const Rational &rhs) {

	this->num = rhs.num;

	this->den = rhs.den;

}



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

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

}



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

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

	int gcd = GCD(this->num * rhs.den + rhs.num * this->den,this->den * rhs.den);

	result.num = (this->num * rhs.den + rhs.num * this->den)/gcd;

	result.den = (this->den * rhs.den)/gcd;

	return result;

}



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

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

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

	result.num = (this->num * rhs.den - rhs.num * this->den)/gcd;

	result.den = (this->den * rhs.den)/gcd;

	return result;

}



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

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

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

	result.num = this->num * rhs.num/gcd;

	result.den = this->den * rhs.den/gcd;

	return result;

}



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

	assert(rhs.num!=0);

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

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

	result.num = this->num * rhs.den / gcd;

	result.den = this->den * rhs.num / gcd;

	return result;

}

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

main.cpp(rational클래스 여러 연산자)  (0) 2023.08.31
main.cpp(complex클래스 여러 연산자)  (0) 2023.08.31
main.cpp(complex 연산)  (0) 2023.08.30
main.cpp(complex 클래스)  (0) 2023.08.30
main.cpp(linked list)  (0) 2023.08.29