Problem 6 - Pretty Parenthesizer
Introduction
Mathemeticians usually use parentheses of different sizes to help
readers determine which opening parenthesis matches which closing
parenthesis in complex expressions with parentheses nested many levels
deep. Any pair of parentheses that does not enclose another pair
should be the smallest possible size. Any pair that does enclose another
pair should be one size bigger than the largest pair it encloses.
On the computer screen we will represent the smallest parentheses using
the normal parentheses characters ('(' and ')'). Larger parentheses
will be built from pipe and slash symbols ('|', '\', and '/'), with smallest
such parentheses built from one pipe and a slash at the top and bottom,
the next size up built from three pipes with a slash at the top and bottom,
and so forth. The slashes should be to the right of the pipes for
an open parenthesis and to the left of the pipes for a close parenthesis.
Input
The input will be a single line containing
a non-empty mathematical expression with any number of
nested parentheses. The parentheses will be properly balanced. There will
be no leading or trailing spaces in the input.
Output
The output must be the same expression with the parentheses symbols
replaced by ASCII art drawings of larger parentheses where the size
is determined by the rules above. The center of the parentheses must
be in line with the rest of the symbols in the expression. The spacing
of the expression must not be changed except that parentheses of size
two or more should occupy two columns instead of one.
Example
Input:
( ( x + y ) (y * (z+1)) )
In the output below, the last row of numbers is given only to help you
see how the spacing works. It should not be output by your program.
Output:
/ \
| / \ |
| ( x + y ) | y * (z+1) | |
| \ / |
\ /
01234567890123456789012345678
Note: there is no trailing space after the slashes on the first and last lines.