[B]asic module for [unit] program testing:
|
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-- . |
Declara el tipo "rational"
.
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 in file rational.h.
#define rational_h |
Evita la inclusión múltiple.
Definition at line 19 of file rational.h.
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. (y != 0)
{{ // 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 ) ); }}
Definition at line 449 of file rational.h.
INT gcd | ( | const INT & | x, |
const INT & | y | ||
) | [inline] |
Sinónimo de mcd(x,y)
.
Definition at line 112 of file rational.h.
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 ); }}
Definition at line 254 of file rational.h.
bool operator< | ( | const rational< NUM > & | x, |
const rational< NUM > & | y | ||
) | [inline] |
¿ x < y ?
Definition at line 266 of file rational.h.
bool operator> | ( | const rational< NUM > & | x, |
const rational< NUM > & | y | ||
) | [inline] |
¿ x > y ?
Definition at line 289 of file rational.h.
bool operator!= | ( | const rational< NUM > & | x, |
const rational< NUM > & | y | ||
) | [inline] |
¿ x != y ?
Definition at line 295 of file rational.h.
bool operator<= | ( | const rational< NUM > & | x, |
const rational< NUM > & | y | ||
) | [inline] |
¿ x <= y ?
Definition at line 301 of file rational.h.
bool operator>= | ( | const rational< NUM > & | x, |
const rational< NUM > & | y | ||
) | [inline] |
¿ x >= y ?
Definition at line 307 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
.
+---+ | 3 | <== m_num == numerador del número racional +---+ |134| <== m_den == denominador del número racional +---+
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.
bool check_ok_no_Rep | ( | const rational< NUM > & | r | ) |
Verifica la invariante de la clase rational
.
Ok()
Definition at line 401 of file rational.h.
ostream& operator<< | ( | ostream & | COUT, |
const rational< NUM > & | r | ||
) |
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 | ||
) |
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.
rational<NUM> operator+ | ( | const rational< NUM > & | x, |
const rational< NUM > & | y | ||
) |
"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 | ||
) |
"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 | ||
) |
"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 | ||
) |
"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.
++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.
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.
r--
.
Definition at line 816 of file rational.h.