Parsing/RPN calculator algorithm: Difference between revisions

m
m (→‎{{header|Wren}}: Minor tidy)
 
(9 intermediate revisions by 5 users not shown)
Line 585:
Result is: +3.00012207
</pre>
 
=={{header|ANSI Standard BASIC}}==
<syntaxhighlight lang="ansi standard basic">1000 DECLARE EXTERNAL SUB rpn
1010 PUBLIC NUMERIC R(64) ! stack
1020 PUBLIC STRING expn$ ! for keyboard input
1030 PUBLIC NUMERIC i, lenn, n, true, false ! global values
1040 LET true = -1
1050 LET false = 0
1060 DO
1070 PRINT "enter an RPN expression:"
1080 INPUT expn$
1090 IF LEN( expn$ ) = 0 THEN EXIT DO
1100 PRINT "expn: ";expn$
1110 CALL rpn( expn$ )
1120 LOOP
1130 END
1140 !
1150 ! interpret reverse polish (postfix) expression
1160 EXTERNAL SUB rpn( expn$ )
1170 DECLARE EXTERNAL FUNCTION is_digit, get_number
1180 DECLARE EXTERNAL SUB print_stack
1190 DECLARE STRING ch$
1200 LET expn$ = expn$ & " " ! must terminate line with space
1210 LET lenn = LEN( expn$ )
1220 LET i = 0
1230 LET n = 1
1240 LET R(n) = 0.0 ! push zero for unary operations
1250 DO
1260 IF i >= lenn THEN EXIT DO ! at end of line
1270 LET i = i + 1
1280 IF expn$(i:i) <> " " THEN ! skip white spaces
1290 IF is_digit( expn$(i:i) ) = true THEN ! push number onto stack
1300 LET n = n + 1
1310 LET R(n) = get_number
1320 CALL print_stack
1330 ELSEIF expn$(i:i) = "+" then ! add and pop stack
1340 IF n < 2 THEN
1350 PRINT "stack underflow"
1360 ELSE
1370 LET R(n-1) = R(n-1) + R(n)
1380 LET n = n - 1
1390 CALL print_stack
1400 END IF
1410 ELSEIF expn$(i:i) = "-" then ! subtract and pop stack
1420 IF n < 2 THEN
1430 PRINT "stack underflow"
1440 ELSE
1450 LET R(n-1) = R(n-1) - R(n)
1460 LET n = n - 1
1470 CALL print_stack
1480 END IF
1490 ELSEIF expn$(i:i) = "*" then ! multiply and pop stack
1500 IF n < 2 THEN
1510 PRINT "stack underflow"
1520 ELSE
1530 LET R(n-1) = R(n-1) * R(n)
1540 LET n = n - 1
1550 CALL print_stack
1560 END IF
1570 ELSEIF expn$(i:i) = "/" THEN ! divide and pop stack
1580 IF n < 2 THEN
1590 PRINT "stack underflow"
1600 ELSE
1610 LET R(n-1) = R(n-1) / R(n)
1620 LET n = n - 1
1630 CALL print_stack
1640 END IF
1650 ELSEIF expn$(i:i) = "^" THEN ! raise to power and pop stack
1660 IF n < 2 THEN
1670 PRINT "stack underflow"
1680 ELSE
1690 LET R(n-1) = R(n-1) ^ R(n)
1700 LET n = n - 1
1710 CALL print_stack
1720 END IF
1730 ELSE
1740 PRINT REPEAT$( " ", i+5 ); "^ error"
1750 EXIT DO
1760 END IF
1770 END IF
1780 LOOP
1790 PRINT "result: "; R(n) ! end of main program
1800 END SUB
1810 !
1820 ! extract a number from a string
1830 EXTERNAL FUNCTION get_number
1840 DECLARE EXTERNAL FUNCTION is_digit
1850 LET j = 1 ! start of number string
1860 DECLARE STRING number$ ! buffer for conversion
1870 DO ! get integer part
1880 LET number$(j:j) = expn$(i:i)
1890 LET i = i + 1
1900 LET j = j + 1
1910 IF is_digit( expn$(i:i) ) = false THEN
1920 IF expn$(i:i) = "." then
1930 LET number$(j:j) = expn$(i:i) ! include decimal point
1940 LET i = i + 1
1950 LET j = j + 1
1960 DO WHILE is_digit( expn$(i:i) ) = true ! get fractional part
1970 LET number$(j:j) = expn$(i:i)
1980 LET i = i + 1
1990 LET j = j + 1
2000 LOOP
2010 END IF
2020 EXIT DO
2030 END IF
2040 LOOP
2050 LET get_number = VAL( number$ )
2060 END FUNCTION
2070 !
2080 ! check for digit character
2090 EXTERNAL FUNCTION is_digit( ch$ )
2100 IF "0" <= expn$(i:i) AND expn$(i:i) <= "9" THEN
2110 LET is_digit = true
2120 ELSE
2130 LET is_digit = false
2140 END IF
2150 END FUNCTION
2160 !
2170 EXTERNAL SUB print_stack
2180 PRINT expn$(i:i);" ";
2190 FOR ptr=n TO 2 STEP -1
2200 PRINT USING "-----%.####":R(ptr);
2210 NEXT ptr
2220 PRINT
2230 END SUB</syntaxhighlight>
 
=={{header|ANTLR}}==
Line 805 ⟶ 679:
The final output value is: '3.000122'</pre>
 
