Click here to Skip to main content
Licence CPOL
First Posted 19 Nov 2007
Views 14,443
Downloads 41
Bookmarked 12 times

Evaluate Arithmetic Expressions using Polish Notations in VB.NET

By | 19 Nov 2007 | Article
This article illustrates how to use Polish notation for evaluate arithmetic expressions.

Introduction

This sample application illustrates how to solve arithmetic expressions using Polish notation (logic for solving arithmetic expressions).

In this source code, I have used a stack and queue and some of their important methods like pop, push etc.

Using the Code

You can use this code in your application. You can create a class, and call the function Evaluate(str) to evaluate the expression. Create a class and copy the following code into the class:

Imports System.Math
Public Class EvaluateArithmeticExp
    '
    'Declaration
    '

    Dim k As Integer = 0
    Dim Stack1 As New Stack() ' Declaring Stacks
    Dim Postfix As New Queue()
    Dim Ch, NextChar As String
    Dim StrInfix As String ' Input String
    Dim Prece As Integer ' For Precendence of Operator
    Dim Infix As String()
    Dim i As Integer
    Dim j As Integer

    Private Function Space(ByVal C As Char) As Boolean
        If C = CType(ChrW(System.Windows.Forms.Keys.Space), Char) _
               Or C = CType(CChar(CStr(System.Windows.Forms.Keys.Tab)), Char) Then
            Return True
        Else
            Return False
        End If
    End Function

    Private Function Precedence(ByVal C As String) As Integer
        If C = "(" Then
            Return 0
        ElseIf C = "+" Or C = "-" Then
            Return 1
        ElseIf C = "*" Or C = "/" Or C = "%" Then
            Return 2
        ElseIf C = "^" Then
            Return 3
        End If
    End Function

    '
    'Function For Convert Input String Into An Array
    '
    Private Sub CreateInfix(ByVal str As String)
        j = 0
        Dim separetor(7) As Char
        Dim c As Integer = 0
        separetor(c) = "+" : c += 1
        separetor(c) = "-" : c += 1
        separetor(c) = "*" : c += 1
        separetor(c) = "/" : c += 1
        separetor(c) = "%" : c += 1
        separetor(c) = "(" : c += 1
        separetor(c) = ")" : c += 1
        separetor(c) = "^"

        Dim posF, posL As Integer
        posF = 0
        posL = 0
        ReDim Infix(str.Length + 5)

        For i = 0 To str.Length - 1
            posL = str.IndexOfAny(separetor, posF)
            If posL > str.Length Or posL < 0 Then
                Exit For
            Else
                Infix(j) = Trim(Mid(str, posF + 1, posL - posF)) : j += 1
                posF = posL + 1
                Infix(j) = Mid(str, posL + 1, 1) : j += 1
            End If
        Next
        Infix(j) = Trim(Mid(str, posF + 1, str.Length))
    End Sub

    '
    'Function to Evaluate Postfix Expression 
    '
    Private Sub Evaluate_Postfix()
        Dim Operands1, Operands2, Result As Long
        Dim Operand_Operators As String
        Try
            For i = 1 To Postfix.Count
                Operand_Operators = Postfix.Dequeue
                If IsNumeric(Operand_Operators) Then
                    Stack1.Push(Operand_Operators)
                Else
                    Operands1 = Stack1.Pop
                    Operands2 = Stack1.Pop
                    If Operand_Operators = "+" Then
                        Result = Operands2 + Operands1
                    ElseIf Operand_Operators = "-" Then
                        Result = Operands2 - Operands1
                    ElseIf Operand_Operators = "*" Then
                        Result = Operands2 * Operands1
                    ElseIf Operand_Operators = "/" Then
                        Result = Operands2 / Operands1
                    ElseIf Operand_Operators = "%" Then
                        DivRem(Operands2, Operands1, Result)
                    ElseIf Operand_Operators = "^" Then
                        Result = Pow(Operands2, Operands1)
                    End If
                    Stack1.Push(Result)
                End If
            Next
        Catch ex As Exception
            MsgBox("Error :" & vbCrLf & vbCrLf & _
                   "Input String Is Not Currect." & vbCrLf & ex.Message)
        End Try
    End Sub

    Public Function Evaluate(ByVal Str As String) As Long
        Postfix.Clear()
        StrInfix = ""
        Stack1.Clear()
        StrInfix = Str
        Call CreateInfix(StrInfix)
        '
        'Convert Infix Expression To Postfix Expression (Login Polish Notation)
        '
        Try
            For i = 1 To Infix.Length
                Ch = Infix(i - 1)
                If Not Space(Ch) Or Not IsDBNull(Ch) Then
                    If System.Char.IsDigit(Ch) Then
                        Postfix.Enqueue(Ch)
                    ElseIf Ch = "(" Then
                        Stack1.Push(Ch)
                    ElseIf Ch = ")" Then
                        Do
                            If Not Stack1.Count = 0 Then
                                NextChar = Stack1.Pop
                                If NextChar = "(" Then
                                    NextChar = ""
                                    Exit Do
                                Else
                                    Postfix.Enqueue(NextChar)
                                End If
                                Else
                                Exit Do
                            End If
                        Loop
                    ElseIf Ch = "+" Or Ch = "-" Or _
                                Ch = "*" Or Ch = "/" Or _
                                Ch = "%" Or Ch = "^" Then
                        Prece = Precedence(Ch)
                        Do
                            If Not Stack1.Count = 0 Then
                                If Prece <= Precedence(Stack1.Peek) Then
                                    Postfix.Enqueue(Stack1.Pop)
                                Else
                                    Exit Do
                                End If
                            Else
                                Exit Do
                            End If
                        Loop
                        Stack1.Push(Ch)
                    End If
                End If
            Next

            For i = 0 To Stack1.Count - 1
                Postfix.Enqueue(Stack1.Pop)
            Next

            Call Evaluate_Postfix()
        Catch ex As Exception
            MsgBox("Error :" & vbCrLf & vbCrLf & _
                   "Input String Is Not Currect." & _
                   vbCrLf & ex.Message)
            Return 0
            Exit Function
        End Try
        Return Stack1.Pop
    End Function
End Class

Points of Interest

In this application, I have used DefWndProc(msg) to move the form.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Suresh Suthar

Software Developer (Senior)
HOC
India India

Member

Still Alive.....

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralGot my 1 PinmemberCristian Amarie3:59 5 Jul '08  
GeneralExcellent -- Thank you! PinmemberBit-Smacker7:22 24 Mar '08  
GeneralRe: Excellent -- Thank you! Pinmembersuresh suthar22:27 24 Mar '08  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web04 | 2.5.120517.1 | Last Updated 19 Nov 2007
Article Copyright 2007 by Suresh Suthar
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid