[B]asic module for [unit] program testing:
 All Classes Namespaces Files Functions Variables Typedefs Enumerator Friends Defines
Public Member Functions | Private Member Functions | Private Attributes | Friends
rational< INT > Class Template Reference

La clase rational implementa las operaciones aritméticas principales para números rationales. More...

#include <rational.h>

List of all members.

Public Member Functions

 rational ()
 Constructor de vector.
 rational (INT num)
 Constructor a partir de un valor entero.
 rational (INT num, INT den)
 Constructor a partir de un valor quedbrado.
 rational (const rational &o)
 Constructor de copia.
 ~rational ()
 Destructor.
void set (const INT &n=INT(0), const INT &d=INT(1))
 Cambia el valor del número rational a "n/d".
const INT & num () const
 Acceso al numerador.
const INT & den () const
 Acceso al denomirador (siempre >0).
rationaloperator= (const rational &)
 Copia desde "o".
rationaloperator= (INT)
 Asignación desde un "INT".
rationalswap (rational &)
 Intercambia los valores de "*this" y "o".
rationaloperator+= (const rational &)
 Le suma a "*this" el valor de "otro".
rationaloperator-= (const rational &)
 Le resta a "*this" el valor de "otro".
rationaloperator*= (const rational &)
 Multiplica "*this" por "num".
rationaloperator/= (const rational &)
 Divide "*this" por el valor de "num".
rational operator- () const
 "-x".
rationalfromString (const char *nStr)
 Establece el varlor de "*this" a partir de la hilera "nStr".

Private Member Functions

void simplify ()
 Simplifica el numerador y el denomidador.

Private Attributes

INT m_num
 Numerador.
INT m_den
 Denominador.

Friends

class test_rational
 Datos de prueba para la clase.
template<class NUM >
rational< NUM > operator+ (const rational< NUM > &, const rational< NUM > &)
 "x+y".
template<class NUM >
rational< NUM > operator- (const rational< NUM > &, const rational< NUM > &)
 "x-y".
template<class NUM >
rational< NUM > operator* (const rational< NUM > &, const rational< NUM > &)
 "x*y".
template<class NUM >
rational< NUM > operator/ (const rational< NUM > &, const rational< NUM > &)
 "x/y".
template<class NUM >
bool operator== (const rational< NUM > &, const rational< NUM > &)
 ¿ x == y ?.
template<class NUM >
bool operator< (const rational< NUM > &, const rational< NUM > &)
 ¿ x < y ?
template<class NUM >
bool operator!= (const rational< NUM > &, const rational< NUM > &)
 ¿ x != y ?
template<class NUM >
bool operator<= (const rational< NUM > &, const rational< NUM > &)
 ¿ x <= y ?
template<class NUM >
bool operator>= (const rational< NUM > &, const rational< NUM > &)
 ¿ x >= y ?
template<class NUM >
bool operator> (const rational< NUM > &, const rational< NUM > &)
 ¿ x > y ?
template<class NUM >
rational< NUM > & operator++ (rational< NUM > &r)
 ++r.
template<class NUM >
rational< NUM > operator++ (rational< NUM > &r, int)
 r++.
template<class NUM >
rational< NUM > & operator-- (rational< NUM > &r)
 --r.
template<class NUM >
rational< NUM > operator-- (rational< NUM > &r, int)
 r--.
template<class NUM >
ostream & operator<< (ostream &, const rational< NUM > &)
 Graba el valor de "r" en el flujo "COUT".
template<class NUM >
istream & operator>> (istream &, rational< NUM > &)
 Lee del flujo de texto "CIN" el valor de "r".
template<class NUM >
double real (const rational< NUM > &)
 Convertidor a punto flotante.
template<class NUM >
long integer (const rational< NUM > &)
 Convertidor a punto fijo.
template<class NUM >
bool check_ok (const rational< NUM > &r)
 Verifica la invariante de la clase rational.

Detailed Description

template<class INT>
class rational< INT >

La clase rational implementa las operaciones aritméticas principales para números rationales.

Definition at line 33 of file rational.h.


Constructor & Destructor Documentation

template<class INT>
rational< INT >::rational ( ) [inline]

Constructor de vector.

See also:
test_rational<NUM>::test_constructor().

Definition at line 42 of file rational.h.

template<class INT>
rational< INT >::rational ( INT  num) [inline]

Constructor a partir de un valor entero.

Definition at line 43 of file rational.h.

template<class INT>
rational< INT >::rational ( INT  num,
INT  den 
) [inline]

Constructor a partir de un valor quedbrado.

