[B]asic module for [unit] program testing:
|
La clase rational
implementa las operaciones aritméticas principales para números rationales.
More...
#include <rational.h>
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). | |
rational & | operator= (const rational &) |
Copia desde "o" . | |
rational & | operator= (INT) |
Asignación desde un "INT" . | |
rational & | swap (rational &) |
Intercambia los valores de "*this" y "o" . | |
rational & | operator+= (const rational &) |
Le suma a "*this" el valor de "otro" . | |
rational & | operator-= (const rational &) |
Le resta a "*this" el valor de "otro" . | |
rational & | operator*= (const rational &) |
Multiplica "*this" por "num" . | |
rational & | operator/= (const rational &) |
Divide "*this" por el valor de "num" . | |
rational | operator- () const |
"-x" . | |
rational & | fromString (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 . |
La clase rational
implementa las operaciones aritméticas principales para números rationales.
[1/3] == [2/6] == ... [9/27] == ...
[1/3] * [2/6] / [3/9] - [9/27]
Definition at line 33 of file rational.h.
Constructor de vector.
Definition at line 42 of file rational.h.
Constructor a partir de un valor entero.
Definition at line 43 of file rational.h.
Constructor a partir de un valor quedbrado.
Definition at line 44 of file rational.h.
Constructor de copia.
Definition at line 47 of file rational.h.
Destructor.
Definition at line 48 of file rational.h.
Simplifica el numerador y el denomidador.
(m_num==0) ==> (m_den==1)
.m_num
y m_den
sean números primos relativos ie, mcd(m_num,m_den) == 1
.m_den
sea un número positivo.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) ); }}
Definition at line 479 of file rational.h.
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"
.
d != 0
.{{ // test::set() rational<INT> r; assertTrue( r == rational<INT>( 0 ) ); r.set(-3, -4); assertTrue( r == rational<INT>( 3*11 , 4*11 ) ); }}
Definition at line 123 of file rational.h.
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 ); }}
Definition at line 57 of file rational.h.
Acceso al denomirador (siempre >0).
Definition at line 59 of file rational.h.
rational< INT > & rational< INT >::operator= | ( | const rational< INT > & | o | ) | [inline] |
Copia desde "o"
.
"*this"
se pierde.1
) {{ // 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) ); }}
Definition at line 152 of file rational.h.
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) ); }}
Definition at line 195 of file rational.h.
Intercambia los valores de "*this"
y "o"
.
1
) {{ // 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) ); }}
Definition at line 172 of file rational.h.
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 ); }}
Definition at line 502 of file rational.h.
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 ); }}
Definition at line 516 of file rational.h.
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 ); }}
Definition at line 208 of file rational.h.
rational< INT > & rational< INT >::operator/= | ( | const rational< INT > & | num | ) | [inline] |
Divide "*this"
por el valor de "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 ); }}
Definition at line 224 of file rational.h.
"-x"
.
"-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 ); }}
Definition at line 241 of file rational.h.
rational< NUM > & rational< NUM >::fromString | ( | const char * | nStr | ) |
Establece el varlor de "*this"
a partir de la hilera "nStr"
.
"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) ); }}
Definition at line 648 of file rational.h.
friend class test_rational [friend] |
Datos de prueba para la clase.
Definition at line 104 of file rational.h.
rational<NUM> operator+ | ( | const rational< NUM > & | x, |
const rational< NUM > & | y | ||
) | [friend] |
"x+y"
.
"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 ); }}
Definition at line 712 of file rational.h.
rational<NUM> operator- | ( | const rational< NUM > & | x, |
const rational< NUM > & | y | ||
) | [friend] |
"x-y"
.
"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 ); }}
Definition at line 729 of file rational.h.
rational<NUM> operator* | ( | const rational< NUM > & | x, |
const rational< NUM > & | y | ||
) | [friend] |
"x*y"
.
"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 ); }}
Definition at line 746 of file rational.h.
rational<NUM> operator/ | ( | const rational< NUM > & | x, |
const rational< NUM > & | y | ||
) | [friend] |
"x/y"
.
"x/y"
. 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 ); }}
Definition at line 764 of file rational.h.
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 ); }}
Definition at line 254 of file rational.h.
bool operator< | ( | const rational< NUM > & | x, |
const rational< NUM > & | y | ||
) | [friend] |
¿ x < y ?
Definition at line 266 of file rational.h.
bool operator!= | ( | const rational< NUM > & | x, |
const rational< NUM > & | y | ||
) | [friend] |
¿ x != y ?
Definition at line 295 of file rational.h.
bool operator<= | ( | const rational< NUM > & | x, |
const rational< NUM > & | y | ||
) | [friend] |
¿ x <= y ?
Definition at line 301 of file rational.h.
bool operator>= | ( | const rational< NUM > & | x, |
const rational< NUM > & | y | ||
) | [friend] |
¿ x >= y ?
Definition at line 307 of file rational.h.
bool operator> | ( | const rational< NUM > & | x, |
const rational< NUM > & | y | ||
) | [friend] |
¿ x > y ?
Definition at line 289 of file rational.h.
++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) ); }}
Definition at line 789 of file rational.h.
rational<NUM> operator++ | ( | rational< NUM > & | r, |
int | |||
) | [friend] |
r++
.
Definition at line 796 of file rational.h.
--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) ); }}
Definition at line 809 of file rational.h.
rational<NUM> operator-- | ( | rational< NUM > & | r, |
int | |||
) | [friend] |
r--
.
Definition at line 816 of file rational.h.
ostream& operator<< | ( | ostream & | COUT, |
const rational< NUM > & | r | ||
) | [friend] |
Graba el valor de "r"
en el flujo "COUT"
.
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]" ); }}
Definition at line 543 of file rational.h.
istream& operator>> | ( | istream & | CIN, |
rational< NUM > & | r | ||
) | [friend] |
Lee del flujo de texto "CIN"
el valor de "r"
.
"]"
. [ -+-+-+-+- 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) ); }}
Definition at line 566 of file rational.h.
Convertidor a punto flotante.
Definition at line 313 of file rational.h.
Convertidor a punto fijo.
Definition at line 319 of file rational.h.
Verifica la invariante de la clase rational
.
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 ) ); }}
Definition at line 356 of file rational.h.
Numerador.
Definition at line 35 of file rational.h.
Denominador.
Definition at line 36 of file rational.h.