From 3c2ddfa05397a829512694ddf387b353b91a144e Mon Sep 17 00:00:00 2001 From: Leandro Lucarella Date: Tue, 4 Oct 2005 04:09:18 +0000 Subject: [PATCH] Arregla fines de linea (saca \r) y otros detalles menores. --- src/number.h | 174 +++++++++++++++++++++++------------------------ tests/number.cpp | 35 +++++----- 2 files changed, 105 insertions(+), 104 deletions(-) diff --git a/src/number.h b/src/number.h index d4593bc..1e857ac 100644 --- a/src/number.h +++ b/src/number.h @@ -7,13 +7,13 @@ #include #include #include -#include - -#ifdef _WIN32 +#include + +#ifdef _WIN32 // VC++ no tiene la stdint.h, se agrega a mano -#include "stdint.h" -#else -#include +#include "stdint.h" +#else +#include #endif enum sign_type { positive, negative }; @@ -61,7 +61,7 @@ struct number number(native_type n, sign_type sign = positive): chunk(1, n), sign(sign) {} - // TODO constructor a partir de string. + number(const std::string& str); // Operadores number& operator++ () @@ -72,8 +72,8 @@ struct number number& operator+= (const number& n); number& operator*= (const number& n); - number& operator<<= (const size_type n); - number& operator-= (const number& n); + number& operator<<= (const size_type n); + number& operator-= (const number& n); bool operator< (const number& n); // Devuelve referencia a 'átomo' i del chunk (no debería ser necesario @@ -126,33 +126,33 @@ struct number chunk.push_back(1); } -}; - -template < typename N, typename E > -number< N, E >::number(const std::string& str) -{ - const N MAX_N = (~( (N)0 ) ); - E increment = 0; - E acum = 0; - - unsigned length = origen.length(); - unsigned number_offset = 0; - - while (number_offset MAX_N) - { - chunk.push_back(acum); - } - - } - - +}; + +template < typename N, typename E > +number< N, E >::number(const std::string& origen) +{ + const N MAX_N = (~( (N)0 ) ); + E increment = 0; + E acum = 0; + + unsigned length = origen.length(); + unsigned number_offset = 0; + + while (number_offset MAX_N) + { + chunk.push_back(acum); + } + + } + + } template < typename N, typename E > @@ -228,54 +228,54 @@ number< N, E > operator+ (const number< N, E >& n1, const number< N, E >& n2) number< N, E > tmp = n1; tmp += n2; return tmp; -} - -template < typename N, typename E > -number< N, E >& number< N, E >::operator-= (const number< N, E >& n) -{ - //TODO IMPLEMENTAR +} + +template < typename N, typename E > +number< N, E >& number< N, E >::operator-= (const number< N, E >& n) +{ + //TODO IMPLEMENTAR return *this; -} - -template < typename N, typename E > -number< N, E > operator- (const number< N, E >& n1, const number< N, E >& n2) -{ - number< N, E > tmp = n1; - tmp -= n2; - return tmp; -} - - -template < typename N, typename E > -bool number< N, E >::operator< (const number< N, E >& n) -{ - number< N, E > n1 = *this; - number< N, E > n2 = n; - - // igualo los largos - n1.normalize_length(n2); - n2.normalize_length(n1); - - // obtengo el largo - size_type length = n1.chunk.size(); - size_type i = length - 1; - - // me voy fijando desde "la cifra" más significativa si alguno es menor que el otro - // sigo iterando si son iguales hasta recorrer todo el número hasta la parte menos significativa - while (i > 0) - { - if (n1[i]n2[i]) - return false; - - i--; - } - - // si llegué hasta acá es porque son iguales, por lo tanto no es menor estricto - return false; - -} +} + +template < typename N, typename E > +number< N, E > operator- (const number< N, E >& n1, const number< N, E >& n2) +{ + number< N, E > tmp = n1; + tmp -= n2; + return tmp; +} + + +template < typename N, typename E > +bool number< N, E >::operator< (const number< N, E >& n) +{ + number< N, E > n1 = *this; + number< N, E > n2 = n; + + // igualo los largos + n1.normalize_length(n2); + n2.normalize_length(n1); + + // obtengo el largo + size_type length = n1.chunk.size(); + size_type i = length - 1; + + // me voy fijando desde "la cifra" más significativa si alguno es menor que el otro + // sigo iterando si son iguales hasta recorrer todo el número hasta la parte menos significativa + while (i > 0) + { + if (n1[i]n2[i]) + return false; + + i--; + } + + // si llegué hasta acá es porque son iguales, por lo tanto no es menor estricto + return false; + +} // efectúa un shifteo a izquierda del chunk, agregando 0s en los casilleros menos significativos template < typename N, typename E > @@ -332,10 +332,10 @@ number< N, E >& number< N, E >::normalize_length(const number< N, E >& n) while (chunk.size() < n.chunk.size()) { chunk.push_back(0); - } - - return *this; - + } + + return *this; + } template < typename N, typename E > diff --git a/tests/number.cpp b/tests/number.cpp index c0d7bd1..490767e 100644 --- a/tests/number.cpp +++ b/tests/number.cpp @@ -21,25 +21,26 @@ int main() nu << 5; std::cout << "nu = " << nu << "\n\n"; -#endif - - number<> n1 = 0xffff00f0; - number<> n2 = 0xffff00f0; - - n1 += 1; - - n1 <<= 4; - n2 <<= 4; - - if (n1 < n2) - std::cout << "n1 es menor que n2\n"; - else if (n2 < n1) - std::cout << "n1 es mayor que n2\n"; - else - std::cout << "n1 es igual que n2\n"; - +#endif + + number<> n1 = 0xffff00f0; + number<> n2 = 0xffff00f0; + + n1 += 1; + + n1 <<= 4; + n2 <<= 4; + + if (n1 < n2) + std::cout << "n1 es menor que n2\n"; + else if (n2 < n1) + std::cout << "n1 es mayor que n2\n"; + else + std::cout << "n1 es igual que n2\n"; + number<> n3 = n1 - n2; return 0; } + -- 2.43.0