Definition at line 44 of file rational.h.

template<class INT>
rational< INT >::rational ( const rational< INT > &  o) [inline]

Constructor de copia.

Definition at line 47 of file rational.h.

template<class INT>
rational< INT >::~rational ( ) [inline]

Destructor.

Definition at line 48 of file rational.h.


Member Function Documentation

template<class NUM >
void rational< NUM >::simplify ( ) [private]

Simplifica el numerador y el denomidador.

  • Transforma el número rational de manera que el numerador y el denominador sean primos relativos, asegurando además que el denominador es siempre positivo.
  • Si (m_num==0) ==> (m_den==1).
  • Simplifica la fracción para que m_num y m_den sean números primos relativos ie, mcd(m_num,m_den) == 1.
  • Asegura que m_den sea un número positivo.
  • Restaura la invariante de la clase rational.
    {{  // test::simplify()
        rational<INT> half(1,2);
        half.m_num *= 23;
        half.m_den *= 23;
        assertTrue( half != rational<INT>(23, 23*2) );
        half.simplify();
        assertTrue( half == rational<INT>(23, 23*2) );
    }}

See also:
test_rational<NUM>::test_simplify()

Definition at line 479 of file rational.h.

template<class INT >
void rational< INT >::set ( const INT &  n = INT(0),
const INT &  d = INT(1) 
) [inline]

Cambia el valor del número rational a "n/d".

Precondition:
d != 0 .
    {{  // test::set()
        rational<INT> r;
        assertTrue( r == rational<INT>( 0 ) );
        r.set(-3, -4);
        assertTrue( r == rational<INT>( 3*11 , 4*11 ) );
    }}

See also:
test_rational<NUM>::test_set()

Definition at line 123 of file rational.h.

template<class INT>
const INT& rational< INT >::num ( ) const [inline]

Acceso al numerador.

    {{  // test::num_den()
        rational<INT> r;       assertTrue( r.num() ==  0 && r.den() == 1 );
        r.set( -1*13, -4*13 ); assertTrue( r.num() ==  1 && r.den() == 4 );
        r.set( -2*11,  5*11 ); assertTrue( r.num() == -2 && r.den() == 5 );
        r.set(  2*17, -6*17 ); assertTrue( r.num() == -1 && r.den() == 3 );
    }}

See also:
test_rational<NUM>::test_num_den()

Definition at line 57 of file rational.h.

template<class INT>
const INT& rational< INT >::den ( ) const [inline]

Acceso al denomirador (siempre >0).

Definition at line 59 of file rational.h.

template<class INT >
rational< INT > & rational< INT >::operator= ( const rational< INT > &  o) [inline]

Copia desde "o".

  • El valor anterior de "*this" se pierde.
Complejidad:
O( 1 )
Returns:
*this
See also:
http://www.di-mare.com/adolfo/binder/c04.htm#sc05
    {{  // test::op_equal()
        rational<INT> r(1), s(18,56); assertTrue( r == rational<INT>(1) );
        r = s;                        assertTrue( r == ( r / ( r / s ) ) );
        r = 34;                       assertTrue( r == rational<INT>(34) );
        s = -3;                       assertTrue( s == rational<INT>(-3) );
    }}

See also:
test_rational<NUM>::test_op_equal()

Definition at line 152 of file rational.h.

template<class INT >
rational< INT > & rational< INT >::operator= ( INT  entero) [inline]

Asignación desde un "INT".

    {{  // test::op_equal()
        rational<INT> r(1), s(18,56); assertTrue( r == rational<INT>(1) );
        r = s;                        assertTrue( r == ( r / ( r / s ) ) );
        r = 34;                       assertTrue( r == rational<INT>(34) );
        s = -3;                       assertTrue( s == rational<INT>(-3) );
    }}

See also:
test_rational<NUM>::test_op_equal()

Definition at line 195 of file rational.h.

template<class INT >
rational< INT > & rational< INT >::swap ( rational< INT > &  o) [inline]

Intercambia los valores de "*this" y "o".

Complejidad:
O( 1 )
Returns:
*this
See also:
http://www.di-mare.com/adolfo/binder/c04.htm#sc08
    {{  // test::swap()
        rational<INT> r, s(18,56);  assertTrue( r == rational<INT>(0) );
        r.swap( s );                assertTrue( s == rational<INT>(0) );
        assertTrue( r == rational<INT>(18,56) );
    }}

See also:
test_rational<NUM>::test_swap()

Definition at line 172 of file rational.h.

