Recent Posts
printf("ho_tari\n");
Rational (사칙연산) 본문
<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);//생성자
Raional(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;
}
<compile 결과>
'C++' 카테고리의 다른 글
Complex4 (증감연산자) (0) | 2023.09.02 |
---|---|
Rational2 (여러연산자) (0) | 2023.09.02 |
Complex2 (연산) (0) | 2023.09.02 |
Complex (0) | 2023.09.02 |
Empty (컴파일러가 자동생성하는 함수) (0) | 2023.09.01 |