Public Member Functions | Protected Attributes | Friends

Rational Class Reference

Rational class implements standard methods and data structures to manipulate Rational ratios arithmetically. More...

#include <Rational.h>

List of all members.

Public Member Functions

 Rational ()
 Default constructor.
 Rational (Integer i)
 Default constructor.
 Rational (Const Rational &x)
 Copy constructor.
 Rational (Real x)
 Construct Rational given a fractional value.
 Rational (Integer num, Integer den)
 Construct Rational given a rational fraction value.
 Rational (Real x, Integer &num, Integer &den)
 Construct Rational given a Real fractional value, determine its rational approximation, assign numerator and denominator to reference arguments.
 Rational (Rational x, Integer &num, Integer &den)
 Construct Rational given a Rational, determine its rational approximation, assign numerator and denominator to reference arguments.
Rationaloperator= (Const Rational &x)
 Rational assignment operator.
Bool operator== (Const Rational &x) Const
 Rational equality operator.
Bool operator!= (Const Rational &x) Const
 Rational inequality operator.
Bool operator>= (Const Rational &x) Const
 Rational >= operator.
Bool operator> (Const Rational &x) Const
 Rational > operator.
Bool operator< (Const Rational &x) Const
 Rational < operator.
Bool operator<= (Const Rational &x) Const
 Rational <= operator.
Rationaloperator+= (Const Real x)
 Rational plus-equals operator.
Rationaloperator+= (Rational x)
 Rational plus-equals operator.
Rationaloperator-= (Const Real x)
 Rational minus-equals operator.
Rationaloperator-= (Rational x)
 Rational minus-equals operator.
Rationaloperator*= (Const Real x)
 Rational times-equals operator.
Rationaloperator*= (Rational x)
 Rational times-equals operator.
Rationaloperator/= (Const Real x)
 Rational divide-equals operator.
Rationaloperator/= (Rational x)
 Rational divide-equals operator.
Rational operator+ (Rational &x)
 Rational addition operator.
Rational operator- (Rational &x)
 Rational subtraction operator.
Rational operator* (Rational &x)
 Rational times operator.
Rational operator/ (Rational &x)
 Rational divide operator.
Rational operator+ (Const Real x)
 Rational add to Real operator.
Rational operator- (Const Real x)
 Rational subtract from Real operator.
Rational operator* (Const Real x)
 Rational multiply by Real operator.
Rational operator/ (Const Real x)
 Rational divide by Real operator.
String print (String s=0) Const
 Print Rational prefixed by String.

Protected Attributes

Integer m_num
Integer m_den
Static char s_buf [128]

Friends

ostream & operator<< (ostream &, Rational &)
istream & operator>> (istream &is, Rational &p)
Bool operator== (Integer &i, Rational &p)
Bool operator== (Rational &p, Integer &i)
Bool operator!= (Integer &i, Rational &p)
Bool operator!= (Rational &p, Integer &i)
Bool operator>= (Rational &p, Integer &r)
Bool operator>= (Integer &i, Rational &p)
Bool operator> (Rational &p, Integer &i)
Bool operator> (Integer &i, Rational &p)
Bool operator<= (Rational &p, Integer &i)
Bool operator<= (Integer &i, Rational &p)
Bool operator== (Real &r, Rational &p)
Bool operator== (Rational &p, Real &r)
Bool operator!= (Real &r, Rational &p)
Bool operator!= (Rational &p, Real &r)
Bool operator>= (Rational &p, Real &r)
Bool operator>= (Real &r, Rational &p)
Bool operator> (Rational &p, Real &r)
Bool operator> (Real &r, Rational &p)
Bool operator<= (Rational &p, Real &r)
Bool operator<= (Real &r, Rational &p)

Detailed Description

Rational class implements standard methods and data structures to manipulate Rational ratios arithmetically.

Definition at line 32 of file Rational.h.


Constructor & Destructor Documentation

Rational::Rational (  ) [inline]

Default constructor.

Definition at line 62 of file Rational.h.

Referenced by operator*(), operator+(), operator+=(), operator-(), and operator/().