=={{header|BBC BASIC}}==
==={{header|ANSI BASIC}}===
{{works with|Decimal BASIC}}
<syntaxhighlight lang="basic">1000 DECLARE EXTERNAL SUB rpn
1010 PUBLIC NUMERIC R(64) ! stack
1020 PUBLIC STRING expn$ ! for keyboard input
1030 PUBLIC NUMERIC i, lenn, n, true, false ! global values
1040 LET true = -1
1050 LET false = 0
1060 DO
1070 PRINT "enter an RPN expression:"
1080 INPUT expn$
1090 IF LEN( expn$ ) = 0 THEN EXIT DO
1100 PRINT "expn: ";expn$
1110 CALL rpn( expn$ )
1120 LOOP
1130 END
1140 !
1150 ! interpret reverse polish (postfix) expression
1160 EXTERNAL SUB rpn( expn$ )
1170 DECLARE EXTERNAL FUNCTION is_digit, get_number
1180 DECLARE EXTERNAL SUB print_stack
1190 DECLARE STRING ch$
1200 LET expn$ = expn$ & " " ! must terminate line with space
1210 LET lenn = LEN( expn$ )
1220 LET i = 0
1230 LET n = 1
1240 LET R(n) = 0.0 ! push zero for unary operations
1250 DO
1260 IF i >= lenn THEN EXIT DO ! at end of line
1270 LET i = i + 1
1280 IF expn$(i:i) <> " " THEN ! skip white spaces
1290 IF is_digit( expn$(i:i) ) = true THEN ! push number onto stack
1300 LET n = n + 1
1310 LET R(n) = get_number
1320 CALL print_stack
1330 ELSEIF expn$(i:i) = "+" then ! add and pop stack
1340 IF n < 2 THEN
1350 PRINT "stack underflow"
1360 ELSE
1370 LET R(n-1) = R(n-1) + R(n)
1380 LET n = n - 1
1390 CALL print_stack
1400 END IF
1410 ELSEIF expn$(i:i) = "-" then ! subtract and pop stack
1420 IF n < 2 THEN
1430 PRINT "stack underflow"
1440 ELSE
1450 LET R(n-1) = R(n-1) - R(n)
1460 LET n = n - 1
1470 CALL print_stack
1480 END IF
1490 ELSEIF expn$(i:i) = "*" then ! multiply and pop stack
1500 IF n < 2 THEN
1510 PRINT "stack underflow"
1520 ELSE
1530 LET R(n-1) = R(n-1) * R(n)
1540 LET n = n - 1
1550 CALL print_stack
1560 END IF
1570 ELSEIF expn$(i:i) = "/" THEN ! divide and pop stack
1580 IF n < 2 THEN
1590 PRINT "stack underflow"
1600 ELSE
1610 LET R(n-1) = R(n-1) / R(n)
1620 LET n = n - 1
1630 CALL print_stack
1640 END IF
1650 ELSEIF expn$(i:i) = "^" THEN ! raise to power and pop stack
1660 IF n < 2 THEN
1670 PRINT "stack underflow"
1680 ELSE
1690 LET R(n-1) = R(n-1) ^ R(n)
1700 LET n = n - 1
1710 CALL print_stack
1720 END IF
1730 ELSE
1740 PRINT REPEAT$( " ", i+5 ); "^ error"
1750 EXIT DO
1760 END IF
1770 END IF
1780 LOOP
1790 PRINT "result: "; R(n) ! end of main program
1800 END SUB
1810 !
1820 ! extract a number from a string
1830 EXTERNAL FUNCTION get_number
1840 DECLARE EXTERNAL FUNCTION is_digit
1850 LET i1 = i ! start of number substring
1860 DO ! get integer part
1870 LET i = i + 1
1880 IF is_digit( expn$(i:i) ) = false THEN
1890 IF expn$(i:i) = "." THEN
1900 LET i = i + 1
1910 DO WHILE is_digit( expn$(i:i) ) = true ! get fractional part
1920 LET i = i + 1
1930 LOOP
1940 END IF
1950 EXIT DO
1960 END IF
1970 LOOP
1980 LET get_number = VAL( expn$(i1:i - 1) )
1990 END FUNCTION
2000 !
2010 ! check for digit character
2020 EXTERNAL FUNCTION is_digit( ch$ )
2030 IF "0" <= ch$ AND ch$ <= "9" THEN
2040 LET is_digit = true
2050 ELSE
2060 LET is_digit = false
2070 END IF
2080 END FUNCTION
2090 !
2100 EXTERNAL SUB print_stack
2110 PRINT expn$(i:i);" ";
2120 FOR ptr=n TO 2 STEP -1
2130 PRINT USING "-----%.####":R(ptr);
2140 NEXT ptr
2150 PRINT
2160 END SUB</syntaxhighlight>
{{out}}
<pre>
enter an RPN expression:
? 3 4 2 * 1 5 - 2 3 ^ ^ / +
expn: 3 4 2 * 1 5 - 2 3 ^ ^ / +
3.0000
4.0000 3.0000
2.0000 4.0000 3.0000
* 8.0000 3.0000
1.0000 8.0000 3.0000
5.0000 1.0000 8.0000 3.0000
- -4.0000 8.0000 3.0000
2.0000 -4.0000 8.0000 3.0000
3.0000 2.0000 -4.0000 8.0000 3.0000
^ 8.0000 -4.0000 8.0000 3.0000
^ 65536.0000 8.0000 3.0000
/ 0.0001 3.0000
+ 3.0001
result: 3.0001220703125
enter an RPN expression:
?
 
</pre>
 
==={{header|BBC BASIC}}===
<syntaxhighlight lang="bbcbasic"> @% = &60B
RPN$ = "3 4 2 * 1 5 - 2 3 ^ ^ / +"
Line 860 ⟶ 878:
+ : 3.00012
</pre>
 
==={{header|FreeBASIC}}===
<syntaxhighlight lang="freebasic">#define NULL 0
 
type node
'implement the stack as a linked list
n as double
p as node ptr
end type
 
function spctok( byref s as string ) as string
'returns everything in the string up to the first space
'modifies the original string to begin at the fist non-space char after the first space
dim as string r
dim as double i = 1
while mid(s,i,1)<>" " and i<=len(s)
r += mid(s,i,1)
i+=1
wend
do
i+=1
loop until mid(s,i,1)<>" " or i >= len(s)
s = right(s,len(s)-i+1)
return r
end function
 
sub print_stack( byval S as node ptr )
'display everything on the stack
print "Stack <--- ";
while S->p <> NULL
S = S->p
print S->n;" ";
wend
print
end sub
 
sub push( byval S as node ptr, v as double )
'push a value onto the stack
dim as node ptr x
x = allocate(sizeof(node))
x->n = v
x->p = S->p
S->p = x
end sub
 
function pop( byval S as node ptr ) as double
'pop a value from the stack
if s->P = NULL then return -99999
dim as double r = S->p->n
dim as node ptr junk = S->p
S->p = S->p->p
deallocate(junk)
return r
end function
 
dim as string s = "3 4 2 * 1 5 - 2 3 ^ ^ / +", c
dim as node StackHead
 
while len(s) > 0
c = spctok(s)
print "Token: ";c;" ";
select case c
case "+"
push(@StackHead, pop(@StackHead) + pop(@StackHead))
print "Operation + ";
case "-"
push(@StackHead, -(pop(@StackHead) - pop(@StackHead)))
print "Operation - ";
case "/"
push(@StackHead, 1./(pop(@StackHead) / pop(@StackHead)))
print "Operation / ";
case "*"
push(@StackHead, pop(@StackHead) * pop(@StackHead))
print "Operation * ";
case "^"
push(@StackHead, pop(@StackHead) ^ pop(@StackHead))
print "Operation ^ ";
case else
push(@StackHead, val(c))
print "Operation push ";
end select
print_stack(@StackHead)
wend</syntaxhighlight>
{{out}}<pre>
Token: 3 Operation push Stack <--- 3
Token: 4 Operation push Stack <--- 4 3
Token: 2 Operation push Stack <--- 2 4 3
Token: * Operation * Stack <--- 8 3
Token: 1 Operation push Stack <--- 1 8 3
Token: 5 Operation push Stack <--- 5 1 8 3
Token: - Operation - Stack <--- -4 8 3
Token: 2 Operation push Stack <--- 2 -4 8 3
Token: 3 Operation push Stack <--- 3 2 -4 8 3
Token: ^ Operation ^ Stack <--- 8 -4 8 3
Token: ^ Operation ^ Stack <--- 65536 8 3
Token: / Operation / Stack <--- 0.0001220703125 3
Token: + Operation + Stack <--- 3.0001220703125
</pre>
 