template<class INT >
rational< INT > & rational< INT >::operator+= ( const rational< INT > &  otro)

Le suma a "*this" el valor de "otro".

    {{  // test::op_add_equal()
        rational<INT> add(0), sub(0);
        for ( int i=20; i>=-20; --i ) {
            add += rational<INT>(i-i, 20*i+1);
            sub -= rational<INT>(i-i, 20*i+1);
        }
        assertTrue( add == sub );
    }}

See also:
test_rational<NUM>::test_op_add_equal()

Definition at line 502 of file rational.h.

template<class INT >
rational< INT > & rational< INT >::operator-= ( const rational< INT > &  otro)

Le resta a "*this" el valor de "otro".

    {{  // test::op_add_equal()
        rational<INT> add(0), sub(0);
        for ( int i=20; i>=-20; --i ) {
            add += rational<INT>(i-i, 20*i+1);
            sub -= rational<INT>(i-i, 20*i+1);
        }
        assertTrue( add == sub );
    }}

See also:
test_rational<NUM>::test_op_add_equal()

Definition at line 516 of file rational.h.

template<class INT >
rational< INT > & rational< INT >::operator*= ( const rational< INT > &  num) [inline]

Multiplica "*this" por "num".

    {{  // test::op_mult_equal()
        rational<INT> mlt(1), div(1);
        for ( int i=15; i>=-15; --i ) {
            mlt *= rational<INT>(17*i-1, 13*i+1);
            div /= rational<INT>(13*i+1, 17*i-1);
        }
        assertTrue( mlt == div );
    }}

See also:
test_rational<NUM>::test_op_mult_equal()

Definition at line 208 of file rational.h.

template<class INT >
rational< INT > & rational< INT >::operator/= ( const rational< INT > &  num) [inline]

Divide "*this" por el valor de "num".

Precondition:
(num != 0)
    {{  // test::op_mult_equal()
        rational<INT> mlt(1), div(1);
        for ( int i=15; i>=-15; --i ) {
            mlt *= rational<INT>(17*i-1, 13*i+1);
            div /= rational<INT>(13*i+1, 17*i-1);
        }
        assertTrue( mlt == div );
    }}

See also:
test_rational<NUM>::test_op_mult_equal()

Definition at line 224 of file rational.h.

template<class INT >
rational< INT > rational< INT >::operator- ( ) const [inline]

"-x".

  • Menos unario.
  • Calcula y retorna el valor "-x".
    {{  // test::op_minus()
        rational<INT> half_n(1,-2), quarter_n(-1,4);
        assertTrue( - half_n == rational<INT>(-1) * half_n );
        assertTrue( half_n * half_n == - quarter_n );
        assertTrue( half_n * half_n * half_n == - quarter_n * half_n );
    }}

See also:
test_rational<NUM>::test_op_minus()

Definition at line 241 of file rational.h.

template<class NUM >
rational< NUM > & rational< NUM >::fromString ( const char *  nStr)

Establece el varlor de "*this" a partir de la hilera "nStr".

Precondition:
"nStr" debe estar escrita en el formato "[num/den]".
    {{  // test::fromString()
        rational<INT> r, half(1,2), quarter(1,4);
        assertTrue( - half  == r.fromString(  "[---12/24]" ) );
        assertTrue( quarter == r.fromString( "[ -03/ -12 ]") );
        r.fromString( "[ -+-+-+-+- 4 / -- -+ -- 32  ]" );
        assertTrue( r == rational<INT>(1,8) );
    }}

See also:
test_rational<NUM>::test_fromString()

Definition at line 648 of file rational.h.


Friends And Related Function Documentation

template<class INT>
friend class test_rational [friend]

Datos de prueba para la clase.

Definition at line 104 of file rational.h.

template<class INT>
template<class NUM >
rational<NUM> operator+ ( const rational< NUM > &  x,
const rational< NUM > &  y 
) [friend]

"x+y".

  • Calcula y retorna la suma "x+y".
    {{  // test::op_add()
        rational<INT> add(0), sub(0);
        for ( int i=20; i>=-20; --i ) {
            add = add + rational<INT>(i-i, 20*i+1);
            sub = sub - rational<INT>(i-i, 20*i+1);
        }
        assertTrue( add == sub );
    }}

See also:
test_rational<NUM>::test_op_add()

Definition at line 712 of file rational.h.

template<class INT>
template<class NUM >
rational<NUM> operator- ( const rational< NUM > &  x,
const rational< NUM > &  y 
) [friend]

