make
./hou 'expression'
./try.sh
As the script asks you, what does the last one mean? Can you get it to report a different value? Hint: it’s in this file, kind of.
This self-documenting scientific calculator compiles clean with no warnings under the most strict pedantic mode of gcc and clang.
The first example, along with the correct result appears in the source code. What does it mean?
The calculator obeys the precedence rules and allows parenthesis.
./hou '1+2*3'
./hou '(1+2)*3'
What exactly does the text formatting do?
What does this evaluate to in C?
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~+
Comments would almost be redundant. :-}
… and not just fancy formatted comment removed by the C Pre-processor.
The code does not contain some simple string-encoded lookup table for calculator operations.
The example input and output in the C code is significant. Can you find out why?
The button layout in the C code is significant … not just a nice looking layout.
The parser is functionally equivalent to a normal arithmetic parser for all valid inputs. For another fun challenge: try to determine how it parses the command line argument.
This program is designed to be self-documenting. It doesn’t just provide an example command line. It also provides the expected output and illustrates all supported functionality using embedded ASCII art.
printf(3)
/sscanf(3)
format strings, even
after preprocessing. No string-encoded lookup table is used.argv
to point to writable memory. It doesn’t require
the actual argv
strings to be writable, though.%e
style numbers like 1e-4
.The parser parses function names, subtraction, and division as single-letter unary operators. A bitmask encoded hash table is used to check no-ops/digits and another hash function maps the ASCII to a function ordinal, which is then executed by the button panel.
Both hash functions were found using a separate search program. Another program was used to find a way to format one hash function as example input such that it produces something resembling the bitmask hash table as part of its output.