=== {{header|GW-BASIC}} ===
{{trans|QuickBASIC}}
Supports multi-digit numbers and negative numbers.
{{works with|BASICA}}
<syntaxhighlight lang="gwbasic">
10 REM Parsing/RPN calculator algorithm
20 MAX.INDEX% = 63
30 REM Stack
40 REM TOP.INDEX% - top index of the stack
50 DIM ELEMS(MAX.INDEX%)
60 EXPR$ = "3 4 2 * 1 5 - 2 3 ^ ^ / +": GOSUB 200
70 END
190 REM ** Evaluate the expression in RPN
200 GOSUB 1000
210 PRINT "Input", "Operation", "Stack after"
220 REM SP% - start position of token, DP% - position of delimiter
230 DP% = 0
240 REM Loop: do ... until DP% = 0
250 SP% = DP% + 1
260 DP% = INSTR(DP% + 1, EXPR$, " ")
270 IF DP% = 0 THEN TOKEN$ = MID$(EXPR$, SP%, LEN(EXPR$) - SP% + 1) ELSE TE% = DP% - 1: TOKEN$ = MID$(EXPR$, SP%, DP% - SP%)
280 PRINT TOKEN$,
290 IF TOKEN$ <> "*" THEN 350
300 PRINT "Operate",
310 GOSUB 1060: SECOND = POP
320 GOSUB 1060: FIRST = POP
330 X = FIRST * SECOND: GOSUB 1160
340 GOTO 610
350 IF TOKEN$ <> "/" THEN 410
360 PRINT "Operate",
370 GOSUB 1060: SECOND = POP
380 GOSUB 1060: FIRST = POP
390 X = FIRST / SECOND: GOSUB 1160
400 GOTO 610
410 IF TOKEN$ <> "-" THEN 470
420 PRINT "Operate",
430 GOSUB 1060: SECOND = POP
440 GOSUB 1060: FIRST = POP
450 X = FIRST - SECOND: GOSUB 1160
460 GOTO 610
470 IF TOKEN$ <> "+" THEN 530
480 PRINT "Operate",
490 GOSUB 1060: SECOND = POP
500 GOSUB 1060: FIRST = POP
510 X = FIRST + SECOND: GOSUB 1160
520 GOTO 610
530 IF TOKEN$ <> "^" THEN 590
540 PRINT "Operate",
550 GOSUB 1060: SECOND = POP
560 GOSUB 1060: FIRST = POP
570 X = FIRST ^ SECOND: GOSUB 1160
580 GOTO 610
590 PRINT "Push",
600 X = VAL(TOKEN$): GOSUB 1160
610 GOSUB 1100
620 IF DP% <> 0 THEN 250
630 GOSUB 1060:
640 PRINT "Final answer: "; POP
650 GOSUB 1030
660 IF NOT EMPTY% THEN PRINT "Error, too many operands: "; : GOSUB 1100: STOP
670 RETURN
980 REM ** Operations on the stack
990 REM ** Make the stack empty
1000 TOP.INDEX% = MAX.INDEX% + 1
1010 RETURN
1020 REM ** Is the stack empty?
1030 EMPTY% = TOP.INDEX% > MAX.INDEX%
1040 RETURN
1050 REM ** Pop from the stack
1060 GOSUB 1030
1070 IF NOT EMPTY% THEN POP = ELEMS(TOP.INDEX%): TOP.INDEX% = TOP.INDEX% + 1 ELSE PRINT "The stack is empty.": STOP
1080 RETURN
1090 REM ** Print the stack
1100 FOR PTR% = TOP.INDEX% TO MAX.INDEX%
1110 PRINT USING "######.###"; ELEMS(PTR%);
1120 NEXT PTR%
1130 PRINT
1140 RETURN
1150 REM ** Push to the stack
1160 IF TOP.INDEX% > 0 THEN TOP.INDEX% = TOP.INDEX% - 1: ELEMS(TOP.INDEX%) = X ELSE PRINT "The stack is full.": STOP
1170 RETURN
</syntaxhighlight>
{{out}}
<pre>
Input Operation Stack after
3 Push 3.000
4 Push 4.000 3.000
2 Push 2.000 4.000 3.000
* Operate 8.000 3.000
1 Push 1.000 8.000 3.000
5 Push 5.000 1.000 8.000 3.000
- Operate -4.000 8.000 3.000
2 Push 2.000 -4.000 8.000 3.000
3 Push 3.000 2.000 -4.000 8.000 3.000
^ Operate 8.000 -4.000 8.000 3.000
^ Operate 65536.000 8.000 3.000
/ Operate 0.000 3.000
+ Operate 3.000
Final answer: 3.000122
</pre>
 
==={{header|Liberty BASIC}}===
{{works with|Just BASIC}}
<syntaxhighlight lang="lb">
global stack$
 
expr$ = "3 4 2 * 1 5 - 2 3 ^ ^ / +"
print "Expression:"
print expr$
print
 
print "Input","Operation","Stack after"
 
stack$=""
token$ = "#"
i = 1
token$ = word$(expr$, i)
token2$ = " "+token$+" "
 
do
print "Token ";i;": ";token$,
select case
'operation
case instr("+-*/^",token$)<>0
print "operate",
op2$=pop$()
op1$=pop$()
if op1$="" then
print "Error: stack empty for ";i;"-th token: ";token$
end
end if
 
op1=val(op1$)
op2=val(op2$)
 
select case token$
case "+"
res = op1+op2
case "-"
res = op1-op2
case "*"
res = op1*op2
case "/"
res = op1/op2
case "^"
res = op1^op2
end select
 
call push str$(res)
'default:number
case else
print "push",
call push token$
end select
print "Stack: ";reverse$(stack$)
i = i+1
token$ = word$(expr$, i)
token2$ = " "+token$+" "
loop until token$ =""
 
res$=pop$()
print
print "Result:" ;res$
extra$=pop$()
if extra$<>"" then
print "Error: extra things on a stack: ";extra$
end if
end
 
'---------------------------------------
function reverse$(s$)
reverse$ = ""
token$="#"
while token$<>""
i=i+1
token$=word$(s$,i,"|")
reverse$ = token$;" ";reverse$
wend
end function
'---------------------------------------
sub push s$
stack$=s$+"|"+stack$ 'stack
end sub
 
function pop$()
'it does return empty on empty stack
pop$=word$(stack$,1,"|")
stack$=mid$(stack$,instr(stack$,"|")+1)
end function
</syntaxhighlight>
 
{{out}}
<pre>
Expression:
3 4 2 * 1 5 - 2 3 ^ ^ / +
 
Input Operation Stack after
Token 1: 3 push Stack: 3
Token 2: 4 push Stack: 3 4
Token 3: 2 push Stack: 3 4 2
Token 4: * operate Stack: 3 8
Token 5: 1 push Stack: 3 8 1
Token 6: 5 push Stack: 3 8 1 5
Token 7: - operate Stack: 3 8 -4
Token 8: 2 push Stack: 3 8 -4 2
Token 9: 3 push Stack: 3 8 -4 2 3
Token 10: ^ operate Stack: 3 8 -4 8
Token 11: ^ operate Stack: 3 8 65536
Token 12: / operate Stack: 3 0.12207031e-3
Token 13: + operate Stack: 3.00012207
 
Result:3.00012207
</pre>
 
==={{header|QuickBASIC}}===
{{trans|Java|In fact, stack and tokenizing had to be implemented. Converting string to numbers is based on the <code>VAL</code> function in BASIC.}}
Supports multi-digit numbers and negative numbers.
<syntaxhighlight lang="qbasic">
' Parsing/RPN calculator algorithm
DECLARE SUB MakeEmpty (S AS ANY)
DECLARE SUB Push (X AS SINGLE, S AS ANY)
DECLARE SUB PrintStack (S AS ANY)
DECLARE SUB EvalRPN (Expr$)
DECLARE FUNCTION Empty% (S AS ANY)
DECLARE FUNCTION Pop! (S AS ANY)
 
CONST MAXINDEX = 63
 
TYPE TNumStack
TopIndex AS INTEGER
Elems(MAXINDEX) AS SINGLE
END TYPE
 
EvalRPN ("3 4 2 * 1 5 - 2 3 ^ ^ / +")
END
 
FUNCTION Empty% (S AS TNumStack)
Empty% = S.TopIndex > MAXINDEX
END FUNCTION
 
