Reverse Polish Notation (RPN) is mathmatical convention for handling expressions. RPN is most commonly found in Hewlett-Packard calculators. RPN, like Forth and Postscript, is stack oriented. Operators only handle data stored on the stack. The infix (or normal) expression 3 + 5 would be expressed as 3 5 +. Note the operator goes at the end of the expression.
What really happens is that the 3 is pushed on the stack, then the 5 is pushed, and finally, when the + is encountered, it takes (pops) two numbers off the stack, adds them and puts the results back on the stack.
For the expression (1 + 2) * 3, you would enter 1 2 + 3 *. You don’t need ()’s to control what operation takes place first. RPN executes each operator as it comes across it.
Below is a Structured SuperBasic program that implements a simple RPN calculator. The left part of the screen is used for entry and results. The right part of the screen is used to show the current stack. With RPN, you can put a lot of numbers on the stack, then enter the operators for them.
Reverse Polish Notation Calculator
T = 0 : F = 0
CLS #1 : CLS #2
num = 0
sx = 0
DIM stack(30)
stackp = 1
num_entered = F
REPeat loop
in$=INKEY$(-1)
IF (in$ INSTR "0123456789") THEN
num = (10*num) + in$
PRINT #2,in$;
num_entered = T
END IF
IF in$ = "+" THEN
IF num_entered = T THEN
push(num)
num_entered = F
END IF
plus : num = 0
END IF
IF in$ = "-" THEN
IF num_entered = T THEN
push(num)
num_entered = F
END IF
minus : num = 0
END IF
IF in$ = "*" THEN
IF num_entered = T THEN
push(num)
num_entered = F
END IF
times : num = 0
END IF
IF in$ = "/" THEN
IF num_entered = T THEN
push(num)
num_entered = F
END IF
divide : num = 0
END IF
IF in$ = "^" THEN
IF num_entered = T THEN
push(num)
num_entered = F
END IF
power : num = 0
END IF
IF in$ = CHR$(10) THEN push(num) : num = 0 : PRINT #2
END REPeat loop
DEFine PROCedure push(x)
sx = sx + 1
AT sx,0 : PRINT x
stackp = stackp + 1
stack(stackp) = x
END DEFine push
DEFine FuNction pop
LOCal temp
IF stackp = 0 THEN
PRINT #2,"Stack Error"
STOP
ELSE
temp = stack(stackp)
AT sx,0 : PRINT " "
sx = sx - 1
IF sx = -1 THEN sx = 0
stackp = stackp - 1
RETURN temp
END IF
END DEFine pop
DEFine PROCedure plus
LOCal temp1, temp2
PRINT #2 : PRINT #2," +"
temp1 = pop
temp2 = pop
temp1 = temp1 + temp2
push(temp1)
PRINT #2," ="
PRINT #2,temp1
END DEFine plus
DEFine PROCedure minus
LOCal temp1, temp2
PRINT #2 : PRINT #2," -"
temp1 = pop
temp2 = pop
temp1 = temp1 - temp2
push(temp1)
PRINT #2," ="
PRINT #2,temp1
END DEFine minus
DEFine PROCedure times
LOCal temp1, temp2
PRINT #2 : PRINT #2," *"
temp1 = pop
temp2 = pop
temp1 = temp1 * temp2
push(temp1)
PRINT #2," ="
PRINT #2,temp1
END DEFine times
DEFine PROCedure divide
LOCal temp1, temp2
PRINT #2 : PRINT #2," /"
temp1 = pop
temp2 = pop
IF temp2 = 0 THEN
PRINT #2,"Divide by Zero Error"
ELSE
temp1 = temp1 / temp2
push(temp1)
PRINT #2," ="
PRINT #2,temp1
END IF
END DEFine divide
DEFine PROCedure power
LOCal temp1, temp2
PRINT #2 : PRINT #2," ^"
temp1 = pop
temp2 = pop
IF temp2 = 0 THEN
PRINT #2,"Negative Power Error"
ELSE
temp1 = temp1 ^ temp2
push(temp1)
PRINT #2," ="
PRINT #2,temp1
END IF
END DEFine power