From 0e5e01b4d0d5f3f872d97c95bd57fd057e4fd5a1 Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Thu, 9 May 2013 15:44:34 -0600 Subject: Added Number::toRadix & Number::set( int32_t ). Both very handy. I'll add other numeric setters later, it was very easy. --- src/number.cpp | 30 ++++++++++++++++++++++++++++++ src/number.h | 2 ++ src/options.cpp | 6 ++---- 3 files changed, 34 insertions(+), 4 deletions(-) diff --git a/src/number.cpp b/src/number.cpp index eb4d01d..cab7f46 100644 --- a/src/number.cpp +++ b/src/number.cpp @@ -358,6 +358,17 @@ void Number::set( const Number &sNum ) iRadix = sNum.iRadix; } +void Number::set( int32_t iNum ) +{ + aFrac.zero(); + aInt.clear(); + while( iNum > 0 ) + { + aInt.append( iNum%iRadix ); + iNum /= iRadix; + } +} + void Number::divide( const Number &rhs, Number &q, Number &r ) const { if( rhs.iScale > 0 ) @@ -598,6 +609,25 @@ int32_t Number::toInt32() const return ret; } +Number Number::toRadix( int iNewRadix, int iNewScale ) const +{ + if( iNewScale < 0 ) + iNewScale = iScale; + Number ret( iNewScale, iNewRadix ); + + Number me( 0, iRadix ); + me = *this; + Number n( 0, iRadix ); + n.set( iNewRadix ); + while( !me.isZero() ) + { + ret.aInt.append( (me%n).toInt32() ); + me = me / n; + } + + return ret; +} + Number Number::add( const Number &rhs, bool bSub ) const { Number ret( Bu::buMax(iScale,rhs.iScale), iRadix ); diff --git a/src/number.h b/src/number.h index 8077fe0..2abf0e1 100644 --- a/src/number.h +++ b/src/number.h @@ -31,6 +31,7 @@ public: void set( const Bu::String &sNum ); void set( const Number &sNum ); + void set( int32_t iNum ); void divide( const Number &rhs, Number &q, Number &r ) const; bool isZero() const; @@ -48,6 +49,7 @@ public: void setScale( int iNewScale ); int32_t toInt32() const; + Number toRadix( int iNewRadix, int iNewScale=-1 ) const; private: Number add( const Number &rhs, bool bSub ) const; diff --git a/src/options.cpp b/src/options.cpp index d9cd8f1..59dd2b4 100644 --- a/src/options.cpp +++ b/src/options.cpp @@ -19,7 +19,8 @@ Options::Options( int argc, char *argv[] ) : addOption( Bu::slot(this, &Options::textPrimes), "text-primes", "Generate primes in base 36 that only have digits > 9 in them."); addOption( Bu::slot(this, &Options::isPrime), 'p', "is-prime", - "Test if the given number is prime. Set radix first."); + "Tests every parameter after to see if it is prime then prints out " + "the ones that are prime. Set radix first."); addHelpOption('h', "help", "This help"); parse( argc, argv ); @@ -93,12 +94,9 @@ int Options::isPrime( Bu::StringArray aArgs ) one = "1"; fact = "2"; - Bu::println("Radix: %1").arg( iRadix ); - for( Bu::StringArray::iterator i = aArgs.begin()+1; i; i++ ) { tst = *i; - Bu::println("%1").arg( tst ); max = tst / fact; bool bPrime = true; for( j = "2"; j < max; j = j + one ) -- cgit v1.2.3