About The Contest

The first ever Olympiad of Misguided Geeks contest at Worse Than Failure (or OMGWTF for short) is a new kind of programming contest. Readers are invited to be creative with devising a calculator with the craziest code they can write. One lucky and potentially insane winner will get either a brand new MacBook Pro or comparable Sony VAIO laptop.



Entry #100226: DL's OMGWTF Calc

by David Lees
A windows based calculator using string processing instead of numbers to do the calculations. Complete with badly implemented wooden table interface and insulting error messages. Basic addition/subtraction done with singly digit loopup tables (case statements). Long multiplication done by lookup and addition. A division function that takes a simple method (www.doubledivision.org) and implements it in a overly complex and buggy way.

Additional WTFs include:

All numbers are stored as strings, but in backwards order so 1234 is stored as "4321". This is done as i thought it easier to loop using i++ rather than i--;

Paint handler that determines which button to paint by its height and width. It then loads each BMP each time the button is drawn. Note that the BMPs need to be in the same place as the EXE in order for the buttons to be visible, needless to say the BMP names are hard coded with no error checking to see if they are there. Some buttons may actually overlap. Some of the buttons are checked more than once. ASCII art representations of the buttons are in the source code for reference.

The subtraction function was refactored from 2 medium length functions (virtually the same as the add function) to be 144 very small functions. The majority of which simply comprise of an if/else statement that calls one of two other functions. The naming convention used for the functions in this module are countries of the world form the song "Yakko's World". I prepended the country names instead of a simple rename so that its not to obfuscated.

Most of the maths is done using lookup tables, that is if you can describe nested case statements (or if/else when subtracting) as a form of lookup. Each inner function works on a single set of digits and returns the answer digit plus the carry.

The division function is based on the double division method published at www.doubledivision.org. This is supposed to be easier than conventioanl long division. However this doesn't translate very well into a usable function. This function is very badly written and gets stuck in an infinite loop if the division won't resolve in 15 significant figures. The is however a count that stops the loop after a couple of thousand tries. Also in this function is tri-boolean logic and abuse of the goto command, jumping from the if to the else part of an if/else statement.

Despite the WTF coding it does actually work and is very fast. Even when it gets stuck in an infinite loop it recovers and displays an answer within a few seconds. The only slow part is that if you try to press the imput buttons too fast it won't register. You need to leave approx 1 second between each press when using it.

Download3 Comments