Feeling a bit drunk so Thought I whip out my old vb compiler not used it in years any way here is a lame RPN calulator I made have fun.
Code:
Private stack(0 To 100) As Double
Private sk_size As Integer
Private Sub sk_push(value As Double)
stack(sk_size) = value
sk_size = (sk_size + 1)
End Sub
Private Function sk_pop() As Double
Dim d As Double
sk_size = (sk_size - 1)
sk_pop = stack(sk_size)
End Function
Private Function rpn(expression As String) As Double
Dim s() As String
Dim tok As String
Dim n As Double
Dim lval, rval As Double
Dim i As Integer
s = Split(expression, " ")
For i = 0 To UBound(s)
'Get token
tok = Trim$(s(i))
If Len(tok) > 0 Then
If (IsNumeric(tok)) Then
Call sk_push(Val(tok))
Else
Select Case UCase(tok)
Case "+"
lval = sk_pop()
rval = sk_pop()
Call sk_push(rval + lval)
Case "-"
lval = sk_pop()
rval = sk_pop()
Call sk_push(rval - lval)
Case "*"
lval = sk_pop()
rval = sk_pop()
Call sk_push(rval * lval)
Case "/"
lval = sk_pop()
rval = sk_pop()
Call sk_push(rval / lval)
Case "%"
lval = sk_pop
rval = sk_pop()
Call sk_push(rval Mod lval)
Case "SIN"
lval = sk_pop()
Call sk_push(Sin(lval))
Case "COS"
lval = sk_pop()
Call sk_push(Cos(lval))
Case "SQR"
lval = sk_pop()
Call sk_push(lval * lval)
Case "LOG"
lval = sk_pop()
Call sk_push(Log(lval))
Case "INT"
lval = sk_pop()
Call sk_push(Int(lval))
End Select
End If
End If
Next i
rpn = sk_pop
End Function
Private Sub Command1_Click()
Dim ex As String
ex = "1 20 * 20 *"
Call MsgBox(rpn(ex))
ex = "5 2 /"
Call MsgBox(rpn(ex))
ex = "5 2 / int"
Call MsgBox(rpn(ex))
End Sub
Private Sub Form_Load()
sk_size = 0
End Sub