diff options
author | Mike Buland <mike@xagasoft.com> | 2013-05-09 15:44:34 -0600 |
---|---|---|
committer | Mike Buland <mike@xagasoft.com> | 2013-05-09 15:44:34 -0600 |
commit | 0e5e01b4d0d5f3f872d97c95bd57fd057e4fd5a1 (patch) | |
tree | 1e6a657c57ceeda4d699f1f0b55ad033e3cacf7b /src | |
parent | ce68e816bd82612c14f3492e8bc969da9bfab06c (diff) | |
download | clic-0e5e01b4d0d5f3f872d97c95bd57fd057e4fd5a1.tar.gz clic-0e5e01b4d0d5f3f872d97c95bd57fd057e4fd5a1.tar.bz2 clic-0e5e01b4d0d5f3f872d97c95bd57fd057e4fd5a1.tar.xz clic-0e5e01b4d0d5f3f872d97c95bd57fd057e4fd5a1.zip |
Added Number::toRadix & Number::set( int32_t ).
Both very handy. I'll add other numeric setters later, it was very
easy.
Diffstat (limited to 'src')
-rw-r--r-- | src/number.cpp | 30 | ||||
-rw-r--r-- | src/number.h | 2 | ||||
-rw-r--r-- | 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 ) | |||
358 | iRadix = sNum.iRadix; | 358 | iRadix = sNum.iRadix; |
359 | } | 359 | } |
360 | 360 | ||
361 | void Number::set( int32_t iNum ) | ||
362 | { | ||
363 | aFrac.zero(); | ||
364 | aInt.clear(); | ||
365 | while( iNum > 0 ) | ||
366 | { | ||
367 | aInt.append( iNum%iRadix ); | ||
368 | iNum /= iRadix; | ||
369 | } | ||
370 | } | ||
371 | |||
361 | void Number::divide( const Number &rhs, Number &q, Number &r ) const | 372 | void Number::divide( const Number &rhs, Number &q, Number &r ) const |
362 | { | 373 | { |
363 | if( rhs.iScale > 0 ) | 374 | if( rhs.iScale > 0 ) |
@@ -598,6 +609,25 @@ int32_t Number::toInt32() const | |||
598 | return ret; | 609 | return ret; |
599 | } | 610 | } |
600 | 611 | ||
612 | Number Number::toRadix( int iNewRadix, int iNewScale ) const | ||
613 | { | ||
614 | if( iNewScale < 0 ) | ||
615 | iNewScale = iScale; | ||
616 | Number ret( iNewScale, iNewRadix ); | ||
617 | |||
618 | Number me( 0, iRadix ); | ||
619 | me = *this; | ||
620 | Number n( 0, iRadix ); | ||
621 | n.set( iNewRadix ); | ||
622 | while( !me.isZero() ) | ||
623 | { | ||
624 | ret.aInt.append( (me%n).toInt32() ); | ||
625 | me = me / n; | ||
626 | } | ||
627 | |||
628 | return ret; | ||
629 | } | ||
630 | |||
601 | Number Number::add( const Number &rhs, bool bSub ) const | 631 | Number Number::add( const Number &rhs, bool bSub ) const |
602 | { | 632 | { |
603 | Number ret( Bu::buMax(iScale,rhs.iScale), iRadix ); | 633 | 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: | |||
31 | 31 | ||
32 | void set( const Bu::String &sNum ); | 32 | void set( const Bu::String &sNum ); |
33 | void set( const Number &sNum ); | 33 | void set( const Number &sNum ); |
34 | void set( int32_t iNum ); | ||
34 | 35 | ||
35 | void divide( const Number &rhs, Number &q, Number &r ) const; | 36 | void divide( const Number &rhs, Number &q, Number &r ) const; |
36 | bool isZero() const; | 37 | bool isZero() const; |
@@ -48,6 +49,7 @@ public: | |||
48 | void setScale( int iNewScale ); | 49 | void setScale( int iNewScale ); |
49 | 50 | ||
50 | int32_t toInt32() const; | 51 | int32_t toInt32() const; |
52 | Number toRadix( int iNewRadix, int iNewScale=-1 ) const; | ||
51 | 53 | ||
52 | private: | 54 | private: |
53 | Number add( const Number &rhs, bool bSub ) const; | 55 | 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[] ) : | |||
19 | addOption( Bu::slot(this, &Options::textPrimes), "text-primes", | 19 | addOption( Bu::slot(this, &Options::textPrimes), "text-primes", |
20 | "Generate primes in base 36 that only have digits > 9 in them."); | 20 | "Generate primes in base 36 that only have digits > 9 in them."); |
21 | addOption( Bu::slot(this, &Options::isPrime), 'p', "is-prime", | 21 | addOption( Bu::slot(this, &Options::isPrime), 'p', "is-prime", |
22 | "Test if the given number is prime. Set radix first."); | 22 | "Tests every parameter after to see if it is prime then prints out " |
23 | "the ones that are prime. Set radix first."); | ||
23 | addHelpOption('h', "help", "This help"); | 24 | addHelpOption('h', "help", "This help"); |
24 | 25 | ||
25 | parse( argc, argv ); | 26 | parse( argc, argv ); |
@@ -93,12 +94,9 @@ int Options::isPrime( Bu::StringArray aArgs ) | |||
93 | one = "1"; | 94 | one = "1"; |
94 | fact = "2"; | 95 | fact = "2"; |
95 | 96 | ||
96 | Bu::println("Radix: %1").arg( iRadix ); | ||
97 | |||
98 | for( Bu::StringArray::iterator i = aArgs.begin()+1; i; i++ ) | 97 | for( Bu::StringArray::iterator i = aArgs.begin()+1; i; i++ ) |
99 | { | 98 | { |
100 | tst = *i; | 99 | tst = *i; |
101 | Bu::println("%1").arg( tst ); | ||
102 | max = tst / fact; | 100 | max = tst / fact; |
103 | bool bPrime = true; | 101 | bool bPrime = true; |
104 | for( j = "2"; j < max; j = j + one ) | 102 | for( j = "2"; j < max; j = j + one ) |