
Nov 5th, 2011, 02:05 PM
#1
Thread Starter
Hyperactive Member
Validate algebraic equations.
At the moment I'm in the process of building a class that can handle more advanced mathematical equations and processes. Essentially my end goal is to to have my class capable of processing anything you'd learn in Elementary Algebra through College Algebra.
So far I am able to multiply monomials and order polynomials.. However for the most part the equations in questioned being processed have to be in correct form. IE PointSlope Form, YIntercept Form, etc etc.
I'm just curious as to how I can go about "validating an equation type by it's current form". If there is an easier way or not that I'm unaware of, or if I'm just stuck manually doing so.

Nov 5th, 2011, 02:13 PM
#2
Re: Validate algebraic equations.

Nov 8th, 2011, 01:23 AM
#3
New Member
Re: Validate algebraic equations.
is there any readymade classes and objects for solving and validating algebraic equations?? i want to give a project on that in vb.net so please is there any code or any help you wanna give me please post about that....

Nov 8th, 2011, 09:17 AM
#4
Re: Validate algebraic equations.
First of all, You search before making a post, second if there is nothing in the search you create your own thread (instead of hijacking someone else thread asking unrelated questions) and ask there, and when you make a post please use the default font, size and style.
EDIT: and I don't know how you missed the links I posted above doing almost exactly what you need.

