Files
ioccc/2011/hou
Cody Boone Ferguson ef456bfb1e Mass typo fixes
This is a HUGE commit:

    417 files changed, 874 insertions(+), 874 deletions(-)

As the IOCCC website now is https enabled I have updated the
'http://www.ioccc' to 'https://www.ioccc'.

There was also a typo in Makefiles where it said 'for VERY WELCOME' when
it should be 'are VERY WELCOME'.

Finally slashdot.org is also https enabled so references to
'http://slashdot' were changed to 'https://slashdot'.

This was done by my sgit tool (see https://github.com/xexyl/sgit)
like:

    sgit -e 's,http://www.ioccc,https://www.ioccc,g' -e 's/for VERY WELCOME/are VERY WELCOME/g' -e  .

but because I didn't have a list of files that should be modified I had
to be careful which files to commit as I had files that were changed but
not ready to be committed. Since I did it in steps, finding typos as I
went along, I actually ran sgit three times but to simplify the above
(and to show that it's possible to do more than one sed command) I made
it all as one. Since I did not have a list of files ahead of time I had
to determine what files to commit, first going through them like:

    for j in $(for i in $(git status|grep modified|cut -d: -f2); do \
	git grep 'https://www.ioccc' "$i"; done|cut -d: -f1|sort|uniq); \
	do git diff "$j"; done|less -r

(I had to pipe through less because of a strange problem with the git
diff which appears to be because of a terminal problem maybe due to the
command above but it was all I could come up with in a very tired head.)

The above and

    git status | grep modified:

showed that some C files had been modified which obviously should not be
done so I had to do:

    git co '*c'

to undo those changes.

Finally to commit the correct files I did:

    for i in $(git status|grep modified|cut -d: -f2); do \
	git grep -E 'https://www.ioccc|https://slashdot|are VERY WELCOME' "$i"; \
	done |cut -d: -f1|sort|uniq|xargs git commit -v

or so (with any formatting issues or typos here due to typos in the log
due to the terminal (in)sanity issue described).
2023-10-07 04:35:47 -07:00
..
2023-05-13 11:45:33 +10:00
2020-12-30 16:57:03 -08:00
2023-07-06 21:49:38 -07:00
2023-10-07 04:35:47 -07:00
2023-05-03 01:24:34 -07:00

Best self documenting program

Hou Qiming
hqm03ster@gmail.com

To build:

make

To run:

./hou 'expression'

Try:

./hou '11/26+222/31'

./hou 'log((21701-19937)-(23209-21701))/log(2)'

./hou 'sin(1.5708)+0.04321+log(sqrt(exp(1*1*1)))+(1+2*3)-0.4-0.6+(4+6)*(2-1+2*3)-tan(0.785398)+2*10/3*6*sqrt(16/2/2)*10+10000-1000'

./hou 'a calculator expression -- see the C Code for details'

What does the last one mean?

Judges' remarks:

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?

+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~+

Self documenting source

Comments would almost be redundant. :-}

The ASCII art is valid C code

... and not just fancy formatted comment removed by the C Pre-processor.

The only multi-byte strings are format strings

The code does not contain some simple string-encoded lookup table for calculator operations.

Example input and output in the source

The example input and output in the C code is significant. Can you find out why?

The button layout

The button layout in the C code is significant ... not just a nice looking layout.

input parsing

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.

Author's remarks:

Obfuscations

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.

  • A few line numbers are significant and careless beautification will break the program.
  • The entire ASCII art is valid code. The preprocessor won't remove any part of it.
  • The only strings longer than 1 byte are printf/sscanf format strings, even after preprocessing. No string-encoded lookup table is used.
  • The example input and its expected output are both algorithmically significant.
  • The button layout is also significant.
  • The actual syntax being parsed is not that obvious, despite being functionally equivalent for all correct inputs.

Portability

  • This program requires argv to point to writable memory. It doesn't require the actual argv strings to be writable, though.
  • This program requires ASCII.
  • This program doesn't work with %e style numbers like 1e-4.
  • Some seemingly redundant code works to eliminate warnings and evade a gcc bug.

Spoilers

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.

This file is Copyright (c) 2023 by Landon Curt Noll. All Rights Reserved. You are free to share and adapt this file under the terms of this license:

Creative Commons Attribution-ShareAlike 4.0 International (CC BY-SA 4.0)

For more information, see: https://creativecommons.org/licenses/by-sa/4.0/