#include "options.h" #include "unitnumber.h" #include "unitparser.h" #include "number.h" #include #include 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", "Tests every parameter after to see if it is prime then prints out " "the ones that are prime. Set radix first."); addOption( Bu::slot(this, &Options::convert), 'c', "convert", "Convert the provided number to the given radix in the format " "number:to-radix or from-radix:number:to-radix. The radix should " "be provided in base-10"); addHelpOption('h', "help", "This help"); parse( argc, argv ); } Options::~Options() { } int Options::selfTest( Bu::StringArray ) { UnitNumber().run(); UnitParser().run(); exit( 0 ); return 0; } bool hasDigits( const Bu::String &s ) { for( Bu::String::const_iterator i = s.begin(); i; i++ ) { if( *i >= '0' && *i <= '9' ) return true; } return false; } int Options::textPrimes( Bu::StringArray aArgs ) { Number tst( 0, 36 ); Number max( 0, 36 ); Number j( 0, 36 ); Number one( 0, 36 ); Number fact( 0, 36 ); one = "1"; fact = "2"; if( aArgs.getSize() >= 2 ) tst = aArgs[1]; else tst = "1"; for(;; tst = tst + one ) { if( hasDigits( tst.toString() ) ) continue; 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 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"; for( Bu::StringArray::iterator i = aArgs.begin()+1; i; i++ ) { tst = *i; 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(); } int Options::convert( Bu::StringArray aArgs ) { for( Bu::StringArray::iterator i = aArgs.begin()+1; i; i++ ) { Bu::StringList lBits = (*i).split(':'); if( lBits.getSize() < 2 || lBits.getSize() > 3 ) throw Bu::ExceptionBase("Invalid format"); int iFromRadix = iRadix; int iToRadix; Number n; if( lBits.getSize() == 2 ) { iToRadix = strtol( lBits.last().getStr(), 0, 10 ); n = Number( lBits.first(), 0, iFromRadix ); } else if( lBits.getSize() == 3 ) { iFromRadix = strtol( lBits.first().getStr(), 0, 10 ); iToRadix = strtol( lBits.last().getStr(), 0, 10 ); n = Number( *(lBits.begin()+1), 0, iFromRadix ); } Bu::println("%1").arg( n.toRadix( iToRadix ).toString() ); } exit( 0 ); return 0; }