Nov 8th, 2011, 12:25 PM
#5
Thread Starter
Hyperactive Member
Re: Validate algebraic equations.
Well so far this is what I've came up with. It works, very buggy. But works nonetheless. However every combination of numbers and operations I've tried seem to get evaluated correctly at the moment. Anything syntax that isn't correct will throw an error, but again that just brings me back to my original post. I'm still working on a function that "validates" an expression for proper syntax.
It supports (), *, /, +, , and ^.. No Square roots yet.
However if you have for example (9)^2, (9) gets replaced with [9] during the process of evaluating the expression. I had to do this just so all negative constants are handled correctly when raising them to a power. Soo I'm assuming, and have yet to test this but using any brackets will cause an exception to be thrown.
Enumerators
Code:
Enum Operations
Exponents = 1
Parenthesis = 2
Multiplication = 3
Division = 3
Addition = 4
Subtraction = 4
End Enum
Solve expression function.
Code:
''' <summary>
''' Evaluates a mathematical expression.
''' </summary>
''' <param name="expression">String; Mathematical expression to be evaluated.</param>
''' <returns>Double; Solution.</returns>
Function SolveExpression(ByVal expression As String) As Double
Dim result As String = expression
Dim cArray() As Char = expression.ToCharArray()
Dim oP As Int32 = 0, cP As Int32 = 0
Dim c As Int32 = cArray.Count1
Dim p As String 'Part of the expression extracted to be solved.
Dim s As String 'Solution
If result.Contains("(") Then
Do While result.Contains("(") 'Loop through the expression until all parenthesis have been processed.
For i As Int32 = result.IndexOf("(") to c 'Start at the first opening parenthesis
If cArray(i) = "("C Then oP = i+1 'Remember the position of the opening parenthesis everytime one has been found.
If cArray(i) = ")"C Then 'Continue to look for the first closing parenthesis.
cP = i 'Remember the position.
Exit For 'Exit out of the for loop, we
End If
Next
p = result.Substring(oP,cPoP) 'Extract the inner expression to be evaluated.
s = SolveSubExpression(p).ToString()
If cP+1 <= c Then 'Make sure were checking inside the range.
If cArray(cP+1) = "^" Then
s = "[" & s & "]"
End If
End If
'Replace the sub expression including parenthesis with the solution to said sub expression.
result = result.Replace("(" & p & ")", s)
cArray = result.ToCharArray() 'Update character array.
Loop 'Do it all over again till all parenthesis are gone.
End If
'Finally solve for the last expression that may be, and return the solution.
Return SolveSubExpression(result)
End Function
All other sub functions used to solve an expression.
Code:
''' <returns>Evaluates a sub expression  one that does not contain any parenthesis.</returns>
Protected Function SolveSubExpression(ByVal expression As String) As Double
'The expression being passed cannot cotain any parenthesis.
If Not expression.Contains("(") Or Not expression.Contains(")") Then
Dim result As String = expression.Replace(" ", vbNullString)
Dim cArray() As Char = result.ToCharArray()
'Perform the order of operations.
'1) Solve exponents.
result = PerformOperation(result, Operations.Exponents)
'3) Solve all multiplication and division operations.
result = PerformOperation(result, Operations.Multiplication And Operations.Division)
'4) Solve all addition and subtraction operations.
result = PerformOperation(result, Operations.Addition And Operations.Subtraction)
Return Convert.ToDouble(result) 'Return the final solution as a double.
Else
MessageBox.Show("Expression cannot contain parenthesis.")
Return 0
End If
End Function
''' <returns>Evaluates a particular operation(s) of an expression.</returns>
Protected Function PerformOperation(ByVal expression As String, ByVal Operation As Operations) As String
Dim result As String = expression.Replace(" ", vbNullString)
Dim cArray() As Char = result.ToCharArray()
Select Case Operation
Case Operations.Exponents
If result.Contains("^") Then
Do While result.Contains("^") 'Loop through the expression until all root have been evaluated.
' Constant Exponent
Dim c As String = vbNullString, e As String = vbNullString
Dim v As Double 'Value.
Dim Brackets As Boolean = False
'Obtain the constant.
If cArray((result.IndexOf("^")1)) = "]"C Then
For i As Int32 = (result.IndexOf("^")2) To 0 Step (1)
If cArray(i) = "["C Then Exit For
c = cArray(i).ToString & c
Next
Brackets = True
Else
For i As Int32 = (result.IndexOf("^")1) to 0 Step (1)
If Char.IsNumber(cArray(i)) or cArray(i).ToString = "." Then
c = cArray(i).ToString() & c
Else
Exit For
End If
Next
End If
'Obtain the exponent.
For i As Int32 = result.IndexOf("^")+1 to cArray.Count1
If Char.IsNumber(cArray(i)) or cArray(i) = "."C Then
e = e & cArray(i).ToString()
Else
Exit For
End If
Next
'Calculate the value.
v = Convert.ToDouble(c)^Convert.ToDouble(e)
If Brackets Then
result = result.Replace("[" & c & "]^" & e, v.ToString()) 'Insert the value into the expression.
Else
result = result.Replace(c & "^" & e, v.ToString()) 'Insert the value into the expression.
End If
cArray = result.ToCharArray() 'Update the character array.
Loop
End If
Case Operations.Multiplication And Operations.Division 'Both multiplication and division get processed the same.
If result.Contains("/") Then
Do While result.Contains("/")
Dim constants(1) As String
Dim t As Double = 0 'Total
For i As Int32 = result.IndexOf("/")1 To 0 Step 1
If Char.IsNumber(cArray(i)) Then
constants(0) = cArray(i).ToString & constants(0)
Else
Exit For
End If
Next
For i As Int32 = result.IndexOf("/")+1 To cArray.Count1
If Char.IsNumber(cArray(i)) Then
constants(1) = constants(1) & cArray(i).ToString
Else
Exit For
End If
Next
t = Convert.ToDouble(constants(0))/Convert.ToDouble(constants(1))
result = result.Replace(constants(0) & "/" & constants(1), t.ToString())
cArray = result.ToCharArray()
Loop
End If
If result.Contains("*") Then
Do While result.Contains("*")
Dim constants(1) As String
Dim t As Double = 0 'Total
For i As Int32 = result.IndexOf("*")1 To 0 Step 1
If Char.IsNumber(cArray(i)) Then
constants(0) = cArray(i).ToString & constants(0)
Else
Exit For
End If
Next
For i As Int32 = result.IndexOf("*")+1 To cArray.Count1
If Char.IsNumber(cArray(i)) Then
constants(1) = constants(1) & cArray(i).ToString
Else
Exit For
End If
Next
t = Convert.ToDouble(constants(0))*Convert.ToDouble(constants(1))
result = result.Replace(constants(0) & "*" & constants(1), t.ToString())
cArray = result.ToCharArray()
Loop
End If
Case Operations.Addition And Operations.Subtraction 'Addition and subtraction must be processed at the same time.
If result.Contains("+") Or result.Contains("") Then
Dim constants() As String
Dim t As Double = 0 'Total.
'Fix any double operators we may have left over.
If result.Contains("+") Then result = result.Replace("+", "")
If result.Contains("+") Then result = result.Replace("+", "")
If result.Contains("++") Then result = result.Replace("++", "+")
If result.Contains("") Then result = result.Replace("", "+")
'Add in the seperator at every operand.
result = result.Replace("+", " +")
result = result.Replace("", " ")
'Split the result to obtain all constants.
constants = result.Split(" "C)
For i As Int32 = 0 to constants.Count1
If constants(i) <> vbNullString Then
t = t + Convert.ToDouble(constants(i))
End If
Next
result = t.ToString()
End If
End Select
Return result
End Function
Hope this helps anyone looking for a direction in accomplishing the same task. But if anyone that has had experience in this topic that has any pointers feel free to be a critic.
Just keep in mind this is my "Draft" if you will.. I code, I get it to work by any means necessary, then once it functions properly I go through and finalize. Sooo if anything is terribly wrong then I probably know about it.. or will.
EDIT: I forgot to add that my process can't handle 2(2) yet, however it can handle 2*(2). This'll be fixed shortly.
Last edited by DavesChillaxin; Nov 8th, 2011 at 02:59 PM.

Nov 9th, 2011, 10:37 PM
#6
Thread Starter
Hyperactive Member
Re: Validate algebraic equations.
FIX: Can now handle n(n) or vice versa (n)n. Little bugs have been fixed as well so give it a shot. However proper syntax is still required, otherwise exceptions will arise.
Code:
Function SimplifyAlgebraicExpression(ByVal expression As String) As Double
Dim result As String = expression.Replace(")(", ")*(")
Dim cArray() As Char = expression.ToCharArray()
Dim oP As Int32 = 0, cP As Int32 = 0
Dim p As String 'Part of the expression extracted to be solved.
Dim s As String 'Solution
Dim Bracket As Boolean = False
If result.Contains("(") Then
Dim c As Char
'Fix any multiplication that may be without the operator. IE x(x), which is required to look like x*(x).
For i As Int32 = result.IndexOf("(") to (cArray.Count1) 'Start at the first opening parenthesis
If cArray(i) = "("C AndAlso i1 >= 0 Then
c = cArray(i1)
If Char.IsNumber(c) Then
result = result.Replace(c & "(", c & "*(")
End If
End If
If cArray(i) = ")"C AndAlso i+1 <= cArray.Count1 Then 'Continue to look for the first closing parenthesis.
c = cArray(i+1)
If Char.IsNumber(c) Then
result = result.Replace(")" & c, ")*" & c)
End If
End If
Next
'Loop through the expression until all parenthesis have been evaluated.
Do While result.Contains("(")
cArray = result.ToCharArray
For i As Int32 = result.IndexOf("(") to (cArray.Count1) 'Start at the first opening parenthesis
If cArray(i) = "("C Then
oP = i+1 'Remember the position of the opening parenthesis everytime one has been found.
End If
If cArray(i) = ")"C Then 'Continue to look for the first closing parenthesis.
cP = i 'Remember the position.
Exit For 'Exit out of the for loop, we
End If
Next
p = result.Substring(oP,cPoP) 'Extract the inner algebraic expression to be evaluated.
s = SimplifySubAlgebraicExpression(p).ToString()
If cP+1 <= cArray.Count1 Then 'Make sure we're within range.
If cArray(cP+1) = "^" Then 'Check if the sub algebraic expression as a whole has any exponents.
s = "[" & s & "]^" 'Add brackets around the solution, which will be handled when solving for exponents.
Bracket = True
End If
End If
'Replace the sub expression including parenthesis with the solution to said sub expression.
If Bracket Then
result = result.Replace("(" & p & ")^", s)
Bracket = False
Else
result = result.Replace("(" & p & ")", s)
End If
cArray = result.ToCharArray() 'Update character array.
Loop 'Do it all over again till all parenthesis are gone.
End If
'Finally solve for the last expression that may be, and return the solution.
Return SimplifySubAlgebraicExpression(result)
End Function
Last edited by DavesChillaxin; Nov 10th, 2011 at 12:18 PM.
Reason: EDIT: There was a little bug that went unoticed. Now no longer exists.
Posting Permissions
 You may not post new threads
 You may not post replies
 You may not post attachments
 You may not edit your posts

Forum Rules

Click Here to Expand Forum to Full Width
Survey posted by VBForums.