SUB EvalRPN (Expr$)
DIM S AS TNumStack
MakeEmpty S
PRINT "Input", "Operation", "Stack after"
' SP% - start position of token
' DP% - position of delimiter
DP% = 0
DO
SP% = DP% + 1
DP% = INSTR(DP% + 1, Expr$, " ")
IF DP% <> 0 THEN
TE% = DP% - 1
Token$ = MID$(Expr$, SP%, DP% - SP%)
ELSE
Token$ = MID$(Expr$, SP%, LEN(Expr$) - SP% + 1)
END IF
PRINT Token$,
IF Token$ = "*" THEN
PRINT "Operate",
Second = Pop(S): First = Pop(S)
Push First * Second, S
ELSEIF Token$ = "/" THEN
PRINT "Operate",
Second = Pop(S): First = Pop(S)
Push First / Second, S
ELSEIF Token$ = "-" THEN
PRINT "Operate",
Second = Pop(S): First = Pop(S)
Push First - Second, S
ELSEIF Token$ = "+" THEN
PRINT "Operate",
Second = Pop(S): First = Pop(S)
Push First + Second, S
ELSEIF Token$ = "^" THEN
PRINT "Operate",
Second = Pop(S): First = Pop(S)
Push First ^ Second, S
ELSE
PRINT "Push",
Push VAL(Token$), S
END IF
PrintStack S
LOOP UNTIL DP% = 0
PRINT "Final answer: "; Pop(S)
IF NOT Empty(S) THEN
PRINT "Error, too many operands: ";
PrintStack S
STOP
END IF
END SUB
 
SUB MakeEmpty (S AS TNumStack)
S.TopIndex = MAXINDEX + 1
END SUB
 
FUNCTION Pop (S AS TNumStack)
IF Empty%(S) THEN
PRINT "The stack is empty."
STOP
ELSE
Pop = S.Elems(S.TopIndex)
S.TopIndex = S.TopIndex + 1
END IF
END FUNCTION
 
SUB PrintStack (S AS TNumStack)
FOR Ptr% = S.TopIndex% TO MAXINDEX
PRINT USING "######.###"; S.Elems(Ptr%);
NEXT Ptr%
PRINT
END SUB
 
SUB Push (X AS SINGLE, S AS TNumStack)
IF S.TopIndex = 0 THEN
PRINT "The stack is full."
STOP
ELSE
S.TopIndex = S.TopIndex - 1
S.Elems(S.TopIndex) = X
END IF
END SUB
</syntaxhighlight>
{{out}}
<pre>
Input Operation Stack after
3 Push 3.000
4 Push 4.000 3.000
2 Push 2.000 4.000 3.000
* Operate 8.000 3.000
1 Push 1.000 8.000 3.000
5 Push 5.000 1.000 8.000 3.000
- Operate -4.000 8.000 3.000
2 Push 2.000 -4.000 8.000 3.000
3 Push 3.000 2.000 -4.000 8.000 3.000
^ Operate 8.000 -4.000 8.000 3.000
^ Operate 65536.000 8.000 3.000
/ Operate 0.000 3.000
+ Operate 3.000
Final answer: 3.000122
</pre>
 
==={{header|Run BASIC}}===
<syntaxhighlight lang="runbasic">prn$ = "3 4 2 * 1 5 - 2 3 ^ ^ / + "
 
j = 0
while word$(prn$,i + 1," ") <> ""
i = i + 1
n$ = word$(prn$,i," ")
if n$ < "0" or n$ > "9" then
num1 = val(word$(stack$,s," "))
num2 = val(word$(stack$,s-1," "))
n = op(n$,num2,num1)
s = s - 1
stack$ = stk$(stack$,s -1,str$(n))
print "Push Opr ";n$;" to stack: ";stack$
else
s = s + 1
stack$ = stack$ + n$ + " "
print "Push Num ";n$;" to stack: ";stack$
end if
wend
 
function stk$(stack$,s,a$)
for i = 1 to s
stk$ = stk$ + word$(stack$,i," ") + " "
next i
stk$ = stk$ + a$ + " "
end function
 
FUNCTION op(op$,a,b)
if op$ = "*" then op = a * b
if op$ = "/" then op = a / b
if op$ = "^" then op = a ^ b
if op$ = "+" then op = a + b
if op$ = "-" then op = a - b
end function</syntaxhighlight>
<pre>Push Num 3 to stack: 3
Push Num 4 to stack: 3 4
Push Num 2 to stack: 3 4 2
Push Opr * to stack: 3 8
Push Num 1 to stack: 3 8 1
Push Num 5 to stack: 3 8 1 5
Push Opr - to stack: 3 8 -4
Push Num 2 to stack: 3 8 -4 2
Push Num 3 to stack: 3 8 -4 2 3
Push Opr ^ to stack: 3 8 -4 8
Push Opr ^ to stack: 3 8 65536
Push Opr / to stack: 3 1.22070312e-4
Push Opr + to stack: 3.00012207</pre>
 
==={{header|Sinclair ZX81 BASIC}}===
If you only have 1k of RAM, this program will correctly evaluate the test expression with fewer than 10 bytes to spare. (I know that because I tried running it with the first line modified to allow a stack depth of 7, i.e. allocating space for two more 40-bit floats, and it crashed with an "out of memory" error code before it could print the result of the final addition.) If we desperately needed a few extra bytes there are ways they could be shaved out of the current program; but this version works, and editing a program that takes up almost all your available RAM isn't very comfortable, and to make it really useful for practical purposes you would still want to have 2k or more anyway.
 
The ZX81 character set doesn't include <code>^</code>, so we have to use <code>**</code> instead. Note that this is not two separate stars, although that's what it looks like: you have to enter it by typing <code>SHIFT</code>+<code>H</code>.
 
No attempt is made to check for invalid syntax, stack overflow or underflow, etc.
 
<syntaxhighlight lang="basic"> 10 DIM S(5)
20 LET P=1
30 INPUT E$
40 LET I=0
50 LET I=I+1
60 IF E$(I)=" " THEN GOTO 110
70 IF I<LEN E$ THEN GOTO 50
80 LET W$=E$
90 GOSUB 150
100 STOP
110 LET W$=E$( TO I-1)
120 LET E$=E$(I+1 TO )
130 GOSUB 150
140 GOTO 40
150 IF W$="+" OR W$="-" OR W$="*" OR W$="/" OR W$="**" THEN GOTO 250
160 LET S(P)=VAL W$
170 LET P=P+1
180 PRINT W$;
190 PRINT ":";
200 FOR I=P-1 TO 1 STEP -1
210 PRINT " ";S(I);
220 NEXT I
230 PRINT
240 RETURN
250 IF W$="**" THEN LET S(P-2)=ABS S(P-2)
260 LET S(P-2)=VAL (STR$ S(P-2)+W$+STR$ S(P-1))
270 LET P=P-1
280 GOTO 180</syntaxhighlight>
{{in}}
<pre>3 4 2 * 1 5 - 2 3 ** ** / +</pre>
{{out}}
<pre>3: 3
4: 4 3
2: 2 4 3
*: 8 3
1: 1 8 3
5: 5 1 8 3
-: -4 8 3
2: 2 -4 8 3
3: 3 2 -4 8 3
**: 8 -4 8 3
**: 65536 8 3
/: .00012207031 3
+: 3.0001221</pre>
 
