Full Site Version
Imports System
Public Module Module1
Public Class Stack
Private _arr(100) As Integer
Private _sp As Integer
Public Sub New()
_sp = -1
End Sub
Public Function Pop()
Dim temp = _arr(_sp)
_sp -= 1
Return temp
End Function
Public Sub Push(item)
_sp += 1
_arr(_sp) = item
End Class
Public Function ApplyOperation(operand1, operand2, operation)
Select Case operation
Case "+" : Return operand1 + operand2
Case "-" : Return operand1 - operand2
Case "/" : Return operand1 / operand2
Case Else : Return operand1 * operand2
End Select
Sub Main()
Dim Stack = New Stack()
Dim Expression As String
Dim postfix() As String
Dim value, operand1, operand2 As Integer
Console.Write("Enter postfix expression: ")
Expression = Console.ReadLine()
postfix = Expression.Split(" ")
For i = 0 To postfix.Length - 1
If Integer.TryParse(postfix(i), value) Then
Stack.Push(value)
Else
operand2 = Stack.Pop()
operand1 = Stack.Pop()
value = ApplyOperation(operand1, operand2, postfix(i))
End If
Next
Console.WriteLine(Stack.Pop())
End Module