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 #100235: XXXCalc
by Meir Maor
the eXcessively eXtensible Xml Calculator,
Allows defining new operations in Xml file, and
using various mathematical systems as a compile time option.
solves arithmetic by repeatedly simplifying expressions.
The basic Numeric representation is defined by supplying a concrete
subclass of the Abstract class Number.
two built in implementations of the Class Number are provided:
* DecimalNumber - the default, which handles boring math over the field Q.
* ZpNumber - performs math over finite fields of size p(p being prime).
selecting between these is a compile time option, others can be added easily.
The actual operations the calculator engine supports are far more robust
and are defined in an XML file named operations.xml
Using this file the user can define any operation he wants by recursively using
the only functionality the Class Number needs to provide is:
successor & predecessor functions. - the base of all math.
divTen - a minimalistic approach for achieving fractions.
compareTo - to compare different Numbers.
isZero - to identify the addition neutral number.
In addition we need to supply methods for converting the number to and from a string.
The calcEngine module gets as input an expression as a string in standard
input, it uses a simple operator precedence model:
left to right no exceptions no biases, we believe this is the most just method
of performing arithmetic(no more biasing against addition and subtraction).
The processing is performed by expression manipulations done on strings, the initial
expression is translated to simpler expressions using the rules defined in operations.xml and this process is carried on until the expression is reduced to a single number(or error).
in the implementation of DecimalNumber we use the %n feature of printf in order
to measure the length of a number we just read, therefore the statement:
may seem like a debug statement(and could be used for this purpose as well)
it is actually an integral part of the calculation process.
The definition of division as defined in operations.xml is perfectly
accurate, not willing to settle to anything less then perfect it will not
allow any result for continuous fractions.
Addition and subtraction are defined only for natural numbers f(Q,N)->Q.
this obviously affects multiplication and division.
the operation divTen in DecimalNumber does not support more than one digit after the decimal point, we believe this is plenty as this is what is found in the test set. The problem is reduced even further since the integration with the GUI level does not pass on any fractions.
Due to the nature of XML parsing care must be taken when adding spaces to operations.xml it may not work as expected.
new operations may have up to 4 different branches in their recursive definition.
The actual calculation is far more robust then the basic gui and can(usually) process expretions with multiple operations (using "standard" left associative no precedence logic). The gui uses this by forking and executing the sub process and pipeing input and output, even though they are all C++, this allows us to change this in the future.
The design of the calculator was done by Yariv Yaari and Meir Maor,
written by Meir Maor.