Hi this is a pice of code I had many years in for VB6 and have converted I wanted to make some simple games in VB.NET like times table testers and adding number but needed something to work out the answers. So I managed to convert it over to VB.NET I also wrapped it up in to a nice and easy class.
At the moment it does not support brackets but I try and add this thought it was not really important for it's indented purpose it also supports variables and has two build in ones PI and E
Anyway I left this code here in the hope it be of some use to someone example included.
To use add new class to your project and call it Eval32 Next add the class code below:
vbnet Code:
Public Class Eval32
'Used to hold expression.
Private mSource As String
'Used for store variables.
Public Vars As New Dictionary(Of String, Object)
Public ReadOnly Property Result As Object
Get
'Return value
Result = Eval(mSource)
End Get
End Property
Public Property Expression As String
Get
'Return expression.
Expression = mSource
End Get
Set(ByVal value As String)
'Store expression.
mSource = value
End Set
End Property
Public Sub New()
'Init variables.
Call Vars.Add("PI", 3.14159265)
Call Vars.Add("E", 2.71828183)
End Sub
Private Function IsOp(ByVal c As Char) As Boolean
'Return true if operator
IsOp = InStr("+-*/\^", c)
End Function
Private Function NextToken(ByVal Source As String, ByRef CharPos As Integer) As Object
Dim c As Char = vbNullChar
Dim VarName As String = vbNullString
Dim vData As Object = 0
'This functions returns the next token.
Do
'Read char
c = Source(CharPos - 1)
'Check for operators.
If IsOp(c) Then
Exit Do
Else
'Build token.
vData = vData & c
'INC char position.
CharPos += 1
End If
Loop Until (CharPos > Source.Length)
If vData.ToString.StartsWith("0") Then
'Remove leading zero
vData = vData.ToString.Remove(0, 1)
End If
'Check for numeric value
If IsNumeric(vData) Then
'Return numeric value.
NextToken = Val(vData)
Else
'Must be a variable
'Get variable name
VarName = vData.ToString.Trim()
Try
'Return variable data.
NextToken = Vars(VarName.ToUpper())
Catch ex As Exception
'Error return 0
NextToken = 0
End Try
End If
End Function
Private Function Eval(ByVal Source As String) As Object
Dim Counter As Integer = 1
Dim Value As Object = 0
Dim c As Char = vbNullChar
Dim vOperator As String = vbNullString
Do While (Counter <= Source.Length)
'Read char
c = Source(Counter - 1)
'Check for operators.
If IsOp(c) Then
vOperator = c
Counter += 1
Else
'Process the maths.
Select Case vOperator
Case vbNullString
'Get next token.
Value = NextToken(Source, Counter)
Case "+"
Value = Value + NextToken(Source, Counter)
Case "-"
Value = Value - NextToken(Source, Counter)
Case "*"
Value = Value * NextToken(Source, Counter)
Case "\"
Value = Value \ NextToken(Source, Counter)
Case "/"
Value = Value / NextToken(Source, Counter)
Case "^"
Value = Value ^ NextToken(Source, Counter)
End Select
End If
Loop
'Return value
Eval = Value
End Function
Protected Overrides Sub Finalize()
'Clear up time
mSource = vbNullString
Call Vars.Clear()
MyBase.Finalize()
End Sub
End Class
Here is the example you can use place code below into a buttons click event.
vbnet Code:
Dim MyVal As New Eval32
'Add some variables.
MyVal.Vars.Add("A", 2)
MyVal.Vars.Add("B", 100)
MyVal.Vars.Add("NUM", 1024)
With MyVal
'Example 1
.Expression = "pi"
MsgBox("PI = " & .Result, vbOKOnly Or vbInformation)
'Example 2
.Expression = "A * B"
MsgBox("A*B = " & .Result, vbOKOnly Or vbInformation)
'Example 3
.Expression = "10+10*2+A"
MsgBox("10+10*2+A = " & .Result, vbOKOnly Or vbInformation)
'Example 4
.Expression = "NUM * 2"
MsgBox("NUM * 2 = " & .Result, vbOKOnly Or vbInformation)
'Example 5
.Expression = "E"
MsgBox("E = " & .Result, vbOKOnly Or vbInformation)
End With