Results 1 to 1 of 1

Thread: Basic math evaluator

  1. #1

    Thread Starter
    Fanatic Member BenJones's Avatar
    Join Date
    Mar 2010
    Location
    Wales UK
    Posts
    629

    Basic math evaluator

    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:
    1. Public Class Eval32
    2.     'Used to hold expression.
    3.     Private mSource As String
    4.     'Used for store variables.
    5.     Public Vars As New Dictionary(Of String, Object)
    6.  
    7.     Public ReadOnly Property Result As Object
    8.         Get
    9.             'Return value
    10.             Result = Eval(mSource)
    11.         End Get
    12.     End Property
    13.  
    14.     Public Property Expression As String
    15.         Get
    16.             'Return expression.
    17.             Expression = mSource
    18.         End Get
    19.         Set(ByVal value As String)
    20.             'Store expression.
    21.             mSource = value
    22.         End Set
    23.     End Property
    24.  
    25.     Public Sub New()
    26.         'Init variables.
    27.         Call Vars.Add("PI", 3.14159265)
    28.         Call Vars.Add("E", 2.71828183)
    29.     End Sub
    30.  
    31.     Private Function IsOp(ByVal c As Char) As Boolean
    32.         'Return true if operator
    33.         IsOp = InStr("+-*/\^", c)
    34.     End Function
    35.  
    36.     Private Function NextToken(ByVal Source As String, ByRef CharPos As Integer) As Object
    37.         Dim c As Char = vbNullChar
    38.         Dim VarName As String = vbNullString
    39.         Dim vData As Object = 0
    40.  
    41.         'This functions returns the next token.
    42.         Do
    43.             'Read char
    44.             c = Source(CharPos - 1)
    45.  
    46.             'Check for operators.
    47.             If IsOp(c) Then
    48.                 Exit Do
    49.             Else
    50.                 'Build token.
    51.                 vData = vData & c
    52.                 'INC char position.
    53.                 CharPos += 1
    54.             End If
    55.         Loop Until (CharPos > Source.Length)
    56.  
    57.         If vData.ToString.StartsWith("0") Then
    58.             'Remove leading zero
    59.             vData = vData.ToString.Remove(0, 1)
    60.         End If
    61.  
    62.         'Check for numeric value
    63.         If IsNumeric(vData) Then
    64.             'Return numeric value.
    65.             NextToken = Val(vData)
    66.         Else
    67.             'Must be a variable
    68.             'Get variable name
    69.             VarName = vData.ToString.Trim()
    70.             Try
    71.                 'Return variable data.
    72.                 NextToken = Vars(VarName.ToUpper())
    73.             Catch ex As Exception
    74.                 'Error return 0
    75.                 NextToken = 0
    76.             End Try
    77.         End If
    78.     End Function
    79.  
    80.     Private Function Eval(ByVal Source As String) As Object
    81.         Dim Counter As Integer = 1
    82.         Dim Value As Object = 0
    83.         Dim c As Char = vbNullChar
    84.         Dim vOperator As String = vbNullString
    85.  
    86.         Do While (Counter <= Source.Length)
    87.             'Read char
    88.             c = Source(Counter - 1)
    89.  
    90.             'Check for operators.
    91.             If IsOp(c) Then
    92.                 vOperator = c
    93.                 Counter += 1
    94.             Else
    95.                 'Process the maths.
    96.                 Select Case vOperator
    97.                     Case vbNullString
    98.                         'Get next token.
    99.                         Value = NextToken(Source, Counter)
    100.                     Case "+"
    101.                         Value = Value + NextToken(Source, Counter)
    102.                     Case "-"
    103.                         Value = Value - NextToken(Source, Counter)
    104.                     Case "*"
    105.                         Value = Value * NextToken(Source, Counter)
    106.                     Case "\"
    107.                         Value = Value \ NextToken(Source, Counter)
    108.                     Case "/"
    109.                         Value = Value / NextToken(Source, Counter)
    110.                     Case "^"
    111.                         Value = Value ^ NextToken(Source, Counter)
    112.                 End Select
    113.             End If
    114.         Loop
    115.  
    116.         'Return value
    117.         Eval = Value
    118.     End Function
    119.  
    120.     Protected Overrides Sub Finalize()
    121.         'Clear up time
    122.         mSource = vbNullString
    123.         Call Vars.Clear()
    124.  
    125.         MyBase.Finalize()
    126.     End Sub
    127. End Class

    Here is the example you can use place code below into a buttons click event.

    vbnet Code:
    1. Dim MyVal As New Eval32
    2.  
    3.         'Add some variables.
    4.         MyVal.Vars.Add("A", 2)
    5.         MyVal.Vars.Add("B", 100)
    6.         MyVal.Vars.Add("NUM", 1024)
    7.  
    8.         With MyVal
    9.             'Example 1
    10.             .Expression = "pi"
    11.             MsgBox("PI = " & .Result, vbOKOnly Or vbInformation)
    12.             'Example 2
    13.             .Expression = "A * B"
    14.             MsgBox("A*B = " & .Result, vbOKOnly Or vbInformation)
    15.             'Example 3
    16.             .Expression = "10+10*2+A"
    17.             MsgBox("10+10*2+A = " & .Result, vbOKOnly Or vbInformation)
    18.             'Example 4
    19.             .Expression = "NUM * 2"
    20.             MsgBox("NUM * 2 = " & .Result, vbOKOnly Or vbInformation)
    21.             'Example 5
    22.             .Expression = "E"
    23.             MsgBox("E = " & .Result, vbOKOnly Or vbInformation)
    24.         End With
    Last edited by BenJones; Feb 4th, 2012 at 04:40 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width