Reverse Polish notation: algorithm, methods and examples

Reverse Polish notation once formed the basis of the world of computer programmer. Today she is not so well known. Therefore, a comic illustration depicting the “reverse” Polish sausage outside the bun can still be misunderstood by some knowledgeable programmers. It is not good to explain the joke, but in this case it will be fully justified.

Infix entry

. , + . , , , . , . . , + (, ), . , , - .

reverse polish notation




Fortran , (. . ) () , – FORmula TRANslation. , , (, (b, c)). , , Add A To B Mutliply By C.





?

, , . - . , , , , 2 + 3 * 4 2 3, 4, . 3 4 2. , . , , . , (2 + 3) * (4 + 5) , 2 + 3 * 4 + 5 , 3 4 2 5.

reverse polish notation examples




, , . - , , , . . , , , . , « », .





. . 1920- . , . , , (). , ( ), . . , + A + B.

reverse pascal polish




, n- , , . . . , , ABC @ , A, B C. 3 @ (A, , ). @ , A @ BC - , , .

, . , , . , + * – ( + ) * , , , . AB + C * , A B + C * -> (A B +) * C -> (A+B)*C.

reverse polish notation algorithm




, , , . , , . , 5 + 6 * 7 5, 6, 7 *, +, . , , 2 , . .

:

  • S = () 5, 6, 7, *, + 5 .
  • S = (5) 6, 7, *, + 6 .
  • S = (5, 6) 7, *, + 7 .
  • S = (5, 6, 7) *, + 2 , * .
  • S = (5, 6 * 7) = (5, 42) + 2 , + .
  • S = (5 + 42) = (47) , .

, , , .

. , , . , , .

reverse polish notation c implementation




( ).

, , . , .

toktype := num;

read();

if in ['+', '-', '*', '/'] then begin

if eoln then cn := ' ' else read(cn);

if cn = ' ' then

case of

'+': toktype := add; '-': toktype := sub;

'*': toktype := mul; '/': toktype := div

end

else begin

if = '-' then sgn := -1 else error := <> '+';

:= cn

end

end;

if (not error) and (toktype = num) then getnumber;

if toktype <> num then begin

:= ; := ;

if not error then

case toktype of

add: z := +; sub: z := -; mul: z := *; div: z := /

end

push(z);

C- ( ):

for (s = strtok(s, w); s; s = strtok(0, w)) {

a = strtod(s, &e);

if (e > s) push(a);

#define rpnop(x) printf("%:", *s), b = pop(), a = pop(), push(x)

else if (*s == '+') rpnop(a + b);

else if (*s == '-') rpnop(a - b);

else if (*s == '*') rpnop(a * b);

else if (*s == '/') rpnop(a / b);

#undef rpnop

}

algorithms and methods reverse polish notation




, , . 1960- ., , , . 2 3 2, 3 "". , , , , .

Burroughs , , . , , – . , n . , Return . . , , . , , , , , .

, . , - , Forth. , .

?

, , -. , . – . , . . . , , , ... , ...




All Articles