"x-y".

  • Calcula y retorna la resta "x-y".
    {{  // test::op_add()
        rational<INT> add(0), sub(0);
        for ( int i=20; i>=-20; --i ) {
            add = add + rational<INT>(i-i, 20*i+1);
            sub = sub - rational<INT>(i-i, 20*i+1);
        }
        assertTrue( add == sub );
    }}

See also:
test_rational<NUM>::test_op_add()

Definition at line 729 of file rational.h.

template<class INT>
template<class NUM >
rational<NUM> operator* ( const rational< NUM > &  x,
const rational< NUM > &  y 
) [friend]

"x*y".

  • Calcula y retorna la multiplicación "x*y".
    {{  // test::op_mult()
        rational<INT> mlt(1), div(1);
        for ( int i=15; i>=-15; --i ) {
            mlt = mlt * rational<INT>(17*i-1, 13*i+1);
            div = div / rational<INT>(13*i+1, 17*i-1);
        }
        assertTrue( mlt == div );
    }}

See also:
test_rational<NUM>::test_op_mult()

Definition at line 746 of file rational.h.

template<class INT>
template<class NUM >
rational<NUM> operator/ ( const rational< NUM > &  x,
const rational< NUM > &  y 
) [friend]

"x/y".

  • Calcula y retorna la división "x/y".
Precondition:
y != 0
    {{  // test::op_mult()
        rational<INT> mlt(1), div(1);
        for ( int i=15; i>=-15; --i ) {
            mlt = mlt * rational<INT>(17*i-1, 13*i+1);
            div = div / rational<INT>(13*i+1, 17*i-1);
        }
        assertTrue( mlt == div );
    }}
See also:
test_rational<NUM>::test_op_mult()

Definition at line 764 of file rational.h.

template<class INT>
template<class NUM >
bool operator== ( const rational< NUM > &  x,
const rational< NUM > &  y 
) [friend]

¿ x == y ?.

    {{  // test::op_comp()
        rational<INT> neg_half(-1,2), quarter(1,4);
        assertTrue( neg_half == -(-neg_half) );
        assertTrue( neg_half <  quarter  );
        assertTrue( quarter  >  neg_half );
        assertTrue( neg_half <= quarter  );
        assertTrue( quarter  >= neg_half );
        assertTrue( neg_half != quarter  );
    }}

See also:
test_rational<NUM>::test_op_comp()

Definition at line 254 of file rational.h.

template<class INT>
template<class NUM >
bool operator< ( const rational< NUM > &  x,
const rational< NUM > &  y 
) [friend]

¿ x < y ?

Definition at line 266 of file rational.h.

template<class INT>
template<class NUM >
bool operator!= ( const rational< NUM > &  x,
const rational< NUM > &  y 
) [friend]

¿ x != y ?

Definition at line 295 of file rational.h.

template<class INT>
template<class NUM >
bool operator<= ( const rational< NUM > &  x,
const rational< NUM > &  y 
) [friend]

¿ x <= y ?

Definition at line 301 of file rational.h.

template<class INT>
template<class NUM >
bool operator>= ( const rational< NUM > &  x,
const rational< NUM > &  y 
) [friend]

¿ x >= y ?

Definition at line 307 of file rational.h.

template<class INT>
template<class NUM >
bool operator> ( const rational< NUM > &  x,
const rational< NUM > &  y 
) [friend]

¿ x > y ?

Definition at line 289 of file rational.h.

template<class INT>
template<class NUM >
rational<NUM>& operator++ ( rational< NUM > &  r) [friend]

++r.

    {{  // test::op_cpp()
        rational<INT> r(3,2);

        assertTrue( r++ == rational<INT>(3,2) );
        assertTrue( r   == rational<INT>(5,2) );

        assertTrue( r-- == rational<INT>(5,2) );
        assertTrue( r   == rational<INT>(3,2) );

        assertTrue( --r == rational<INT>(1,2) );
    }}

See also:
test_rational<NUM>::test_op_cpp()

Definition at line 789 of file rational.h.

template<class INT>
template<class NUM >
rational<NUM> operator++ ( rational< NUM > &  r,
int   
) [friend]

r++.

Definition at line 796 of file rational.h.

template<class INT>
template<class NUM >
rational<NUM>& operator-- ( rational< NUM > &  r) [friend]

--r.

    {{  // test::op_cpp()
        rational<INT> r(3,2);

        assertTrue( r++ == rational<INT>(3,2) );
        assertTrue( r   == rational<INT>(5,2) );

        assertTrue( r-- == rational<INT>(5,2) );
        assertTrue( r   == rational<INT>(3,2) );

        assertTrue( --r == rational<INT>(1,2) );
    }}

