Have you ever wanted to develop a math parser and evaluator? Are you in such a quandary or just have curiosity or interest in the matter? If so, you may download the complete source version 8.3.48 or later, but be warned there is almost no comment; or instead, follow versions v8.4.x which will be more digest and, of course, all the source code for each new v8.4.x will be there, if you wish, to download.
Currently, v8.4.0 does simple arithmetic but contains the main classes and algorithms that hold all the rest.
Version 8.4.0 has been developed entirely under MS Visual Studio Express 2013 for Windows Desktop and, surely, in future releases.
In the downloadable example you may find Form1 having 2 textboxes: tbInput and tbOuput, one button and a label to show eventual messages: btnCalculate and lblMessage.
When running, user types for example "3+4/2" (without quotes) in tbInput and clicks the "Calculate" button, so btnCalculate_Click() is executed and, in turn, the Calculate() method.
There, the shared method exprParser.Parse is called passing the input string as parameter.
If everything goes right, there will be no error, eP.retErr will be 'Nothing' and eP.retVal will have the result.
If not, eP.retErr.ToString contains the error message.
In the example, the ouput would be 5.
On the contrary, an incomplete "3+4/" would show the corresponding error message.
Note: simple arithmetic of only numbers and operators give the expected result, at the moment.
Public Class Form1
Private Sub btnCalculate_Click(sender As Object, e As EventArgs) Handles btnCalculate.Click
Calculate()
End Sub
Sub Calculate()
Try
Dim eP As exprParser = exprParser.Parse(tbInput.Text)
If eP.retErr IsNot Nothing Then
' Show error message:
lblMessage.Text = eP.retErr.ToString
tbOutput.Text = ""
Else
' Show the result:
tbOutput.Text = eP.retVal.ToString
lblMessage.Text = ""
End If
Catch ex As Exception
MessageBox.Show("Unexpected error:" + vbCrLf + ex.Message)
End Try
End Sub
End Class
You may find the afore mentioned here.