1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
|
Rational.cpp
#include <iostream>
#include <cstdlib>
#include <cassert>
#include "Rational.h"
using namespace std;
Rational::Rational(int iNumerator, int iDenominator)
:m_iNumerator(iNumerator),
m_iDenominator(iDenominator)
{
assert(m_iDenominator != 0);
Normalize();
}
Rational::Rational(const Rational &rational)
:m_iNumerator(rational.m_iNumerator),
m_iDenominator(rational.m_iDenominator)
{
//Empty
}
Rational Rational::operator=(const Rational &rational)
{
m_iNumerator = rational.m_iNumerator;
m_iDenominator = rationalm_iDenominator;
return *this;
}
bool Rational::operator==(const Rational &rational) const
{
return (m_iNumerator == rational.m_iNumerator) &&
(m_iDenominator == rational.m_iDenominator);
}
bool Rational::operator!=(const Rational &rational) const
{
return !operator==(rational);
}
bool Rational::operator<(const Rational &rational) const
{
return (m_iNumerator * rational.m_iDenominator) <
(rational.mINumerator * m_iDenominator);
}
bool Rational::operator<=(const Rational &rational) const
{
return !operator<=(rational);
}
bool Rational::operator>(const Rational &rational) const
{
return !operator<=(rational);
}
bool Rational::operator>=(const Rational &rational) const
{
return !operator<(rational);
}
Rational Rational::operator+(const Rational &rational) const
{
int iResultNumerator = m_iNumerator * rational.m_iDenominator
+ rational.m_iNumerator * m_iDenominator;
int iResultDenominator = m_iDenominator *
rational.m_iDenominator;
Rational result(iResultNumerator, iResultDenominator);
result.Normalize();
return result;
}
Rational Rational::operator-(const Rational &rational) const
{
int iResultNumerator = m_iNumerator * rational.m_iDenominator-
rational.m_iNumerator * m_iDenominator;
int iResultDenominator = m_iDenominator * rational.m_iDenominator;
Rational result(iResultNumerator, iResultDenominator);
result.Normalize();
return result;
}
Rational Rational::operator*(const Rational &rational) const
{
int iResultNumerator = m_iNumerator * rational.m_iNumerator;
int iResultDenominator = m_iDenominator * rational.m_iDenominator;
Rational result(iResultNumerator, iResultDenominator);
result.Normalize();
return result;
}
Rational Rational::operator/(const Rational &rational) const
{
assert(rational.m_iNumerator != 0);
int iResultNumerator = m_iDenominator * rational.m_iDenominator;
int iResultDenominator = m_iNumerator * rational.m_iNumerator;
Rational result(iResultNumerator, iResultDenominator);
result.Normalize();
return result;
}
istream &operator>>(istream &inputStream, Rational &rational)
{
inputStream >> rational.m_iNumerator
>> rational.m_iDenominator;
return inputStream;
}
ostream &operator<<(ostream &outputStream,
const Rational &rational)
{
if(rational.m_iNumerator == 0)
{
outputStream << "0";
}
else if(rational.m_iDenominator == 1)
{
outputStream << "1";
}
else
{
outputStream << "(" << rational.m_iNumerator << "/"
<< rational.m_iDenominator << ")";
}
return outputStream;
}
void Rational::Normalize()
{
if(m_iNumerator == 0)
{
m_iDenominator = 1;
return;
}
if(m_iDenominator < 0)
{
m_iNumerator = -m_iNumerator;
m_iDenominator = -m_iDenominator;
}
int iGcd = GreatestCommonDivider(abs(m_iNumerator),
m_iDenominator);
m_iNumerator /= iGcd;
m_iDenominator /= iGcd;
}
int Rational::GreatestCommonDivider(int iNum1, int iNum2)
{
if(iNum1 > iNum2)
{
return GreatestCommonDivider(iNum1 - iNum2, iNum2);
}
else if (iNum2 > iNum1)
{
return GreatestCommonDivider(iNum1, iNum2 - iNum1);
}
else
{
return iNum1;
}
}
|