See also:
test_rational<NUM>::test_op_cpp()

Definition at line 809 of file rational.h.

template<class INT>
template<class NUM >
rational<NUM> operator-- ( rational< NUM > &  r,
int   
) [friend]

r--.

Definition at line 816 of file rational.h.

template<class INT>
template<class NUM >
ostream& operator<< ( ostream &  COUT,
const rational< NUM > &  r 
) [friend]

Graba el valor de "r" en el flujo "COUT".

  • Graba el valor en el formato [num/den].
  • En particular, este es el operador que se invoca cuando se usa, por ejemplo, este tipo de instrucción:
              cout << r << q;
    
    {{  // test::op_out()
        std::basic_ostringstream<char> ost; // receptor de salida
        ost.str(""); ost << rational<INT>(-1,2);  assertTrue( ost.str() == "[-1/2]" );
        ost.str(""); ost << rational<INT>(-12);   assertTrue( ost.str() == "[-12]"  );
        ost.str(""); ost << rational<INT>(1-1,8); assertTrue( ost.str() == "[0]"    );
        ost.str(""); // Borra el receptor de salida
        ost << rational<INT>(-1,2) << rational<INT>(-12) << rational<INT>(1-1,8);
        assertTrue( ost.str() == "[-1/2][-12][0]" );
    }}

See also:
test_rational<NUM>::test_op_out()

Definition at line 543 of file rational.h.

template<class INT>
template<class NUM >
istream& operator>> ( istream &  CIN,
rational< NUM > &  r 
) [friend]

Lee del flujo de texto "CIN" el valor de "r".

Precondition:
El número rational debe haber sido escrito usando el formato "[r/den]", aunque es permisible usar algunos blancos.
  • Se termina de leer el valor sólo cuando encuentra "]".
  • [ -+-+-+-+- 4 / -- -+ -- 32 ] se lee como [1/8]
    {{  // test::op_in()
        std::basic_istringstream<char> ist( "[-1/2] [-12] [0]" );
        rational<INT> r(0); ist >> r;  assertTrue( r == rational<INT>(-1,2) );
        rational<INT> s(1); ist >> s;  assertTrue( s == rational<INT>(-12)  );
        rational<INT> t(2); ist >> t;  assertTrue( t == rational<INT>(0)    );

        ist.str( "[ -+-+-+-+- 4 / -- -+ -- 32  ]" );
        rational<INT> u(3); ist >> u;  assertTrue( u == rational<INT>(1,8) );
    }}

See also:
test_rational<NUM>::test_op_in()

Definition at line 566 of file rational.h.

template<class INT>
template<class NUM >
double real ( const rational< NUM > &  num) [friend]

Convertidor a punto flotante.

Definition at line 313 of file rational.h.

template<class INT>
template<class NUM >
long integer ( const rational< NUM > &  num) [friend]

Convertidor a punto fijo.

Definition at line 319 of file rational.h.

template<class INT>
template<class NUM >
bool check_ok ( const rational< NUM > &  r) [friend]

Verifica la invariante de la clase rational.

Rep Modelo de la clase:
    +---+
    | 3 | <==  m_num == numerador del número racional
    +---+
    |134| <==  m_den == denominador del número racional
    +---+
Remarks:
Libera al programador de implementar el método Ok()
    {{  // test::check_ok()
        rational<INT> r, *nul=0;      assertFalse( check_ok(*nul) );
        r.m_num =  2;  r.m_den =  0;  assertFalse( check_ok(  r ) );
        r.m_num =  2;  r.m_den = -1;  assertFalse( check_ok(  r ) );
        r.m_num =  0;  r.m_den =  2;  assertFalse( check_ok(  r ) );
        r.m_num = 31;  r.m_den = 31;  assertFalse( check_ok(  r ) );
        r.simplify();                 assertTrue ( check_ok(  r ) );
    }}

See also:
test_rational<NUM>::test_check_ok()
  • Invariante: ningún objeto puede estar almacenado en la posición nula.
  • Invariante: el denominador debe ser un número positivo.
  • Invariante: el cero debe representarse con denominador igual a "1".
  • Invariante: el numerador y el denominador deben ser primos relativos.

Definition at line 356 of file rational.h.


Member Data Documentation

template<class INT>
INT rational< INT >::m_num [private]

Numerador.

Definition at line 35 of file rational.h.

template<class INT>
INT rational< INT >::m_den [private]

Denominador.

Definition at line 36 of file rational.h.


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