Skip to main content

Evaluate postfix expression using stack in

Vb program for Evaluate postfix expression using stack. Here problem description and other solutions.

' Include namespace system
Imports System 
' program for 
'  Evaluate postfix expression

'  Stack Node
Public Class StackNode
    Public  operand As Integer
    Public  [next] As StackNode
    Public Sub New(ByVal operand As Integer, 
                   ByVal top As StackNode)
        Me.operand = operand = top
    End Sub
End Class

Public Class MyStack
    Public  top As StackNode
    Public  count As Integer
    Public Sub New() = Nothing
        Me.count = 0
    End Sub
    '  Returns the number of element in stack
    Public Function  isSize() As Integer
        Return  Me.count
    End Function
    Public Function  isEmpty() As Boolean
        if (Me.isSize() > 0) Then
            Return  False
            Return  True
        End IF
    End Function
    '  Add a new element in stack
    Public Sub push(ByVal value As Integer)
        '  Make a new stack node
        '  And set as top = New StackNode(value,
        '  Increase node value
        Me.count += 1
    End Sub
    '  Remove a top element
    Public Function  pop() As Integer
        Dim value As Integer = Me.peek()
        if (Me.isEmpty() = False) Then
            '  Reduce the size
            Me.count -= 1
        End If
        Return  value
    End Function
    '  Used to get top element of stack
    Public Function  peek() As Integer
        if (Not Me.isEmpty()) Then
            Return  0
        End IF
    End Function
End Class
public Class Execution
    Public Sub evaluate(ByVal e As String)
        '  Get the length of expression
        Dim size As Integer = e.Length
        Dim a As Integer = 0
        Dim b As Integer = 0
        Dim s As MyStack = New MyStack()
        Dim isVaild As Boolean = True
        '  work with (+,-,/,*,%) operator
        Dim i As Integer = 0
        While i < size AndAlso isVaild
            if (e(i) >= "0"c AndAlso e(i) <= "9"c) Then
                a = Int(e(i)) - 48
            ElseIf (s.isSize() > 1) Then
                a = s.pop()
                b = s.pop()
                '  Perform arithmetic operations between 2 operands
                if (e(i) = "+"c) Then
                    s.push(b + a)
                ElseIf (e(i) = "-"c) Then
                    s.push(b - a)
                ElseIf (e(i) = "*"c) Then
                    s.push(b * a)
                ElseIf (e(i) = "/"c) Then
                    s.push((b / a))
                ElseIf (e(i) = "%"c) Then
                    s.push(b Mod a)
                    '  When use other operator
                    isVaild = False
                End IF
            ElseIf (s.isSize() = 1) Then
                '  Special case  
                '  When use +, - at the beginning
                if (e(i) = "-"c) Then
                    a = s.pop()
                ElseIf (e(i) <> "+"c) Then
                    '  When not use  +,-
                    isVaild = False
                End If
                isVaild = False
            End IF
        i += 1
        End While
        if (isVaild = False) Then
            '  Possible case use other operators
            '  1) When using special operators
            '  2) Or expression is invalid
            Console.WriteLine(e + " Invalid expression ")
        End If
        Console.WriteLine(e + " = " + s.pop().ToString())
    End Sub
    Public Shared Sub Main(ByVal args As String())
        Dim task As Execution = New Execution()
        '  Execute the following expression
        '  (1+2)*(3+5)
        '  -((7*2) + (6/2) + 4)
    End Sub
End Class


12+35+* = 24
72*62/+4+- = -21


Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment