109 lines
2.4 KiB
C++
109 lines
2.4 KiB
C++
#include "exact_number.h"
|
|
|
|
using namespace std;
|
|
|
|
ExactNumber::ExactNumber(){
|
|
numer = 0;
|
|
denom = 1;
|
|
}
|
|
|
|
ExactNumber::ExactNumber(int n, int d){
|
|
numer = n;
|
|
denom = d;
|
|
simplify();
|
|
}
|
|
|
|
ExactNumber::ExactNumber(const ExactNumber& n) {
|
|
numer = n.numer;
|
|
denom = n.denom;
|
|
}
|
|
|
|
ExactNumber::~ExactNumber() {
|
|
}
|
|
|
|
ExactNumber& ExactNumber::operator=(const ExactNumber& rhs) {
|
|
numer = rhs.numer;
|
|
denom = rhs.denom;
|
|
return *this;
|
|
}
|
|
|
|
ExactNumber& ExactNumber::operator+=(const ExactNumber& rhs) {
|
|
numer = numer * rhs.denom + denom * rhs.numer;
|
|
denom *= rhs.denom;
|
|
simplify();
|
|
return *this;
|
|
}
|
|
|
|
ExactNumber& ExactNumber::operator-=(const ExactNumber& rhs) {
|
|
numer = numer * rhs.denom - denom * rhs.numer;
|
|
denom *= rhs.denom;
|
|
simplify();
|
|
return *this;
|
|
}
|
|
|
|
ExactNumber& ExactNumber::operator*=(const ExactNumber& rhs) {
|
|
numer *= rhs.numer;
|
|
denom *= rhs.denom;
|
|
simplify();
|
|
return *this;
|
|
}
|
|
|
|
ExactNumber& ExactNumber::operator/=(const ExactNumber& rhs) {
|
|
numer *= rhs.denom;
|
|
denom *= rhs.numer;
|
|
simplify();
|
|
return *this;
|
|
}
|
|
|
|
ExactNumber operator+(const ExactNumber& lhs, const ExactNumber& rhs) {
|
|
ExactNumber result(lhs);
|
|
result += rhs;
|
|
return result;
|
|
}
|
|
|
|
ExactNumber operator-(const ExactNumber& lhs, const ExactNumber& rhs) {
|
|
ExactNumber result(lhs);
|
|
result -= rhs;
|
|
return result;
|
|
}
|
|
|
|
ExactNumber operator*(const ExactNumber& lhs, const ExactNumber& rhs) {
|
|
ExactNumber result(lhs);
|
|
result *= rhs;
|
|
return result;
|
|
}
|
|
|
|
ExactNumber operator/(const ExactNumber& lhs, const ExactNumber& rhs) {
|
|
ExactNumber result(lhs);
|
|
result /= rhs;
|
|
return result;
|
|
}
|
|
|
|
bool ExactNumber::operator==(const ExactNumber& rhs) const {
|
|
return numer == rhs.numer && denom == rhs.denom;
|
|
}
|
|
|
|
bool ExactNumber::operator!=(const ExactNumber& rhs) const {
|
|
return numer != rhs.numer || denom != rhs.denom;
|
|
}
|
|
|
|
bool ExactNumber::operator<(const ExactNumber& rhs) const {
|
|
return numer * rhs.denom < denom * rhs.numer;
|
|
}
|
|
|
|
bool ExactNumber::operator>(const ExactNumber& rhs) const {
|
|
return numer * rhs.denom > denom * rhs.numer;
|
|
}
|
|
|
|
bool ExactNumber::operator<=(const ExactNumber& rhs) const {
|
|
return numer * rhs.denom <= denom * rhs.numer;
|
|
}
|
|
|
|
bool ExactNumber::operator>=(const ExactNumber& rhs) const {
|
|
return numer * rhs.denom >= denom * rhs.numer;
|
|
}
|
|
|
|
ostream& operator<<(ostream& os, const ExactNumber& n) {
|
|
os << n.numer << "/" << n.denom;
|
|
return os;
|
|
} |