PDA

Click to See Complete Forum and Search --> : ARGH! Equation solver program


alkatran
Apr 11th, 2004, 08:18 PM
To start, it doesn't actually solve equations yet. It only sums them up.. erm...

It changes 4+5*6^2 to 1300

ANYWAYS. I have run into problems. Here's the (large amount of) code:


Public Function SolveEquation(ByVal Equation As String)
Dim A As Long
Dim B As Long
Dim C As Long
Dim LastBracket As Long
Dim Level As Long
Dim BracketLevel As Long
Dim Number As Single

'Go through equation, adding parantheses for PEDMAS
Level = 1
Do While A <= Len(Equation)
A = A + 1
Select Case Mid(Equation, A, 1)
Case "("
BracketLevel = BracketLevel + 1
Case ")"
BracketLevel = BracketLevel - 1
Case "+", "-"
Select Case Level
Case 2, 3
Equation = Left(Equation, A - 1) & ")" & Mid(Equation, A, Len(Equation))
BracketLevel = BracketLevel - 1
End Select
Level = 1
Case "/", "\", "*"
Select Case Level
Case 1
'Open parenthese before number
For B = A - 1 To 1 Step -1
Select Case Mid(Equation, B, 1)
Case "+", "-"
Equation = Left(Equation, B) & "(" & Mid(Equation, B + 1, Len(Equation))
BracketLevel = BracketLevel + 1
B = 0
End Select
If B = 1 Then
Equation = "(" & Equation
BracketLevel = BracketLevel + 1
B = 0
End If
Next B
Case 3
Equation = Left(Equation, A - 1) & ")" & Mid(Equation, A, Len(Equation))
BracketLevel = BracketLevel - 1
End Select
Level = 2
Case "^"
Select Case Level
Case 1, 2
'Open parenthese before number
For B = A - 1 To 1 Step -1
Select Case Mid(Equation, B, 1)
Case "+", "-", "*", "/", "\"
Equation = Left(Equation, B) & "(" & Mid(Equation, B + 1, Len(Equation))
BracketLevel = BracketLevel + 1
B = 0
End Select
If B = 1 Then
Equation = "(" & Equation
BracketLevel = BracketLevel + 1
B = 0
End If
Next B
End Select
Level = 3
Case " "
Case Else 'number
End Select
Loop
For BracketLevel = BracketLevel To 1 Step -1
Equation = Equation & ")"
Next BracketLevel
Equation = "(" & Equation & ")"
'Equation prepped

'Begin solving
A = 0
Level = 0
'Logic: Loop throug equation, looking for ")" and storing "("
'Solve from last "(" to ")", since it is all on same level
'Start from beginning of equation with solved bracket
'Repeat until no brackets left

'Loop through equation
Do While A <= Len(Equation)
A = A + 1
Select Case Mid(Equation, A, 1)
Case "("
LastBracket = A
Case ")"
'Since brackets are being closed, this is
'the highest in PEDMAS, solve inside brackets
B = LastBracket
'using bracket level as a true/false
BracketLevel = 0
Do Until BracketLevel
B = B + 1
Select Case Mid(Equation, B, 1)
Case "+"
Level = 1
Case "-"
Level = 2
Case "\", "/"
Level = 3
Case "*"
Level = 4
Case "^"
Level = 5
Case ")"
A = B
BracketLevel = 1 'exit the loop, bracket solved
'remove brackets and leave solved number
Equation = Left(Equation, LastBracket - 1) & CStr(Number) & Mid(Equation, A, Len(Equation))
Case Else
C = 0
'Get the entire number, not just 1st digit
Do While IsNumeric(Mid(Equation, C + B, 1)) Or Mid(Equation, C + B, 1) = "."
C = C + 1
Loop
'If this isn't the first number (>4< + 5), (1st + 2nd)
If Level Then
Select Case Level
Case 1 'Addition
Number = Number + CSng(Mid(Equation, B, C))
Case 2 'Subtraction
Number = Number - CSng(Mid(Equation, B, C))
Case 3 'Division
If CSng(Mid(Equation, B, C)) = 0 Then
Number = 0
Else
Number = Number / CSng(Mid(Equation, B, C))
End If
Case 4 'Multiplication
Number = Number * CSng(Mid(Equation, B, C))
Case 5 'Exponential
Number = Number ^ CSng(Mid(Equation, B, C))
End Select
Number = CSng(Format(Number, "########0.####"))
Equation = Left(Equation, LastBracket) & CStr(Number) & Mid(Equation, B + C, Len(Equation))
B = LastBracket
Level = 0
Else
Number = CSng(Mid(Equation, B, C))
B = B + C - 1 'Put B at the end of the number
End If
End Select
Loop
'get rid of brackets
Equation = Left(Equation, B - 2) & Mid(Equation, B, Len(Equation))
'Start solving from start again, with brackets solved
A = 0
Level = 0
LastBracket = 0
End Select
'Wouldn't want a nasty inescapable infinite loop
DoEvents
Loop
SolveEquation = Equation
End Function


The problem is usually right after the format(...) near the end. It says type mismatch... grrr...


Anyways, it's great if you see the problem. If you have any pointers, shout them out. I'll be toiling on this.

*edit* commented code a bit

jemidiah
Apr 11th, 2004, 10:56 PM
For me, it works fine as long as you don't give it a variable. Is that the problem or did I miss something?

alkatran
Apr 11th, 2004, 10:57 PM
What do you mean don't give it a variable? (don't tell me I;ve been calling it wrong! :blush: )

jemidiah
Apr 11th, 2004, 11:00 PM
If I pass it a normal mathmatical operation (say... (1+2)^5) it works fine. With, say, (1+a)^5=6, it gives the error.

PS. I haven't actually looked at the code; I'm lazy and want to know what to look for first ;)

alkatran
Apr 11th, 2004, 11:08 PM
It doesn't actually solve equations, it just simplifies them to a single number... the name is sortof misleading, lol.:lol:

Acidic
Apr 12th, 2004, 08:19 AM
hehe, this can be done in a few lines of HTML:

<html>
<input type="text" onBlur="this.value=eval(this.value)">
</html>


if you type in 3+4, it will return 7.

you can also use it to store variables.

eg.
a=Math.pi

then 2*a will return 6.2...

alkatran
Apr 12th, 2004, 08:41 AM
That is awsome Acidic. But it destroys the point! ;)

Acidic
Apr 12th, 2004, 12:54 PM
Originally posted by alkatran
That is awsome Acidic. But it destroys the point! ;)
thx ;) and sorry.

jemidiah
Apr 12th, 2004, 01:01 PM
Well, you can actually add a reference to the MS Script control and use its Eval function in much the same way from VB, though I thought you wanted to write your own parser ;)