TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
 Specialized Dev Zones Research Center eBook Library .NET Java C++ Web Dev Architecture Database Security Open Source Enterprise Mobile Special Reports 10-Minute Solutions DevXtra Blogs Slideshow

 Home » Tip Bank » .NET » VB.NET » Math Processing
Language: VB4,VB5,VB6
Expertise: Intermediate
Oct 6, 2001

### WEBINAR:On-Demand

Building the Right Environment to Support AI, Machine Learning and Deep Learning

# A simple expression evaluator

While any Windows user could pop up the Calculator accessory to perform any type of math calculations, it would be great if you could offer him or her the capability to do simple math from within your application. This is a very simple expression evaluator function that does it:

Function EvalExpression(ByVal expression As String) As Double
Dim result As Double
Dim operand As Double
Dim opcode As String
Dim index As Integer
Dim lastIndex As Integer

' the null character mark the end of the string
expression = expression & vbNullChar

For index = 1 To Len(expression) + 1
If InStr("+-*/" & vbNullChar, Mid\$(expression, index, 1)) Then
If lastIndex = 0 Then
' this is the first operand in the expression
result = Val(Left\$(expression, index - 1))
Else
' extract the new operand
operand = Val(Mid\$(expression, lastIndex, index - lastIndex))
' execute the pending operation
Select Case opcode
Case "+"
result = result + operand
Case "-"
result = result - operand
Case "*"
result = result * operand
Case "/"
result = result / operand
End Select
End If
opcode = Mid\$(expression, index, 1)
lastIndex = index + 1
End If
Next
EvalExpression = LTrim\$(result)

End Function
This evaluator is very simple, and has a number of limitations: it does not account for negative numbers nor for parenthesis and sub-expressions, and operands are evaluated from left to right without any priority rule (e.g. "10+2*3" returns 36, and not 16). Nevertheless it is very handy and lets you make you program friendlier to your customer by adding the capability to perform calculation from within a textbox control. This routine shows how to evaluate the expression in a textbox control when the user presses the F2 key:

Sub Text1_KeyDown (KeyCode As Integer, Shift As Integer)
If KeyCode = vbKeyF2 And Shift = 0 Then
Text1.Text = EvalExpression(Text1.Text)
Text1.SelStart = Len(Text1.Text)
End If
End Sub
If you want to evaluate more complex expressions you can use the MS Script Control library, which offers a simple way to do it. This control has an Eval method that evaluates any expression, with parenthesis, sub-expressions, math functions such as sqr, power, etc., and evaluates the operators in the right order. Add the "Microsoft Script Control" library from the References dialog window, and execute the following code to calculate the result of the given sample expression: Dim oScriptCtl As New ScriptControl Dim sExpr As String

oScriptCtl.Language = "VBScript"
sExpr = "sqr(25)+2^2+((3+7)/5+1)"
MsgBox sExpr & " = " & oScriptCtl.Eval(sExpr)
Francesco Balena

 Submit a Tip Browse ".NET" Tips Browse All Tips
Comment and Contribute

(Maximum characters: 1200). You have 1200 characters left.

Thanks for your registration, follow us on our social networks to keep up-to-date