Why not just break up the mathematical expression into in a tree? Then you can "percolate up" the tree generating the new expression line by line. The first line printed by perl would be all the leaf nodes with their conecting operator signs (+-*/).
The second line would be the leaf node operations completed and so on until you only have a root
You would basically need 2 routines.
1. Build-Tree -> responsible for taking an expression and making a tree out of it
2. iterate-tree -> responsible for performing the mathematical operations described at leaf level and replacing the parent node with the new answer.
i.e.
<some-root> <some-root>
/ /
+ => 9
/ \
5 4
Printing intermediate results is a breeze since it is in tree form. Just make sure that you adhere to the same principle you used when generating the tree (in-order, pre-order, or post-order).
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.