==={{header|VBA}}===
{{trans|Liberty BASIC}}
<syntaxhighlight lang="vba">Global stack$
Function RPN(expr$)
Debug.Print "Expression:"
Debug.Print expr$
Debug.Print "Input", "Operation", "Stack after"
stack$ = ""
token$ = "#"
i = 1
token$ = Split(expr$)(i - 1) 'split is base 0
token2$ = " " + token$ + " "
Do
Debug.Print "Token "; i; ": "; token$,
'operation
If InStr("+-*/^", token$) <> 0 Then
Debug.Print "operate",
op2$ = pop$()
op1$ = pop$()
If op1$ = "" Then
Debug.Print "Error: stack empty for "; i; "-th token: "; token$
End
End If
op1 = Val(op1$)
op2 = Val(op2$)
Select Case token$
Case "+"
res = CDbl(op1) + CDbl(op2)
Case "-"
res = CDbl(op1) - CDbl(op2)
Case "*"
res = CDbl(op1) * CDbl(op2)
Case "/"
res = CDbl(op1) / CDbl(op2)
Case "^"
res = CDbl(op1) ^ CDbl(op2)
End Select
Call push2(str$(res))
'default:number
Else
Debug.Print "push",
Call push2(token$)
End If
Debug.Print "Stack: "; reverse$(stack$)
i = i + 1
If i > Len(Join(Split(expr, " "), "")) Then
token$ = ""
Else
token$ = Split(expr$)(i - 1) 'base 0
token2$ = " " + token$ + " "
End If
Loop Until token$ = ""
Debug.Print
Debug.Print "Result:"; pop$()
'extra$ = pop$()
If stack <> "" Then
Debug.Print "Error: extra things on a stack: "; stack$
End If
End
End Function
'---------------------------------------
Function reverse$(s$)
reverse$ = ""
token$ = "#"
While token$ <> ""
i = i + 1
token$ = Split(s$, "|")(i - 1) 'split is base 0
reverse$ = token$ & " " & reverse$
Wend
End Function
'---------------------------------------
Sub push2(s$)
stack$ = s$ + "|" + stack$ 'stack
End Sub
Function pop$()
'it does return empty on empty stack
pop$ = Split(stack$, "|")(0)
stack$ = Mid$(stack$, InStr(stack$, "|") + 1)
End Function</syntaxhighlight>
 
{{out}}
<pre>?RPN("3 4 2 * 1 5 - 2 3 ^ ^ / +")
Expression:
3 4 2 * 1 5 - 2 3 ^ ^ / +
Input Operation Stack after
Token 1 : 3 push Stack: 3
Token 2 : 4 push Stack: 3 4
Token 3 : 2 push Stack: 3 4 2
Token 4 : * operate Stack: 3 8
Token 5 : 1 push Stack: 3 8 1
Token 6 : 5 push Stack: 3 8 1 5
Token 7 : - operate Stack: 3 8 -4
Token 8 : 2 push Stack: 3 8 -4 2
Token 9 : 3 push Stack: 3 8 -4 2 3
Token 10 : ^ operate Stack: 3 8 -4 8
Token 11 : ^ operate Stack: 3 8 65536
Token 12 : / operate Stack: 3 .0001220703125
Token 13 : + operate Stack: 3.0001220703125
 
Result: 3.0001220703125</pre>
 
==={{header|Xojo}}===
{{trans|VBA}}
<syntaxhighlight lang="xojo">
Function RPN(expr As String) As String
Dim tokenArray() As String
Dim stack() As String
Dim Wert1 As Double
Dim Wert2 As Double
'Initialize array (removed later)
ReDim tokenArray(1)
ReDim stack(1)
tokenArray = Split(expr, " ")
Dim i As integer
i = 0
 
While i <= tokenArray.Ubound
If tokenArray(i) = "+" Then
Wert2 = Val(stack.pop)
Wert1 = Val(stack.pop)
stack.Append(Str(Wert1+Wert2))
ElseIf tokenArray(i) = "-" Then
Wert2 = Val(stack.pop)
Wert1 = Val(stack.pop)
stack.Append(Str(Wert1-Wert2))
ElseIf tokenArray(i) = "*" Then
Wert2 = Val(stack.pop)
Wert1 = Val(stack.pop)
stack.Append(Str(Wert1*Wert2))
ElseIf tokenArray(i) = "/" Then
Wert2 = Val(stack.pop)
Wert1 = Val(stack.pop)
stack.Append(Str(Wert1/Wert2))
ElseIf tokenArray(i) = "^" Then
Wert2 = Val(stack.pop)
Wert1 = Val(stack.pop)
stack.Append(Str(pow(Wert1,Wert2)))
Else
stack.Append(tokenArray(i))
End If
i = i +1
Wend
Return stack(2)
End Function</syntaxhighlight>
 
 
 
{{out}}
<pre>?RPN("3 4 2 * 1 5 - 2 3 ^ ^ / +")
Expression:
3 4 2 * 1 5 - 2 3 ^ ^ / +
 
Input Operation Stack after
Token 1 : 3 push Stack: 3
Token 2 : 4 push Stack: 3 4
Token 3 : 2 push Stack: 3 4 2
Token 4 : * operate Stack: 3 8
Token 5 : 1 push Stack: 3 8 1
Token 6 : 5 push Stack: 3 8 1 5
Token 7 : - operate Stack: 3 8 -4
Token 8 : 2 push Stack: 3 8 -4 2
Token 9 : 3 push Stack: 3 8 -4 2 3
Token 10 : ^ operate Stack: 3 8 -4 8
Token 11 : ^ operate Stack: 3 8 65536
Token 12 : / operate Stack: 3 .000122
Token 13 : + operate Stack: 3.000122
 
Result: 3.000122</pre>
 
=={{header|Bracmat}}==
Line 1,677 ⟶ 2,420:
 
The final value is 3.00012</pre>
 
=={{header|Delphi}}==
{{works with|Delphi|6.0}}
{{libheader|SysUtils,StdCtrls}}
This is a good example of creating s simple object to create a stakc for use in parsing the data.
 
<syntaxhighlight lang="Delphi">
{This code normally exists in a library, but is presented here for clarity}
 
function ExtractToken(S: string; Sep: char; var P: integer): string;
{Extract token from S, starting at P up to but not including Sep}
{Terminates with P pointing past Sep or past end of string}
var C: char;
begin
Result:='';
while P<=Length(S) do
begin
C:=S[P]; Inc(P);
if C=Sep then break
else Result:=Result+C;
end;
end;
 
{Create stack object to handle parsing}
 
type TRealStack = class(TObject)
private
Data: array of double;
protected
public
function GetStackStr: string;
procedure Push(D: double);
function Pop: double;
end;
 
procedure TRealStack.Push(D: double);
{Push double on stack}
begin
SetLength(Data,Length(Data)+1);
Data[High(Data)]:=D;
end;
 
 
function TRealStack.Pop: double;
{Pop double off stack, raises exception if stack empty}
begin
if Length(Data)<1 then raise exception.Create('Stack Empty');
Result:=Data[High(Data)];
SetLength(Data,Length(Data)-1);
end;
 
 
function TRealStack.GetStackStr: string;
{Get string representation of stack data}
var I: integer;
begin
Result:='';
for I:=0 to High(Data) do
begin
if I<>0 then Result:=Result+', ';
Result:=Result+FloatToStrF(Data[I],ffGeneral,18,4);
end;
end;
 
 
 
