Made a pretty simple little equation solver, ex: "5/2*(6+7)*2" which both google and the module gives the answer 65. It gives decimal answers but can easily be changed to integer, which doesn't matter too much.
Just paste as module and use as LHMath.SolveEquation("5/2*(6+7)*2").
Source: (I didn't use regex because I made this in java too, but regex is a lot faster.)
Code:
Module LHMath
Private setNumbers As Char() = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "."}
Private setMath As Char() = {"*", "/", "+", "-"}
Private Function chrCount(ByVal src As String, ByVal chr As Char) As Int32
Dim total As Int32 = 0
Dim chrSrc As Char() = src.ToCharArray
For i As Integer = 0 To chrSrc.Length - 1
If (chrSrc(i) = chr) Then
total += 1
End If
Next
Return total
End Function
Private Function aChrContains(ByVal src As Char(), ByVal chr As Char) As Boolean
For I As Integer = 0 To src.Length - 1
If (src(I) = chr) Then
Return True
End If
Next
Return False
End Function
Private Function aStrContains(ByVal src As Char(), ByVal str As String) As Boolean
Dim chrStr As Char() = str.ToCharArray
For I As Integer = 0 To chrStr.Length - 1
If (aChrContains(src, chrStr(I)) = True) Then
Return True
End If
Next
Return False
End Function
Private Function SolveSingleEquation(ByVal obj1 As String, ByVal obj2 As String, ByVal symbol As String) As Double
obj1 = obj1.Replace(" ", "")
obj2 = obj2.Replace(" ", "")
symbol = symbol.Replace(" ", "")
Select Case symbol
Case "*"
Return CDec(obj1) * CDec(obj2)
Case "/"
Return CDec(obj1) / CDec(obj2)
Case "+"
Return CDec(obj1) + CDec(obj2)
Case "-"
Return CDec(obj1) - CDec(obj2)
End Select
Return 0
End Function
Private Function GetEquationNotation(ByVal src As String) As String()
Dim Equ As String = src.Replace(" ", "")
Dim chrEqu As Char() = Eq*****CharArray
Dim MyStack As New List(Of String)
Dim Temp As String = Nothing
For I As Integer = 0 To chrEqu.Length - 1
If (aChrContains(setMath, chrEqu(I))) Then
If Not (Temp = "") Then
MyStack.Add(Temp)
Else
MyStack.Add("0")
End If
MyStack.Add(chrEqu(I))
Temp = ""
ElseIf (I = chrEqu.Length - 1) Then
Temp = Temp & chrEqu(I)
MyStack.Add(Temp)
Temp = ""
ElseIf (Char.IsLetterOrDigit(chrEqu(I))) Then
Temp = Temp & chrEqu(I)
End If
Next
Return MyStack.ToArray
End Function
Private Function SolveSimpleEquation(ByVal NT As String()) As String
If (NT.Length = 0) Then
Return "0"
ElseIf (NT.Length = 1) Then
Return NT(0)
ElseIf (NT.Length = 2) Then
Return "0"
End If
Dim ToBeCalculated As String = Nothing
Dim A, B, C As String
A = ""
B = ""
C = ""
For I As Integer = 0 To NT.Length - 1
If (A = "") Then
A = NT(I)
ElseIf (B = "") Then
B = NT(I)
ElseIf (C = "") Then
C = NT(I)
Else
If (B = "*" Or B = "/") Then
A = SolveSingleEquation(A, C, B).ToString
B = NT(I)
C = ""
ElseIf (NT(I) = "*" Or NT(I) = "/") Then
ToBeCalculated = ToBeCalculated & A & B
A = C
B = NT(I)
C = ""
ElseIf (NT(I) = "+" Or NT(I) = "-") Then
A = SolveSingleEquation(A, C, B).ToString
B = NT(I)
C = ""
End If
End If
Next
A = SolveSingleEquation(A, C, B).ToString
If (ToBeCalculated = "") Then
Return A
Else
Dim Remains As String = ToBeCalculated & A
Dim rNT As String() = GetEquationNotation(Remains)
A = SolveSimpleEquation(rNT)
Return A
End If
Return 0
End Function
Private Function SolveComplexEquation(ByVal src As String) As Double
Dim Equ As String = src.Replace(" ", "")
While (Equ.Contains("("))
Dim A, B, C As String
A = ""
B = ""
C = ""
Dim splParam As String() = Equ.Split("(")
For I As Integer = 0 To splParam.Length - 2
A = A & splParam(I)
Next
Dim sprParam As String() = splParam(splParam.Length - 1).Split(")")
For I As Integer = 1 To sprParam.Length - 1
C = C & sprParam(I)
Next
Dim splitEqu As String = splParam(splParam.Length - 1).Split(")")(0)
Dim tNT As String() = GetEquationNotation(splitEqu)
B = SolveSimpleEquation(tNT)
Equ = A & B & C
End While
Dim fNT As String() = GetEquationNotation(Equ)
Dim Answer As String = SolveSimpleEquation(fNT)
Return Answer
End Function
Public Function SolveEquation(ByVal src As String) As Decimal
Return SolveComplexEquation(src)
End Function
End Module