diff options
Diffstat (limited to 'src/options.cpp')
| -rw-r--r-- | src/options.cpp | 100 | 
1 files changed, 100 insertions, 0 deletions
diff --git a/src/options.cpp b/src/options.cpp index 0633c26..c127022 100644 --- a/src/options.cpp +++ b/src/options.cpp  | |||
| @@ -40,6 +40,8 @@ Options::Options( int argc, char *argv[] ) : | |||
| 40 | "your shell)."); | 40 | "your shell)."); | 
| 41 | addOption( Bu::slot(this, &Options::sum), "sum", | 41 | addOption( Bu::slot(this, &Options::sum), "sum", | 
| 42 | "Read numbers from standard input and sum them, output the result."); | 42 | "Read numbers from standard input and sum them, output the result."); | 
| 43 | addOption( Bu::slot(this, &Options::grind), 'g', "grind", | ||
| 44 | "Search for magic numbers. Secret magic numbers."); | ||
| 43 | addOption( Bu::slot(this, &Options::version), 'v', "version", | 45 | addOption( Bu::slot(this, &Options::version), 'v', "version", | 
| 44 | "Show the version string ('" CLIC_VERSION_STR "')"); | 46 | "Show the version string ('" CLIC_VERSION_STR "')"); | 
| 45 | addHelpOption('h', "help", "This help"); | 47 | addHelpOption('h', "help", "This help"); | 
| @@ -225,3 +227,101 @@ int Options::version( Bu::StringArray aArgs ) | |||
| 225 | return 0; | 227 | return 0; | 
| 226 | } | 228 | } | 
| 227 | 229 | ||
| 230 | bool onlyNines( const Bu::String &s ) | ||
| 231 | { | ||
| 232 | for( Bu::String::const_iterator i = s.begin(); i; i++ ) | ||
| 233 | { | ||
| 234 | if( *i != '9' ) | ||
| 235 | return false; | ||
| 236 | } | ||
| 237 | return true; | ||
| 238 | } | ||
| 239 | |||
| 240 | bool onlyOnes( const Bu::String &s ) | ||
| 241 | { | ||
| 242 | for( Bu::String::const_iterator i = s.begin(); i; i++ ) | ||
| 243 | { | ||
| 244 | if( *i != '1' ) | ||
| 245 | return false; | ||
| 246 | } | ||
| 247 | return true; | ||
| 248 | } | ||
| 249 | |||
| 250 | int Options::grind( Bu::StringArray aArgs ) | ||
| 251 | { | ||
| 252 | Number test("1"); | ||
| 253 | Number bit("1"); | ||
| 254 | Number mult("2"); | ||
| 255 | int64_t iBits = 1; | ||
| 256 | int iLastSize = 0; | ||
| 257 | |||
| 258 | if( aArgs.getSize() > 1 ) | ||
| 259 | { | ||
| 260 | Bu::print("Loading number..."); Bu::sioRaw.flush(); | ||
| 261 | Bu::String num = Bu::File(aArgs[1], Bu::File::Read).readAll(); | ||
| 262 | test = num; | ||
| 263 | Bu::println("done."); | ||
| 264 | Bu::print("Computing bit..."); Bu::sioRaw.flush(); | ||
| 265 | bit = test; | ||
| 266 | bit = bit+Number("1"); | ||
| 267 | bit = bit/mult; | ||
| 268 | Bu::println("done."); | ||
| 269 | if( aArgs.getSize() > 2 ) | ||
| 270 | { | ||
| 271 | Bu::print("Trusting a human for where we are..."); Bu::sioRaw.flush(); | ||
| 272 | iBits = strtoll( aArgs[2].getStr(), NULL, 10 ); | ||
| 273 | } | ||
| 274 | else | ||
| 275 | { | ||
| 276 | Bu::print("Figuring out where we are..."); Bu::sioRaw.flush(); | ||
| 277 | Bu::String sBin = bit.toRadix(2).toString(); | ||
| 278 | iBits = sBin.getSize(); | ||
| 279 | } | ||
| 280 | // Bu::println("Done. Checking."); | ||
| 281 | // Bu::println("test = %1, bit = %2").arg( test ).arg( bit ); | ||
| 282 | // Bu::String sTest = test.toRadix(2).toString(); | ||
| 283 | // Bu::File("check-bit.txt", Bu::File::WriteNew ).write( sBin ); | ||
| 284 | // Bu::File("check-test.txt", Bu::File::WriteNew ).write( sTest ); | ||
| 285 | iLastSize = num.getSize(); | ||
| 286 | // Bu::println("File written. %1 bits").arg( sBin.getSize() ); | ||
| 287 | Bu::println("done."); | ||
| 288 | Bu::println("Continuing calculation..."); | ||
| 289 | } | ||
| 290 | |||
| 291 | for(;;) | ||
| 292 | { | ||
| 293 | bit = bit*mult; | ||
| 294 | test = test + bit; | ||
| 295 | Bu::String r = test.toString(); | ||
| 296 | iBits++; | ||
| 297 | |||
| 298 | if( onlyNines(r) ) | ||
| 299 | { | ||
| 300 | Bu::println("%1 digits").arg( r.getSize() ); | ||
| 301 | Bu::File f("answer-dec.txt", Bu::File::WriteNew ); | ||
| 302 | f.write( r ); | ||
| 303 | Bu::println("Success."); | ||
| 304 | // Bu::println("%1 == %2").arg( test.toRadix(2) ).arg( r ); | ||
| 305 | return 0; | ||
| 306 | } | ||
| 307 | if( r.getSize() != iLastSize && r.getSize()%1000 == 0 ) | ||
| 308 | { | ||
| 309 | /* | ||
| 310 | Bu::String rs = test.toRadix(2).toString(); | ||
| 311 | int iOBits = rs.getSize(); | ||
| 312 | if( iOBits != iBits ) | ||
| 313 | Bu::println("Bit mismatch: %1 vs %2").arg( iOBits ).arg( iBits ); | ||
| 314 | if( !onlyOnes( rs ) ) | ||
| 315 | Bu::println("Not only ones!"); | ||
| 316 | */ | ||
| 317 | iLastSize = r.getSize(); | ||
| 318 | Bu::println("%1 digits").arg( r.getSize() ); | ||
| 319 | Bu::File f(Bu::String("interum-%1-%2.txt").arg( r.getSize() ).arg( iBits ), Bu::File::WriteNew ); | ||
| 320 | f.write( r ); | ||
| 321 | } | ||
| 322 | } | ||
| 323 | Bu::println("Failure."); | ||
| 324 | exit( 0 ); | ||
| 325 | return 0; | ||
| 326 | } | ||
| 327 | |||