procedure RPNParser(Memo: TMemo; S: string);
{Parse RPN string and display all operations}
var I: integer;
var Stack: TRealStack;
var Token: string;
var D: double;
 
 
function HandleOperator(S: string): boolean;
{Handle numerical operator command}
var Arg1,Arg2: double;
begin
Result:=False;
{Empty comand string? }
if Length(S)>1 then exit;
{Invalid command? }
if not (S[1] in ['+','-','*','/','^']) then exit;
{Get arguments off stack}
Arg1:=Stack.Pop; Arg2:=Stack.Pop;
Result:=True;
{Decode command}
case S[1] of
'+': Stack.Push(Arg2 + Arg1);
'-': Stack.Push(Arg2 - Arg1);
'*': Stack.Push(Arg2 * Arg1);
'/': Stack.Push(Arg2 / Arg1);
'^': Stack.Push(Power(Arg2,Arg1));
else Result:=False;
end;
end;
 
 
begin
Stack:=TRealStack.Create;
try
I:=1;
while true do
begin
{Extract one token from string}
Token:=ExtractToken(S,' ',I);
{Exit if no more data}
if Token='' then break;
{If token is a number convert it to a double otherwise, process an operator}
if Token[1] in ['0'..'9'] then Stack.Push(StrToFloat(Token))
else if not HandleOperator(Token) then raise Exception.Create('Illegal Token: '+Token);
Memo.Lines.Add(Token+' ['+Stack.GetStackStr+']');
end;
finally Stack.Free; end;
end;
 
 
procedure ShowRPNParser(Memo: TMemo);
var S: string;
begin
S:='3 4 2 * 1 5 - 2 3 ^ ^ / + ';
RPNParser(Memo,S);
end;
 
 
</syntaxhighlight>
{{out}}
<pre>
3 [3]
4 [3, 4]
2 [3, 4, 2]
* [3, 8]
1 [3, 8, 1]
5 [3, 8, 1, 5]
- [3, 8, -4]
2 [3, 8, -4, 2]
3 [3, 8, -4, 2, 3]
^ [3, 8, -4, 8]
^ [3, 8, 65536]
/ [3, 0.0001220703125]
+ [3.0001220703125]
Elapsed Time: 16.409 ms.
 
</pre>
 
 
=={{header|EchoLisp}}==
Line 2,042 ⟶ 2,930:
25 + XEQ 1: 3.000122
Result is... 3.000122</pre>
 
=={{header|FreeBASIC}}==
<syntaxhighlight lang="freebasic">#define NULL 0
 
type node
'implement the stack as a linked list
n as double
p as node ptr
end type
 
function spctok( byref s as string ) as string
'returns everything in the string up to the first space
'modifies the original string to begin at the fist non-space char after the first space
dim as string r
dim as double i = 1
while mid(s,i,1)<>" " and i<=len(s)
r += mid(s,i,1)
i+=1
wend
do
i+=1
loop until mid(s,i,1)<>" " or i >= len(s)
s = right(s,len(s)-i+1)
return r
end function
 
sub print_stack( byval S as node ptr )
'display everything on the stack
print "Stack <--- ";
while S->p <> NULL
S = S->p
print S->n;" ";
wend
print
end sub
 
sub push( byval S as node ptr, v as double )
'push a value onto the stack
dim as node ptr x
x = allocate(sizeof(node))
x->n = v
x->p = S->p
S->p = x
end sub
 
function pop( byval S as node ptr ) as double
'pop a value from the stack
if s->P = NULL then return -99999
dim as double r = S->p->n
dim as node ptr junk = S->p
S->p = S->p->p
deallocate(junk)
return r
end function
 
dim as string s = "3 4 2 * 1 5 - 2 3 ^ ^ / +", c
dim as node StackHead
 
while len(s) > 0
c = spctok(s)
print "Token: ";c;" ";
select case c
case "+"
push(@StackHead, pop(@StackHead) + pop(@StackHead))
print "Operation + ";
case "-"
push(@StackHead, -(pop(@StackHead) - pop(@StackHead)))
print "Operation - ";
case "/"
push(@StackHead, 1./(pop(@StackHead) / pop(@StackHead)))
print "Operation / ";
case "*"
push(@StackHead, pop(@StackHead) * pop(@StackHead))
print "Operation * ";
case "^"
push(@StackHead, pop(@StackHead) ^ pop(@StackHead))
print "Operation ^ ";
case else
push(@StackHead, val(c))
print "Operation push ";
end select
print_stack(@StackHead)
wend</syntaxhighlight>
{{out}}<pre>
Token: 3 Operation push Stack <--- 3
Token: 4 Operation push Stack <--- 4 3
Token: 2 Operation push Stack <--- 2 4 3
Token: * Operation * Stack <--- 8 3
Token: 1 Operation push Stack <--- 1 8 3
Token: 5 Operation push Stack <--- 5 1 8 3
Token: - Operation - Stack <--- -4 8 3
Token: 2 Operation push Stack <--- 2 -4 8 3
Token: 3 Operation push Stack <--- 3 2 -4 8 3
Token: ^ Operation ^ Stack <--- 8 -4 8 3
Token: ^ Operation ^ Stack <--- 65536 8 3
Token: / Operation / Stack <--- 0.0001220703125 3
Token: + Operation + Stack <--- 3.0001220703125
</pre>
 
=={{header|FunL}}==
Line 2,695 ⟶ 3,485:
if length<=1 then .
elif op == "+" then update(two | add)
elif op == "/" then update(two | (.[0] / .[1]))
elif op == "*" then update(two | (.[0] * .[1]))
elif op == "/" then update(two | (.[0] / .[1]))
Line 2,731 ⟶ 3,520:
+ => [3.0001220703125]
</pre>
 
 
=={{header|Julia}}==
Line 2,913 ⟶ 3,701:
- and the "or" boolean function.
</syntaxhighlight>
 
=={{header|Liberty BASIC}}==
<syntaxhighlight lang="lb">
global stack$
 
expr$ = "3 4 2 * 1 5 - 2 3 ^ ^ / +"
print "Expression:"
print expr$
print
 
print "Input","Operation","Stack after"
 
stack$=""
token$ = "#"
i = 1
token$ = word$(expr$, i)
token2$ = " "+token$+" "
 
do
print "Token ";i;": ";token$,
select case
'operation
case instr("+-*/^",token$)<>0
print "operate",
op2$=pop$()
op1$=pop$()
if op1$="" then
print "Error: stack empty for ";i;"-th token: ";token$
end
end if
 
op1=val(op1$)
op2=val(op2$)
 
select case token$
case "+"
res = op1+op2
case "-"
res = op1-op2
case "*"
res = op1*op2
case "/"
res = op1/op2
case "^"
res = op1^op2
end select
 
call push str$(res)
'default:number
case else
print "push",
call push token$
end select
print "Stack: ";reverse$(stack$)
i = i+1
token$ = word$(expr$, i)
token2$ = " "+token$+" "
loop until token$ =""
 
res$=pop$()
print
print "Result:" ;res$
extra$=pop$()
if extra$<>"" then
print "Error: extra things on a stack: ";extra$
end if
end
 
'---------------------------------------
function reverse$(s$)
reverse$ = ""
token$="#"
while token$<>""
i=i+1
token$=word$(s$,i,"|")
reverse$ = token$;" ";reverse$
wend
end function
'---------------------------------------
sub push s$
stack$=s$+"|"+stack$ 'stack
end sub
 
function pop$()
'it does return empty on empty stack
pop$=word$(stack$,1,"|")
stack$=mid$(stack$,instr(stack$,"|")+1)
end function
</syntaxhighlight>
 
{{out}}
<pre>
Expression:
3 4 2 * 1 5 - 2 3 ^ ^ / +
 
