[B]asic module for [unit] program testing:
 All Classes Namespaces Files Functions Variables Typedefs Enumerator Friends Defines
Classes | Namespaces | Defines | Functions
rational.h File Reference

Declara el tipo "rational". More...

#include <iostream>
#include <cstdlib>
#include <cctype>

Go to the source code of this file.

Classes

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

Namespaces

namespace  std
 Defined by the C++ estándar library.

Defines

#define rational_h
 Evita la inclusión múltiple.

Functions

template<class NUM >
NUM mcd (NUM x, NUM y)
 Calcula el Máximo Común Divisor de los números "x" y "y".
template<class INT >
INT gcd (const INT &x, const INT &y)
 Sinónimo de mcd(x,y).
template<class NUM >
bool operator== (const rational< NUM > &x, const rational< NUM > &y)
 ¿ x == y ?.
template<class NUM >
bool operator< (const rational< NUM > &x, const rational< NUM > &y)
 ¿ x < y ?
template<class NUM >
bool operator> (const rational< NUM > &x, const rational< NUM > &y)
 ¿ x > y ?
template<class NUM >
bool operator!= (const rational< NUM > &x, const rational< NUM > &y)
 ¿ x != y ?
template<class NUM >
bool operator<= (const rational< NUM > &x, const rational< NUM > &y)
 ¿ x <= y ?
template<class NUM >
bool operator>= (const rational< NUM > &x, const rational< NUM > &y)
 ¿ x >= y ?
template<class NUM >
double real (const rational< NUM > &num)
 Convertidor a punto flotante.
template<class NUM >
long integer (const rational< NUM > &num)
 Convertidor a punto fijo.
template<class NUM >
bool check_ok (const rational< NUM > &r)
 Verifica la invariante de la clase rational.
template<class NUM >
bool check_ok_no_Rep (const rational< NUM > &r)
 Verifica la invariante de la clase rational.
template<class NUM >
ostream & operator<< (ostream &COUT, const rational< NUM > &r)
 Graba el valor de "r" en el flujo "COUT".
template<class NUM >
istream & operator>> (istream &CIN, rational< NUM > &r)
 Lee del flujo de texto "CIN" el valor de "r".
template<class NUM >
rational< NUM > operator+ (const rational< NUM > &x, const rational< NUM > &y)
 "x+y".
template<class NUM >
rational< NUM > operator- (const rational< NUM > &x, const rational< NUM > &y)
 "x-y".
template<class NUM >
rational< NUM > operator* (const rational< NUM > &x, const rational< NUM > &y)
 "x*y".
template<class NUM >
rational< NUM > operator/ (const rational< NUM > &x, const rational< NUM > &y)
 "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--.

Detailed Description

Declara el tipo "rational".

Author:
Adolfo Di Mare adolf.nosp@m.o@di.nosp@m.-mare.nosp@m..com
Date:
2005

Definition in file rational.h.


Define Documentation

#define rational_h

Evita la inclusión múltiple.

Definition at line 19 of file rational.h.


Function Documentation

template<class NUM >
NUM mcd ( NUM  x,
NUM  y 
)

Calcula el Máximo Común Divisor de los números "x" y "y".

  • mcd(x,y) >= 1 siempre.
  • MCD <==> GCD: Greatest Common Divisor .
Precondition:
(y != 0)
Remarks:
Se usa el algoritmo de Euclides para hacer el cálculo.
Ejemplo:
    2*3*5 == mcd( 2*2*2*2 * 3*3 * 5*5, 2*3*5 )
       30 == mcd( -3600, -30 )
    {{  // test::mcd()
        assertTrue( 1 == mcd(1,2) );
        assertTrue( 2*3*5 == mcd( 2*2*2*2 * 3*3 * 5*5, 2*3*5 ) );
        assertTrue( 30 == mcd( -3600, -30 ) );
    }}

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

Definition at line 449 of file rational.h.

template<class INT >
INT gcd ( const INT &  x,
const INT &  y 
) [inline]

Sinónimo de mcd(x,y).

Definition at line 112 of file rational.h.

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

¿ 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 NUM >
bool operator< ( const rational< NUM > &  x,
const rational< NUM > &  y 
) [inline]

¿ x < y ?

Definition at line 266 of file rational.h.

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

¿ x > y ?

Definition at line 289 of file rational.h.

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

¿ x != y ?

Definition at line 295 of file rational.h.

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

¿ x <= y ?

Definition at line 301 of file rational.h.

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

¿ x >= y ?

Definition at line 307 of file rational.h.

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

Convertidor a punto flotante.

Definition at line 313 of file rational.h.

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

Convertidor a punto fijo.

Definition at line 319 of file rational.h.

template<class NUM >
bool check_ok ( const rational< NUM > &  r)

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.

template<class NUM >
bool check_ok_no_Rep ( const rational< NUM > &  r)

Verifica la invariante de la clase rational.

Remarks:
Esta implementación nos se le mete al Rep (casi siempre no es posible implementar una función como ésta).
Libera al programador de implementar el método 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 401 of file rational.h.

template<class NUM >
ostream& operator<< ( ostream &  COUT,
const rational< NUM > &  r 
)

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 NUM >
istream& operator>> ( istream &  CIN,
rational< NUM > &  r 
)

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 NUM >
rational<NUM> operator+ ( const rational< NUM > &  x,
const rational< NUM > &  y 
)

"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 NUM >
rational<NUM> operator- ( const rational< NUM > &  x,
const rational< NUM > &  y 
)

"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 NUM >
rational<NUM> operator* ( const rational< NUM > &  x,
const rational< NUM > &  y 
)

"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 NUM >
rational<NUM> operator/ ( const rational< NUM > &  x,
const rational< NUM > &  y 
)

"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 NUM >
rational<NUM>& operator++ ( rational< NUM > &  r) [inline]

++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 NUM >
rational<NUM> operator++ ( rational< NUM > &  r,
int   
) [inline]

r++.

Definition at line 796 of file rational.h.

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

--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 NUM >
rational<NUM> operator-- ( rational< NUM > &  r,
int   
) [inline]

r--.

Definition at line 816 of file rational.h.