News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

help with reverse poliish notation

Started by gammaman, June 26, 2009, 01:55:57 PM

Previous topic - Next topic

tenkey

Quote from: dedndave on July 01, 2009, 04:23:59 PM
also, i think with RPN, you are supposed to resolve things in parenthesis and brackets first, then work your way out
this may not be the same as
QuoteIn RPN, the operators are performed in the order written.

There are no brackets (or parentheses or braces) in RPN. I know the rule about mult and div before add and sub in standard infix notation.

Problem (infix notation):
(8-2)*[20/(8+2)+10]

Original RPN Answer:

82-82+20/10+*

Evaluation of Original RPN Answer:

-- 8 2 - 8 2 + 20 / 10 + *
8 -- 2 - 8 2 + 20 / 10 + *
8 2 -- - 8 2 + 20 / 10 + *
8 2 - -- 8 2 + 20 / 10 + *
6 -- 8 2 + 20 / 10 + *
6 8 -- 2 + 20 / 10 + *
6 8 2 -- + 20 / 10 + *
6 8 2 + -- 20 / 10 + *
6 10 -- 20 / 10 + *
6 10 20 -- / 10 + *
6 10 20 / -- 10 + *
At this point, how do you define 10 20 /  ???
Beforehand, "8 2 -" was evaluated as (8 - 2).
If we use the same pattern, "10 20 /" means (10 / 20); which results in the following steps:
6 0.5 -- 10 + *
6 0.5 10 -- + *
6 0.5 10 + -- *
6 10.5 -- *
6 10.5 * --
63 -- unexpected answer

My RPN Answer:

8 2 - 20 8 2 + / 10 + *

Evaluation of My RPN Answer:

-- 8 2 - 20 8 2 + / 10 + *
8 -- 2 - 20 8 2 + / 10 + *
8 2 -- - 20 8 2 + / 10 + *
8 2 - -- 20 8 2 + / 10 + *
6 -- 20 8 2 + / 10 + *
6 20 -- 8 2 + / 10 + *
6 20 8 -- 2 + / 10 + *
6 20 8 2 -- + / 10 + *
6 20 8 2 + -- / 10 + *
6 20 10 -- / 10 + *
6 20 10 / -- 10 + *
Note the order of the two values in front of /
6 2 -- 10 + *
6 2 10 + -- *
6 12 -- *
6 12 * --
72 -- the expected answer
A programming language is low level when its programs require attention to the irrelevant.
Alan Perlis, Epigram #8