From: Ezequiel Date: Tue, 4 Oct 2005 03:47:33 +0000 (+0000) Subject: operator < X-Git-Tag: Entrega_1~44 X-Git-Url: https://git.llucax.com/z.facultad/75.29/dale.git/commitdiff_plain/5e6b85a00fe62bb0a04c91d4312c34af74ebafd8?ds=inline operator < --- diff --git a/src/number.h b/src/number.h index 99eec8c..d4593bc 100644 --- a/src/number.h +++ b/src/number.h @@ -7,8 +7,14 @@ #include #include #include -#include -#include +#include + +#ifdef _WIN32 +// VC++ no tiene la stdint.h, se agrega a mano +#include "stdint.h" +#else +#include +#endif enum sign_type { positive, negative }; @@ -66,7 +72,9 @@ struct number number& operator+= (const number& n); number& operator*= (const number& n); - number& operator<<= (const size_type 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 // si la multiplicación es un método de este objeto). @@ -118,7 +126,34 @@ 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< N, E >::operator+= (const number< N, E >& n) @@ -193,7 +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 + 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; + +} // efectúa un shifteo a izquierda del chunk, agregando 0s en los casilleros menos significativos template < typename N, typename E > @@ -250,14 +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); - } - - // si no tiene cantidad par de números le agrego un atomic_type 0 a la - // izquierda para no tener que contemplar splits de chunks impares - if ((chunk.size() % 2) != 0) - { - chunk.push_back(0); - } + } + + return *this; + } template < typename N, typename E > diff --git a/tests/number.cpp b/tests/number.cpp index 44e56ab..c0d7bd1 100644 --- a/tests/number.cpp +++ b/tests/number.cpp @@ -3,7 +3,7 @@ int main() { -#if 1 +#if 0 unsigned buf[5] = { 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff, 0 }; //unsigned buf[3] = { 0xffffffff, 0x1, 0 }; number<> nu(buf, 4); @@ -21,7 +21,24 @@ int main() nu << 5; std::cout << "nu = " << nu << "\n\n"; -#endif +#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;