diff options
author | Mike Buland <mike@xagasoft.com> | 2013-05-09 15:24:11 -0600 |
---|---|---|
committer | Mike Buland <mike@xagasoft.com> | 2013-05-09 15:24:11 -0600 |
commit | ce68e816bd82612c14f3492e8bc969da9bfab06c (patch) | |
tree | be50fa63ce720ecde2748df3df83f38a284c3ff6 /src/options.cpp | |
parent | 722e0ef0ea2624c1cd9bd5ca69833e37dc09f97f (diff) | |
download | clic-ce68e816bd82612c14f3492e8bc969da9bfab06c.tar.gz clic-ce68e816bd82612c14f3492e8bc969da9bfab06c.tar.bz2 clic-ce68e816bd82612c14f3492e8bc969da9bfab06c.tar.xz clic-ce68e816bd82612c14f3492e8bc969da9bfab06c.zip |
Added better filtering in Number::set, and cli options.
The command line options let you set the initial radix/scale, and
there's a function te test if any number is prime, that's fun.
Diffstat (limited to 'src/options.cpp')
-rw-r--r-- | src/options.cpp | 47 |
1 files changed, 46 insertions, 1 deletions
diff --git a/src/options.cpp b/src/options.cpp index 7f66028..d9cd8f1 100644 --- a/src/options.cpp +++ b/src/options.cpp | |||
@@ -6,12 +6,20 @@ | |||
6 | #include <bu/sio.h> | 6 | #include <bu/sio.h> |
7 | #include <stdlib.h> | 7 | #include <stdlib.h> |
8 | 8 | ||
9 | Options::Options( int argc, char *argv[] ) | 9 | Options::Options( int argc, char *argv[] ) : |
10 | iScale( 0 ), | ||
11 | iRadix( 10 ) | ||
10 | { | 12 | { |
13 | addOption( iRadix, 'r', "radix", | ||
14 | Bu::String("Set the radix (default: %1)").arg( iRadix ) ); | ||
15 | addOption( iScale, 's', "scale", | ||
16 | Bu::String("Set the scale (default: %1)").arg( iScale ) ); | ||
11 | addOption( Bu::slot(this, &Options::selfTest), "self-test", | 17 | addOption( Bu::slot(this, &Options::selfTest), "self-test", |
12 | "Run a series of tests to ensure everything is working correctly."); | 18 | "Run a series of tests to ensure everything is working correctly."); |
13 | addOption( Bu::slot(this, &Options::textPrimes), "text-primes", | 19 | addOption( Bu::slot(this, &Options::textPrimes), "text-primes", |
14 | "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", | ||
22 | "Test if the given number is prime. Set radix first."); | ||
15 | addHelpOption('h', "help", "This help"); | 23 | addHelpOption('h', "help", "This help"); |
16 | 24 | ||
17 | parse( argc, argv ); | 25 | parse( argc, argv ); |
@@ -75,3 +83,40 @@ int Options::textPrimes( Bu::StringArray ) | |||
75 | return 0; | 83 | return 0; |
76 | } | 84 | } |
77 | 85 | ||
86 | int Options::isPrime( Bu::StringArray aArgs ) | ||
87 | { | ||
88 | Number tst( 0, iRadix ); | ||
89 | Number max( 0, iRadix ); | ||
90 | Number j( 0, iRadix ); | ||
91 | Number one( 0, iRadix ); | ||
92 | Number fact( 0, iRadix ); | ||
93 | one = "1"; | ||
94 | fact = "2"; | ||
95 | |||
96 | Bu::println("Radix: %1").arg( iRadix ); | ||
97 | |||
98 | for( Bu::StringArray::iterator i = aArgs.begin()+1; i; i++ ) | ||
99 | { | ||
100 | tst = *i; | ||
101 | Bu::println("%1").arg( tst ); | ||
102 | max = tst / fact; | ||
103 | bool bPrime = true; | ||
104 | for( j = "2"; j < max; j = j + one ) | ||
105 | { | ||
106 | if( (tst%j).isZero() ) | ||
107 | { | ||
108 | bPrime = false; | ||
109 | break; | ||
110 | } | ||
111 | } | ||
112 | |||
113 | if( bPrime ) | ||
114 | { | ||
115 | Bu::println("%1").arg( tst ); | ||
116 | } | ||
117 | } | ||
118 | |||
119 | exit( 0 ); | ||
120 | return aArgs.getSize(); | ||
121 | } | ||
122 | |||