From ce68e816bd82612c14f3492e8bc969da9bfab06c Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Thu, 9 May 2013 15:24:11 -0600 Subject: 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. --- src/options.cpp | 47 ++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 46 insertions(+), 1 deletion(-) (limited to 'src/options.cpp') 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 @@ #include #include -Options::Options( int argc, char *argv[] ) +Options::Options( int argc, char *argv[] ) : + iScale( 0 ), + iRadix( 10 ) { + addOption( iRadix, 'r', "radix", + Bu::String("Set the radix (default: %1)").arg( iRadix ) ); + addOption( iScale, 's', "scale", + Bu::String("Set the scale (default: %1)").arg( iScale ) ); addOption( Bu::slot(this, &Options::selfTest), "self-test", "Run a series of tests to ensure everything is working correctly."); 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."); addHelpOption('h', "help", "This help"); parse( argc, argv ); @@ -75,3 +83,40 @@ int Options::textPrimes( Bu::StringArray ) return 0; } +int Options::isPrime( Bu::StringArray aArgs ) +{ + Number tst( 0, iRadix ); + Number max( 0, iRadix ); + Number j( 0, iRadix ); + Number one( 0, iRadix ); + Number fact( 0, iRadix ); + 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 ) + { + if( (tst%j).isZero() ) + { + bPrime = false; + break; + } + } + + if( bPrime ) + { + Bu::println("%1").arg( tst ); + } + } + + exit( 0 ); + return aArgs.getSize(); +} + -- cgit v1.2.3