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
Dim k As Integer = 0
Dim Stack1 As New Stack() Dim Postfix As New Queue()
Dim Ch, NextChar As String
Dim StrInfix As String Dim Prece As Integer 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
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
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)
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.