: m_num(0), m_den(0) { }
Rational::Rational ( Integer  i ) [inline]

Default constructor.

Definition at line 65 of file Rational.h.

: m_num(i), m_den(i) { }
Rational::Rational ( Const Rational x ) [inline]

Copy constructor.

Definition at line 68 of file Rational.h.

: m_num(x.m_num), m_den(x.m_den) { }
Rational::Rational ( Real  x ) [inline]

Construct Rational given a fractional value.

Parameters:
xRational fraction expressed as a Real

Definition at line 72 of file Rational.h.

References m_den, m_num, and RealToRational().

: m_num(0), m_den(0) { RealToRational( x, m_num, m_den ); }
Rational::Rational ( Integer  num,
Integer  den 
) [inline]

Construct Rational given a rational fraction value.

Parameters:
numNumerator of rational fraction
denDenominator of rational fraction

Definition at line 77 of file Rational.h.

References If, and m_den.

                                           : m_num(num), m_den(den) {
                If (m_den == 0)
                        m_den = 1;
        }
Rational::Rational ( Real  x,
Integer num,
Integer den 
) [inline]

Construct Rational given a Real fractional value, determine its rational approximation, assign numerator and denominator to reference arguments.

Parameters:
xReal fractional reference value
numReference Integer numerator of derived rational fraction approximation
denReference Integer denominator of derived rational fraction approximation

Definition at line 86 of file Rational.h.

References m_den, m_num, and RealToRational().

                                                     : m_num(0), m_den(0) { 
                RealToRational( x, num, den ); 
                m_num = num;
                m_den = den;
        }
Rational::Rational ( Rational  x,
Integer num,
Integer den 
) [inline]

Construct Rational given a Rational, determine its rational approximation, assign numerator and denominator to reference arguments.

Parameters:
xRational value
numReference Integer numerator of derived rational fraction approximation
denReference Integer denominator of derived rational fraction approximation

Definition at line 96 of file Rational.h.

References m_den, m_num, and RealToRational().

                                                         : m_num(0), m_den(0) { 
                RealToRational( x.quotient(), num, den ); 
                m_num = num;
                m_den = den;
        }

Member Function Documentation

Bool Rational::operator!= ( Const Rational x ) [inline]

Rational inequality operator.

Definition at line 115 of file Rational.h.

References m_den, m_num, and Return.

                                                 {
                Return m_num != x.m_num || m_den != x.m_den;
        }
Rational Rational::operator* ( Rational x ) [inline]

Rational times operator.

Definition at line 190 of file Rational.h.

References Rational(), and Return.

                                        {
                Return( Rational(quotient() * x.quotient()) );
        }
Rational Rational::operator* ( Const Real  x ) [inline]

Rational multiply by Real operator.

Definition at line 210 of file Rational.h.

References Rational(), and Return.

                                         {
                Return( Rational(quotient() * x) );
        }
Rational& Rational::operator*= ( Const Real  x ) [inline]

Rational times-equals operator.

Definition at line 160 of file Rational.h.

References Return.

                                           {
                Return( *this = quotient() * x );
        }
Rational& Rational::operator*= ( Rational  x ) [inline]

Rational times-equals operator.

Definition at line 165 of file Rational.h.

References Return.

                                         {
                Return( *this = quotient() * x.quotient() );
        }
Rational Rational::operator+ ( Rational x ) [inline]

Rational addition operator.

Definition at line 180 of file Rational.h.

References Rational(), and Return.

                                        {
                Return( Rational(quotient() + x.quotient()) );
        }
Rational Rational::operator+ ( Const Real  x ) [inline]

Rational add to Real operator.

Definition at line 200 of file Rational.h.

References Rational(), and Return.

                                         {
                Return( Rational(quotient() + x) );
        }
Rational& Rational::operator+= ( Rational  x ) [inline]

Rational plus-equals operator.

Definition at line 145 of file Rational.h.

References Rational(), and Return.

                                         {
                Return( *this = Rational(quotient() + x.quotient() ) );
        }
Rational& Rational::operator+= ( Const Real  x ) [inline]

Rational plus-equals operator.

Definition at line 140 of file Rational.h.

References Rational(), and Return.

                                           {
                Return( *this = Rational(quotient() + x) );
        }
Rational Rational::operator- ( Rational x ) [inline]

Rational subtraction operator.

Definition at line 185 of file Rational.h.

References Rational(), and Return.

                                        {
                Return( Rational(quotient() - x.quotient()) );
        }
Rational Rational::operator- ( Const Real  x ) [inline]

Rational subtract from Real operator.

Definition at line 205 of file Rational.h.

References Rational(), and Return.

                                         {
                Return( Rational(quotient() - x) );
        }
Rational& Rational::operator-= ( Rational  x ) [inline]

Rational minus-equals operator.

Definition at line 155 of file Rational.h.

References Return.

                                         {
                Return( *this = quotient() - x.quotient() );
        }
Rational& Rational::operator-= ( Const Real  x ) [inline]

Rational minus-equals operator.

Definition at line 150 of file Rational.h.

References Return.

                                           {
                Return( *this = quotient() - x );
        }
Rational Rational::operator/ ( Const Real  x ) [inline]

Rational divide by Real operator.

Definition at line 215 of file Rational.h.

References Rational(), and Return.

                                         {
                Return( Rational(quotient() / x ) );
        }
Rational Rational::operator/ ( Rational x ) [inline]

Rational divide operator.

Definition at line 195 of file Rational.h.

References Rational(), and Return.

                                        {
                Return( Rational(quotient() / x.quotient()) );
        }
Rational& Rational::operator/= ( Rational  x ) [inline]

Rational divide-equals operator.

Definition at line 175 of file Rational.h.

References Return.

                                         {
                Return( *this = quotient() / x.quotient() );
        }
Rational& Rational::operator/= ( Const Real  x ) [inline]

Rational divide-equals operator.

Definition at line 170 of file Rational.h.

References Return.

                                           {
                Return( *this = quotient() / x );
        }
Bool Rational::operator< ( Const Rational x ) [inline]

Rational < operator.

Definition at line 130 of file Rational.h.

References Return.

                                                {
                Return quotient() < x.quotient();
        }
Bool Rational::operator<= ( Const Rational x ) [inline]

Rational <= operator.

Definition at line 135 of file Rational.h.

References Return.

                                                 {
                Return quotient() <= x.quotient();
        }
Rational& Rational::operator= ( Const Rational x ) [inline]

Rational assignment operator.

Definition at line 103 of file Rational.h.

References m_den, m_num, and Return.

                                               {
                m_num = x.m_num;
                m_den = x.m_den;
                Return *this;
        }
Bool Rational::operator== ( Const Rational x ) [inline]

Rational equality operator.

Definition at line 110 of file Rational.h.

References And, m_den, m_num, and Return.

                                                 {
                Return m_num == x.m_num  And m_den == x.m_den;
        }
Bool Rational::operator> ( Const Rational x ) [inline]

Rational > operator.

Definition at line 125 of file Rational.h.

References Return.

                                                {
                Return quotient() > x.quotient();
        }
Bool Rational::operator>= ( Const Rational x ) [inline]

Rational >= operator.

Definition at line 120 of file Rational.h.

References Return.

                                                 {
                Return quotient() >= x.quotient();
        }
String Rational::print ( String  s = 0 ) [inline]

Print Rational prefixed by String.

Parameters:
sPrefix string

Definition at line 221 of file Rational.h.

References If, m_den, m_num, Return, and s_buf.

Referenced by operator<<().

                                         {
                If (s)
                        cout << s;
#ifdef _MSC_VER
// Disable Microsoft Visual Studio warning about sprintf()
// until is universal agreement about how to handle its insecurities
#pragma warning(push)
#pragma warning(disable: 4996)
                sprintf(s_buf, "{%d,%d}", m_num, m_den);
#pragma warning(pop)
#else
                sprintf(s_buf, "{%d,%d}", m_num, m_den);
#endif
                Return s_buf;
        }

