|
-
Apr 11th, 2004, 08:18 PM
#1
Thread Starter
Fanatic Member
ARGH! Equation solver program
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:
VB 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
Last edited by alkatran; Apr 11th, 2004 at 08:28 PM.
Don't pay attention to this signature, it's contradictory.
-
Apr 11th, 2004, 10:56 PM
#2
For me, it works fine as long as you don't give it a variable. Is that the problem or did I miss something?
The time you enjoy wasting is not wasted time.
Bertrand Russell
<- Remember to rate posts you find helpful.
-
Apr 11th, 2004, 10:57 PM
#3
Thread Starter
Fanatic Member
What do you mean don't give it a variable? (don't tell me I;ve been calling it wrong! )
Don't pay attention to this signature, it's contradictory.
-
Apr 11th, 2004, 11:00 PM
#4
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
The time you enjoy wasting is not wasted time.
Bertrand Russell
<- Remember to rate posts you find helpful.
-
Apr 11th, 2004, 11:08 PM
#5
Thread Starter
Fanatic Member
It doesn't actually solve equations, it just simplifies them to a single number... the name is sortof misleading, lol.
Don't pay attention to this signature, it's contradictory.
-
Apr 12th, 2004, 08:19 AM
#6
Frenzied Member
hehe, this can be done in a few lines of HTML:
Code:
<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...
Have I helped you? Please Rate my posts. 
-
Apr 12th, 2004, 08:41 AM
#7
Thread Starter
Fanatic Member
That is awsome Acidic. But it destroys the point!
Don't pay attention to this signature, it's contradictory.
-
Apr 12th, 2004, 12:54 PM
#8
-
Apr 12th, 2004, 01:01 PM
#9
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
The time you enjoy wasting is not wasted time.
Bertrand Russell
<- Remember to rate posts you find helpful.
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
|