printf("ho_tari\n");
Rational3 (inline 함수, namespace) 본문
<main.cpp>
#include <iostream>
#include "rational.h"
using iot::Rational;
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;
Rational r5;
std::cout << "input r5 : ";
std::cin >> r5;
std::cout << "r5 : " << r5 << std::endl;
return 0;
}
<rational.h>
#ifndef RATIONAL_H
#define RATIONAL_H
#include <iostream>
namespace iot
{
class Rational {
friend std::istream& operator>>(std::istream& in, Rational& rhs);
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;
};
inline int Rational::getNum()
{
return this->num;
}
inline int Rational::getDen()
{
return this->den;
}
inline void Rational::setNum(int num)
{
this->num = num;
}
inline void Rational::setDen(int den)
{
this->den = den;
}
}
#endif
<rational.cpp>
#include "rational.h"
#include <cassert>
#include "gcd.h"
namespace iot
{
std::istream& operator>>(std::istream& in, Rational& rhs)
{
int n = 0, d = 1;
in >> n;
if (in.peek() == '/')
{
in.ignore();
in >> d;
}
if (in )
{
rhs = Rational(n, d);
}
return in;
}
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 );
int g = gcd(num, den);
this->num = num / g;
this->den = den / g;
}
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;
}
}
<compile 결과>
'C++' 카테고리의 다른 글
Complex6 (inline 함수, namespace) (0) | 2023.09.11 |
---|---|
Complex6 (inline 함수) (0) | 2023.09.11 |
String4 (const_cast) (0) | 2023.09.08 |
Shape (dynamic_cast) (0) | 2023.09.08 |
Pointer3 (reinterpret_cast) (0) | 2023.09.08 |