Input Operation Stack after
Token 1: 3 push Stack: 3
Token 2: 4 push Stack: 3 4
Token 3: 2 push Stack: 3 4 2
Token 4: * operate Stack: 3 8
Token 5: 1 push Stack: 3 8 1
Token 6: 5 push Stack: 3 8 1 5
Token 7: - operate Stack: 3 8 -4
Token 8: 2 push Stack: 3 8 -4 2
Token 9: 3 push Stack: 3 8 -4 2 3
Token 10: ^ operate Stack: 3 8 -4 8
Token 11: ^ operate Stack: 3 8 65536
Token 12: / operate Stack: 3 0.12207031e-3
Token 13: + operate Stack: 3.00012207
 
Result:3.00012207
</pre>
 
=={{header|Lua}}==
Line 5,043 ⟶ 5,719:
'''output''' &nbsp; is identical to the 2<sup>nd</sup> REXX version.
<br><br>
 
=={{header|RPL}}==
'''Straightforward '''
"3 4 2 * 1 5 - 2 3 ^ ^ / +" STR→
{{out}}
<pre>
1: 3.00012207031
</pre>
 
'''Step-by-step'''
 
<code>LEXER</code> is defined at [[Parsing/Shunting-yard algorithm#RPL|Parsing/Shunting-yard algorithm]]
{{works with|Halcyon Calc|4.2.7}}
≪ <span style="color:blue">LEXER</span> "" { } 0 → postfix token steps depth
≪ 1 postfix SIZE '''FOR''' j
postfix j GET 'token' STO
'''IF''' token TYPE '''THEN'''
"≪" token + "≫" + STR→ EVAL
depth 1 - ‘depth’ STO
'''ELSE'''
token
depth 1 + ‘depth’ STO
'''END'''
depth DUPN depth →LIST
steps "Token " token →STR + " → " + ROT →STR +
+ ‘steps’ STO
'''NEXT''' steps
≫ ≫ '<span style="color:blue">CALC</span>' STO
 
"3 4 2 * 1 5 - 2 3 ^ ^ / +" <span style="color:blue">CALC</span>
{{out}}
<pre>
2: 3.00012207031
1: { "Token 3 → { 3 }"
"Token 4 → { 3 4 }"
"Token 2 → { 3 4 2 }"
"Token * → { 3 8 }"
"Token 1 → { 3 8 1 }"
"Token 5 → { 3 8 1 5 }"
"Token - → { 3 8 -4 }"
"Token 2 → { 3 8 -4 2 }"
"Token 3 → { 3 8 -4 2 3 }"
"Token ^ → { 3 8 -4 8 }"
"Token ^ → { 3 8 65536 }"
"Token / → { 3 1.220703125E-04 }"
"Token + → { 3.00012207031 }" }
</pre>
Additional spaces and CR characters have been added to the above output to enhance readability.
 
=={{header|Ruby}}==
Line 5,065 ⟶ 5,789:
+ ADD [3.0001220703125]
Value = 3.0001220703125</pre>
 
=={{header|Run BASIC}}==
<syntaxhighlight lang="runbasic">prn$ = "3 4 2 * 1 5 - 2 3 ^ ^ / + "
 
j = 0
while word$(prn$,i + 1," ") <> ""
i = i + 1
n$ = word$(prn$,i," ")
if n$ < "0" or n$ > "9" then
num1 = val(word$(stack$,s," "))
num2 = val(word$(stack$,s-1," "))
n = op(n$,num2,num1)
s = s - 1
stack$ = stk$(stack$,s -1,str$(n))
print "Push Opr ";n$;" to stack: ";stack$
else
s = s + 1
stack$ = stack$ + n$ + " "
print "Push Num ";n$;" to stack: ";stack$
end if
wend
 
function stk$(stack$,s,a$)
for i = 1 to s
stk$ = stk$ + word$(stack$,i," ") + " "
next i
stk$ = stk$ + a$ + " "
end function
 
FUNCTION op(op$,a,b)
if op$ = "*" then op = a * b
if op$ = "/" then op = a / b
if op$ = "^" then op = a ^ b
if op$ = "+" then op = a + b
if op$ = "-" then op = a - b
end function</syntaxhighlight>
<pre>Push Num 3 to stack: 3
Push Num 4 to stack: 3 4
Push Num 2 to stack: 3 4 2
Push Opr * to stack: 3 8
Push Num 1 to stack: 3 8 1
Push Num 5 to stack: 3 8 1 5
Push Opr - to stack: 3 8 -4
Push Num 2 to stack: 3 8 -4 2
Push Num 3 to stack: 3 8 -4 2 3
Push Opr ^ to stack: 3 8 -4 8
Push Opr ^ to stack: 3 8 65536
Push Opr / to stack: 3 1.22070312e-4
Push Opr + to stack: 3.00012207</pre>
 
=={{header|Rust}}==
Line 5,299 ⟶ 5,974:
3.0001220703125
</pre>
 
=={{header|Sinclair ZX81 BASIC}}==
If you only have 1k of RAM, this program will correctly evaluate the test expression with fewer than 10 bytes to spare. (I know that because I tried running it with the first line modified to allow a stack depth of 7, i.e. allocating space for two more 40-bit floats, and it crashed with an "out of memory" error code before it could print the result of the final addition.) If we desperately needed a few extra bytes there are ways they could be shaved out of the current program; but this version works, and editing a program that takes up almost all your available RAM isn't very comfortable, and to make it really useful for practical purposes you would still want to have 2k or more anyway.
 
The ZX81 character set doesn't include <code>^</code>, so we have to use <code>**</code> instead. Note that this is not two separate stars, although that's what it looks like: you have to enter it by typing <code>SHIFT</code>+<code>H</code>.
 
No attempt is made to check for invalid syntax, stack overflow or underflow, etc.
 
<syntaxhighlight lang="basic"> 10 DIM S(5)
20 LET P=1
30 INPUT E$
40 LET I=0
50 LET I=I+1
60 IF E$(I)=" " THEN GOTO 110
70 IF I<LEN E$ THEN GOTO 50
80 LET W$=E$
90 GOSUB 150
100 STOP
110 LET W$=E$( TO I-1)
120 LET E$=E$(I+1 TO )
130 GOSUB 150
140 GOTO 40
150 IF W$="+" OR W$="-" OR W$="*" OR W$="/" OR W$="**" THEN GOTO 250
160 LET S(P)=VAL W$
170 LET P=P+1
180 PRINT W$;
190 PRINT ":";
200 FOR I=P-1 TO 1 STEP -1
210 PRINT " ";S(I);
220 NEXT I
230 PRINT
240 RETURN
250 IF W$="**" THEN LET S(P-2)=ABS S(P-2)
260 LET S(P-2)=VAL (STR$ S(P-2)+W$+STR$ S(P-1))
270 LET P=P-1
280 GOTO 180</syntaxhighlight>
{{in}}
<pre>3 4 2 * 1 5 - 2 3 ** ** / +</pre>
{{out}}
<pre>3: 3
4: 4 3
2: 2 4 3
*: 8 3
1: 1 8 3
5: 5 1 8 3
-: -4 8 3
2: 2 -4 8 3
3: 3 2 -4 8 3
**: 8 -4 8 3
**: 65536 8 3
/: .00012207031 3
+: 3.0001221</pre>
 
=={{header|Swift}}==
Line 5,569 ⟶ 6,192:
+ : 3
3</syntaxhighlight>
 
=={{header|VBA}}==
 
{{trans|Liberty BASIC}}
 
<syntaxhighlight lang="vba">Global stack$
Function RPN(expr$)
Debug.Print "Expression:"
Debug.Print expr$
Debug.Print "Input", "Operation", "Stack after"
stack$ = ""
token$ = "#"
i = 1
token$ = Split(expr$)(i - 1) 'split is base 0
token2$ = " " + token$ + " "
Do
Debug.Print "Token "; i; ": "; token$,
'operation
If InStr("+-*/^", token$) <> 0 Then
Debug.Print "operate",
op2$ = pop$()
op1$ = pop$()
If op1$ = "" Then
Debug.Print "Error: stack empty for "; i; "-th token: "; token$
End
End If
op1 = Val(op1$)
op2 = Val(op2$)
Select Case token$
Case "+"
res = CDbl(op1) + CDbl(op2)
Case "-"
res = CDbl(op1) - CDbl(op2)
Case "*"
res = CDbl(op1) * CDbl(op2)
Case "/"
res = CDbl(op1) / CDbl(op2)
Case "^"
res = CDbl(op1) ^ CDbl(op2)
End Select
Call push2(str$(res))
'default:number
Else
Debug.Print "push",
Call push2(token$)
End If
Debug.Print "Stack: "; reverse$(stack$)
i = i + 1
If i > Len(Join(Split(expr, " "), "")) Then
token$ = ""
Else
token$ = Split(expr$)(i - 1) 'base 0
token2$ = " " + token$ + " "
End If
Loop Until token$ = ""
Debug.Print
Debug.Print "Result:"; pop$()
'extra$ = pop$()
If stack <> "" Then
Debug.Print "Error: extra things on a stack: "; stack$
End If
End
End Function
'---------------------------------------
Function reverse$(s$)
reverse$ = ""
token$ = "#"
While token$ <> ""
i = i + 1
token$ = Split(s$, "|")(i - 1) 'split is base 0
reverse$ = token$ & " " & reverse$
Wend
End Function
'---------------------------------------
Sub push2(s$)
stack$ = s$ + "|" + stack$ 'stack
End Sub
Function pop$()
'it does return empty on empty stack
pop$ = Split(stack$, "|")(0)
stack$ = Mid$(stack$, InStr(stack$, "|") + 1)
End Function</syntaxhighlight>
 
{{out}}
<pre>?RPN("3 4 2 * 1 5 - 2 3 ^ ^ / +")
Expression:
3 4 2 * 1 5 - 2 3 ^ ^ / +
Input Operation Stack after
Token 1 : 3 push Stack: 3
Token 2 : 4 push Stack: 3 4
Token 3 : 2 push Stack: 3 4 2
Token 4 : * operate Stack: 3 8
Token 5 : 1 push Stack: 3 8 1
Token 6 : 5 push Stack: 3 8 1 5
Token 7 : - operate Stack: 3 8 -4
Token 8 : 2 push Stack: 3 8 -4 2
Token 9 : 3 push Stack: 3 8 -4 2 3
Token 10 : ^ operate Stack: 3 8 -4 8
Token 11 : ^ operate Stack: 3 8 65536
Token 12 : / operate Stack: 3 .0001220703125
Token 13 : + operate Stack: 3.0001220703125
 
Result: 3.0001220703125</pre>
 
=={{header|V (Vlang)}}==
Line 5,797 ⟶ 6,308:
{{trans|Kotlin}}
{{libheader|Wren-seq}}
<syntaxhighlight lang="ecmascriptwren">import "./seq" for Stack
 
var rpnCalculate = Fn.new { |expr|
Line 5,852 ⟶ 6,363:
</pre>
 
=={{header|XojoXPL0}}==
<syntaxhighlight lang "XPL0">real Stack(10);
 
int SP;
{{trans|VBA}}
 
<syntaxhighlight lang="xojo">
Function RPN(expr As String) As String
 
Dim tokenArray() As String
Dim stack() As String
Dim Wert1 As Double
Dim Wert2 As Double
'Initialize array (removed later)
ReDim tokenArray(1)
ReDim stack(1)
tokenArray = Split(expr, " ")
Dim i As integer
i = 0
While i <= tokenArray.Ubound
If tokenArray(i) = "+" Then
Wert2 = Val(stack.pop)
Wert1 = Val(stack.pop)
stack.Append(Str(Wert1+Wert2))
ElseIf tokenArray(i) = "-" Then
Wert2 = Val(stack.pop)
Wert1 = Val(stack.pop)
stack.Append(Str(Wert1-Wert2))
ElseIf tokenArray(i) = "*" Then
Wert2 = Val(stack.pop)
Wert1 = Val(stack.pop)
stack.Append(Str(Wert1*Wert2))
ElseIf tokenArray(i) = "/" Then
Wert2 = Val(stack.pop)
Wert1 = Val(stack.pop)
stack.Append(Str(Wert1/Wert2))
ElseIf tokenArray(i) = "^" Then
Wert2 = Val(stack.pop)
Wert1 = Val(stack.pop)
stack.Append(Str(pow(Wert1,Wert2)))
Else
stack.Append(tokenArray(i))
End If
i = i +1
Wend
Return stack(2)
End Function</syntaxhighlight>
 
proc Push(X);
real X;
[Stack(SP):= X; SP:= SP+1];
 
func real Pop;
[SP:= SP-1; return Stack(SP)];
 
char Str; real Top; int Token, I;
[Str:= "3 4 2 * 1 5 - 2 3 ^^ ^^ / + ";
SP:= 0;
Format(6, 8);
loop [repeat Token:= Str(0); Str:= Str+1;
until Token # ^ ; \skip space characters
case Token of
^+: [Top:= Pop; Push(Pop+Top)];
^-: [Top:= Pop; Push(Pop-Top)];
^*: [Top:= Pop; Push(Pop*Top)];
^/: [Top:= Pop; Push(Pop/Top)];
^^: [Top:= Pop; Push(Pow(Pop, Top))];
$A0: quit \space with MSB set
other [Push(float(Token-^0))]; \single digit number
ChOut(0, Token);
for I:= 0 to SP-1 do \show stack
RlOut(0, Stack(I));
CrLf(0);
];
]</syntaxhighlight>
{{out}}
<pre>
<pre>?RPN("3 4 2 * 1 5 - 2 3 ^ ^ / +")
3 3.00000000
Expression:
3 4 2 * 1 5 -3.00000000 2 3 ^ ^ / +4.00000000
2 3.00000000 4.00000000 2.00000000
 
Input* 3.00000000 Operation Stack after8.00000000
Token 1 : 3 push 3.00000000 8.00000000 Stack: 3 1.00000000
Token5 2 : 4 3.00000000 push 8.00000000 1.00000000 Stack: 3 4 5.00000000
Token- 3 : 2 3.00000000 push 8.00000000 Stack: 3 -4 2 .00000000
Token2 4 : * 3.00000000 operate 8.00000000 Stack: -4.00000000 3 8 2.00000000
Token3 5 : 1 3.00000000 push 8.00000000 -4.00000000 Stack: 3 2.00000000 8 1 3.00000000
Token^ 6 : 5 3.00000000 push 8.00000000 -4.00000000 Stack: 3 8 1 5 .00000000
^ 3.00000000 8.00000000 65536.00000000
Token 7 : - operate Stack: 3 8 -4
/ 3.00000000 0.00012207
Token 8 : 2 push Stack: 3 8 -4 2
+ 3.00012207
Token 9 : 3 push Stack: 3 8 -4 2 3
</pre>
Token 10 : ^ operate Stack: 3 8 -4 8
Token 11 : ^ operate Stack: 3 8 65536
Token 12 : / operate Stack: 3 .000122
Token 13 : + operate Stack: 3.000122
 
Result: 3.000122</pre>
 
=={{header|zkl}}==
9,482

edits