code:

Can't Copy and Paste this?

Click here for a copy-and-paste friendly version of this code!

 

Terms of Agreement:   

By using this code, you agree to the following terms...   

1) You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for langauges that allow it) freely and with no charge.   

2) You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   

3) You may link to this code from another website, but ONLY if it is not wrapped in a frame. 

4) You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.  

//     

rational_h

# ifndef RATIONAL_H

# define RATIONAL_H

#include 

    class Rational {

    

     private:

     long num; // numerator

     long den; // denominator

     long gcd (long n, long m);

     void Reduce();

     public:

     //Rational(); //default constructor

     Rational(long=0, long=1);

     ~Rational();

    

     void printFP();

     friend istream& operator >> (istream&, Rational& r1);

    friend ostream& operator << (ostream&, Rational& r2); 

    

     // addition, subtraction, multiplication, division

     Rational operator + (Rational& e);

    Rational operator - (Rational& e);

    Rational operator * (Rational& e);

    Rational operator / (Rational& e);

    

     // Overload the relational and equality operators for this class.

     int operator > (Rational& e);

    int operator >= (Rational& e);

     int operator < (Rational& e);

    int operator <= (Rational& e);

    int operator == (Rational& e);

     int operator != (Rational& e);

};

# endif

rational.cpp

#include 

#include 

# include "rational.h"

    Rational::Rational(long n, long d) {

     num = n;

     den = d;

}

    Rational::~Rational() {

}

// addition

    Rational Rational:: operator + (Rational& e) {

     return Rational ( num * e.den + den * e.num, den * e.den) ;

}

// subraction

Rational Rational:: operator - (Rational& e) { 

return Rational ( num * e.den - den * e.num, den * e.den) ;

}

// division

Rational Rational:: operator / (Rational& e) {

Rational temp = Rational (num * e.den, den * e.num);

temp.Reduce(); 

return temp;

}

// multiplication 

Rational Rational:: operator * (Rational& e) { 

Rational temp = Rational (num * e.num, den * e.den); 

return temp; 

//) Overload the relational and equality

//     operators for this class.

// equality

int Rational::operator == (Rational& v) {

return num * v.den == den * v.num ;

}

// not equal

int Rational::operator != (Rational& v) { 

return num * v.den != den * v.num ; 

}

// greater

int Rational::operator > (Rational& v) { 

return num * v.den > den * v.num ; 

}

// greater than equal 

int Rational::operator >= (Rational& v) { 

return num * v.den >= den * v.num ; 

}

// less than equal

int Rational::operator <= (Rational& v) { 

return num * v.den <= den * v.num ; 

}

// less

int Rational::operator < (Rational& v) { 

return num * v.den < den * v.num ; 

}

//print double

void Rational::printFP(){

cout <<"\n d in double number form"<< double(num)/den;

}

//greatest common denominator

long Rational::gcd (long n,long m)

int tmp;

if(m==0)

tmp=n;

else

tmp=gcd(m,n%m);

return tmp;

//reduce

void Rational::Reduce()

long d, sign; 

sign = 1; 

if (num < 0) { 

sign = -1; 

num = -num; 

d = gcd(num, den); 

num = sign*(num / d); 

den = den / d; 

// cin

istream& operator >> (istream& in, Rational& temp) {

in.unsetf(ios::skipws);

// for slash chracter input

char c;

cout << "\n Enter the Rational Number below" << endl;

in >> temp.num >> c >> temp.den;

// checking for denominator=0

if (temp.den == 0) {

cerr << "invalid data ";

}

temp.Reduce();

return in;

}

// cout

ostream& operator << (ostream& out, Rational& temp) {

temp.Reduce();

out << temp.num << "/" << temp.den;

return out;

}

rational_process.cpp

#include 

#include "rational.cpp"

void main() {

Rational a, b(5), c(6,2), d, e;

e = a + b * c;

cout << "a="<

cin >> d;

cout << "\nd=" << d;

d.printFP();

cout << ((b > c) ? "\n b is larger\n" : "\n c is larger\n");

}