/******************************************************* FILE NAME = rational.cpp (problem 1) PROGRAMMER NAME - Justin C. Miller DATE - 09/17/2003 BUGS - I believe that everything works properly DESCRIPTION - This program defines the functions in rational.h This is for Problem 1 *******************************************************/ #ifndef rational_cpp #define rational_cpp #include #include #include #include "rational.h" using namespace std; // Problem 1: #1, ceiling and floor int rational::ceiling(void) const { if(this->numerator() == 0) return 0 ; // if zero else if(this->numerator() > 0){ int result = this->numerator() / this->denominator() ; if( this->numerator() % this->denominator() != 0) result++ ; return result ; }else{ return this->numerator() / this->denominator() ; }//else }//end of ceiling int rational::floor(void) const { if(this->numerator() >= 0) return this->numerator() / this->denominator() ; else{ int result = this->numerator() / this->denominator() ; if(this->numerator() % this->denominator() != 0) result-- ; return result ; }//else }//end of ceiling // Problem 1: #2, overloaded +,-,*,/ rational operator+(const rational & left, const rational & right){ rational r(left.numerator()*right.denominator() + left.denominator()*right.numerator(), left.denominator() * right.denominator()) ; // the constructor normalizes for me return r; }//end of operator+ rational operator-(const rational & left, const rational & right){ rational r(left.numerator()*right.denominator() - left.denominator()*right.numerator(), left.denominator() * right.denominator()) ; // the constructor normalizes for me return r; }//end of operator- rational operator*(const rational & left, const rational & right){ rational r(left.numerator()*right.numerator(), left.denominator() * right.denominator()) ; // the constructor normalizes for me return r; }//end of operator* rational operator/(const rational & left, const rational & right){ rational r(left.numerator()*right.denominator(), left.denominator() * right.numerator()) ; // the constructor normalizes for me return r; }//end of operator/ // Problem 1: #3, overloaded << and >> ostream & operator<<(ostream & out, const rational & r){ out << r.numerator() << "/" << r.denominator() ; return out ; }//end of operator<< istream & operator>>(istream & in, rational & r){ char slash ; // because i'm a friend i have access to r's top and bottom in >> r.top >> slash >> r.bottom ; r.normalize() ; return in ; }//end of operator>> int gcd(int x, int y) { int a = y; while (a != 0) { y = a; a = x % a; x = y; } return x; } rational::rational() { top=0; bottom=1; } rational::rational(int i) { top=i; bottom=1; } rational::rational(int i, int j) { assert(j!=0); top=i; bottom=j; normalize(); } rational::rational(const rational &source) { top=source.top; bottom=source.bottom; } int rational::numerator() const{ return top; } int rational::denominator() const{ return bottom; } void rational::operator = (const rational &r) { top = r.top; bottom = r.bottom; } void rational::operator += (const rational &r) { top = top * r.bottom + r.top * bottom; bottom = r.bottom * bottom; (*this).normalize(); } void rational::operator -= (const rational &r) { top = top * r.bottom - r.top * bottom; bottom = r.bottom * bottom; (*this).normalize(); } void rational::operator *= (const rational &r) { top = top * r.top; bottom = bottom * r.bottom; (*this).normalize(); } void rational::operator /= (const rational &r) { top = top * r.bottom; bottom = bottom * r.top; (*this).normalize(); } // returns 1 if p > q, returns 0 if p = q, and -1 if p < q int rational::compare(const rational &r) const { if (top * r.bottom > bottom * r.top) return 1; if (top * r.bottom < bottom * r.top) return -1; return 0; } void rational::normalize() { bool flag = false ; if(top < 0){ flag = true ; top = top * -1 ; } int m = gcd(top, bottom); assert(m != 0); top /= m; bottom /= m; if(flag) top = top * -1 ; } #endif