Creating infix to postfix.
By rhodium
- 485 reads
Converting Infix Notation to Reverse Polish
By Salim Meghani
To create the algorithm as suggested by A.S. Tanenbaum in his book
'Structured Computer
Organisation', it is necessary to-:
1. Treat the Texas Line as a stack.
2. Treat the California Line as a string.
3. Use the character at the switch to compare with the most recent item
on the stack, according to the decision table given.
4. Either push the character onto the stack or add it to the string or
delete both the character at the switch and stack, or take the
character at the stack and add it to the string.
This can be carried out within a do..while loop. The action of
finishing converting is legal when both the item on the stack and the
character at the switch are the terminator.
It is a fairly simple and trivial process for unsigned single integers.
More complex algorithms are required for signed floating point numbers.
It is suggested that M is used for unary minus.Then, the string is
searched for M's replacing with (0-.) to get the expression.
Evaluating Reverse Polish.
This is carried out in the following way-:
1. A while loop puts any integers onto the stack until an operator is
found from the California line.
2. When an operator is found two most recent items off the stack are
retrieved and answer
calculated.
3. The answer is put onto the stack.
This process is repeated until the end of the expression (California
Line is reached), when the answer is the item on the stack.
To make life simple (it is comfy when it is) use a stack of
doubles.
- Log in to post comments