diff options
author | Mike Buland <mike@xagasoft.com> | 2015-01-07 09:53:14 -0700 |
---|---|---|
committer | Mike Buland <mike@xagasoft.com> | 2015-01-07 09:53:14 -0700 |
commit | e64cb84fe43660818f7d6eff2954147495b1ac8b (patch) | |
tree | 8e89978bc4a72677a8728d62eb850d81c9b92d46 /src/options.cpp | |
parent | 4d0ab515cc4142b22247e26f1b57b5cd0a6b31d5 (diff) | |
download | clic-e64cb84fe43660818f7d6eff2954147495b1ac8b.tar.gz clic-e64cb84fe43660818f7d6eff2954147495b1ac8b.tar.bz2 clic-e64cb84fe43660818f7d6eff2954147495b1ac8b.tar.xz clic-e64cb84fe43660818f7d6eff2954147495b1ac8b.zip |
Fixed parsing bug, added unit tests, debug cleanup.0.12
There was an issue with order of operations outside of parenthesies,
easily solved.
Diffstat (limited to '')
-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 d88df3b..26f0cb7 100644 --- a/src/options.cpp +++ b/src/options.cpp | |||
@@ -39,6 +39,8 @@ Options::Options( int argc, char *argv[] ) : | |||
39 | "your shell)."); | 39 | "your shell)."); |
40 | addOption( Bu::slot(this, &Options::sum), "sum", | 40 | addOption( Bu::slot(this, &Options::sum), "sum", |
41 | "Read numbers from standard input and sum them, output the result."); | 41 | "Read numbers from standard input and sum them, output the result."); |
42 | addOption( Bu::slot(this, &Options::grind), 'g', "grind", | ||
43 | "Search for magic numbers. Secret magic numbers."); | ||
42 | addOption( Bu::slot(this, &Options::version), 'v', "version", | 44 | addOption( Bu::slot(this, &Options::version), 'v', "version", |
43 | "Show the version string ('" CLIC_VERSION_STR "')"); | 45 | "Show the version string ('" CLIC_VERSION_STR "')"); |
44 | addHelpOption('h', "help", "This help"); | 46 | addHelpOption('h', "help", "This help"); |
@@ -224,3 +226,101 @@ int Options::version( Bu::StringArray aArgs ) | |||
224 | return 0; | 226 | return 0; |
225 | } | 227 | } |
226 | 228 | ||
229 | bool onlyNines( const Bu::String &s ) | ||
230 | { | ||
231 | for( Bu::String::const_iterator i = s.begin(); i; i++ ) | ||
232 | { | ||
233 | if( *i != '9' ) | ||
234 | return false; | ||
235 | } | ||
236 | return true; | ||
237 | } | ||
238 | |||
239 | bool onlyOnes( const Bu::String &s ) | ||
240 | { | ||
241 | for( Bu::String::const_iterator i = s.begin(); i; i++ ) | ||
242 | { | ||
243 | if( *i != '1' ) | ||
244 | return false; | ||
245 | } | ||
246 | return true; | ||
247 | } | ||
248 | |||
249 | int Options::grind( Bu::StringArray aArgs ) | ||
250 | { | ||
251 | Number test("1"); | ||
252 | Number bit("1"); | ||
253 | Number mult("2"); | ||
254 | int64_t iBits = 1; | ||
255 | int iLastSize = 0; | ||
256 | |||
257 | if( aArgs.getSize() > 1 ) | ||
258 | { | ||
259 | Bu::print("Loading number..."); Bu::sioRaw.flush(); | ||
260 | Bu::String num = Bu::File(aArgs[1], Bu::File::Read).readAll(); | ||
261 | test = num; | ||
262 | Bu::println("done."); | ||
263 | Bu::print("Computing bit..."); Bu::sioRaw.flush(); | ||
264 | bit = test; | ||
265 | bit = bit+Number("1"); | ||
266 | bit = bit/mult; | ||
267 | Bu::println("done."); | ||
268 | if( aArgs.getSize() > 2 ) | ||
269 | { | ||
270 | Bu::print("Trusting a human for where we are..."); Bu::sioRaw.flush(); | ||
271 | iBits = strtoll( aArgs[2].getStr(), NULL, 10 ); | ||
272 | } | ||
273 | else | ||
274 | { | ||
275 | Bu::print("Figuring out where we are..."); Bu::sioRaw.flush(); | ||
276 | Bu::String sBin = bit.toRadix(2).toString(); | ||
277 | iBits = sBin.getSize(); | ||
278 | } | ||
279 | // Bu::println("Done. Checking."); | ||
280 | // Bu::println("test = %1, bit = %2").arg( test ).arg( bit ); | ||
281 | // Bu::String sTest = test.toRadix(2).toString(); | ||
282 | // Bu::File("check-bit.txt", Bu::File::WriteNew ).write( sBin ); | ||
283 | // Bu::File("check-test.txt", Bu::File::WriteNew ).write( sTest ); | ||
284 | iLastSize = num.getSize(); | ||
285 | // Bu::println("File written. %1 bits").arg( sBin.getSize() ); | ||
286 | Bu::println("done."); | ||
287 | Bu::println("Continuing calculation..."); | ||
288 | } | ||
289 | |||
290 | for(;;) | ||
291 | { | ||
292 | bit = bit*mult; | ||
293 | test = test + bit; | ||
294 | Bu::String r = test.toString(); | ||
295 | iBits++; | ||
296 | |||
297 | if( onlyNines(r) ) | ||
298 | { | ||
299 | Bu::println("%1 digits").arg( r.getSize() ); | ||
300 | Bu::File f("answer-dec.txt", Bu::File::WriteNew ); | ||
301 | f.write( r ); | ||
302 | Bu::println("Success."); | ||
303 | // Bu::println("%1 == %2").arg( test.toRadix(2) ).arg( r ); | ||
304 | return 0; | ||
305 | } | ||
306 | if( r.getSize() != iLastSize && r.getSize()%1000 == 0 ) | ||
307 | { | ||
308 | /* | ||
309 | Bu::String rs = test.toRadix(2).toString(); | ||
310 | int iOBits = rs.getSize(); | ||
311 | if( iOBits != iBits ) | ||
312 | Bu::println("Bit mismatch: %1 vs %2").arg( iOBits ).arg( iBits ); | ||
313 | if( !onlyOnes( rs ) ) | ||
314 | Bu::println("Not only ones!"); | ||
315 | */ | ||
316 | iLastSize = r.getSize(); | ||
317 | Bu::println("%1 digits").arg( r.getSize() ); | ||
318 | Bu::File f(Bu::String("interum-%1-%2.txt").arg( r.getSize() ).arg( iBits ), Bu::File::WriteNew ); | ||
319 | f.write( r ); | ||
320 | } | ||
321 | } | ||
322 | Bu::println("Failure."); | ||
323 | exit( 0 ); | ||
324 | return 0; | ||
325 | } | ||
326 | |||