Friends And Related Function Documentation

Bool operator!= ( Integer i,
Rational p 
) [friend]

Definition at line 39 of file Rational.h.

{ Return i != p.quotient(); }
Bool operator!= ( Rational p,
Real r 
) [friend]

Definition at line 51 of file Rational.h.

{ Return p.quotient() != r; }
Bool operator!= ( Rational p,
Integer i 
) [friend]

Definition at line 40 of file Rational.h.

{ Return p.quotient() != i; }
Bool operator!= ( Real r,
Rational p 
) [friend]

Definition at line 50 of file Rational.h.

{ Return r != p.quotient(); }
ostream& operator<< ( ostream &  os,
Rational r 
) [friend]

Definition at line 21 of file Rational.cpp.

{
        os << r.print();
        Return os;
}
Bool operator<= ( Rational p,
Integer i 
) [friend]

Definition at line 45 of file Rational.h.

{ Return p.quotient() <= i; }
Bool operator<= ( Rational p,
Real r 
) [friend]

Definition at line 56 of file Rational.h.

{ Return p.quotient() <= r; }
Bool operator<= ( Real r,
Rational p 
) [friend]

Definition at line 57 of file Rational.h.

{ Return r <= p.quotient(); }
Bool operator<= ( Integer i,
Rational p 
) [friend]

Definition at line 46 of file Rational.h.

{ Return i <= p.quotient(); }
Bool operator== ( Rational p,
Integer i 
) [friend]

Definition at line 38 of file Rational.h.

{ Return p.quotient() == i; }
Bool operator== ( Integer i,
Rational p 
) [friend]

Definition at line 37 of file Rational.h.

{ Return i == p.quotient(); }
Bool operator== ( Real r,
Rational p 
) [friend]

Definition at line 48 of file Rational.h.

{ Return r == p.quotient(); }
Bool operator== ( Rational p,
Real r 
) [friend]

Definition at line 49 of file Rational.h.

{ Return p.quotient() == r; }
Bool operator> ( Rational p,
Integer i 
) [friend]

Definition at line 43 of file Rational.h.

{ Return p.quotient() > i; }
Bool operator> ( Rational p,
Real r 
) [friend]

Definition at line 54 of file Rational.h.

{ Return p.quotient() > r; }
Bool operator> ( Integer i,
Rational p 
) [friend]

Definition at line 44 of file Rational.h.

{ Return i > p.quotient(); }
Bool operator> ( Real r,
Rational p 
) [friend]

Definition at line 55 of file Rational.h.

{ Return r > p.quotient(); }
Bool operator>= ( Rational p,
Integer r 
) [friend]

Definition at line 41 of file Rational.h.

{ Return p.quotient() >= r; }
Bool operator>= ( Rational p,
Real r 
) [friend]

Definition at line 52 of file Rational.h.

{ Return p.quotient() >= r; }
Bool operator>= ( Integer i,
Rational p 
) [friend]

Definition at line 42 of file Rational.h.

{ Return i >= p.quotient(); }
Bool operator>= ( Real r,
Rational p 
) [friend]

Definition at line 53 of file Rational.h.

{ Return r >= p.quotient(); }
istream& operator>> ( istream &  is,
Rational p 
) [friend]

Definition at line 27 of file Rational.cpp.

{
        char ch;
        is >> ch && (ch == '{' || ch == '(')
                && is >> p.m_num >> ch && ch == ','
                && is >> p.m_den >> ch && (ch == '}' || ch == '(');
        Return is;
}

Member Data Documentation

Integer Rational::m_den [protected]

Definition at line 244 of file Rational.h.

Referenced by operator!=(), operator=(), operator==(), operator>>(), print(), and Rational().

Integer Rational::m_num [protected]

Definition at line 243 of file Rational.h.

Referenced by operator!=(), operator=(), operator==(), operator>>(), print(), and Rational().

Character Rational::s_buf [protected]

Definition at line 245 of file Rational.h.

Referenced by print().


The documentation for this class was generated from the following files: