Results 1 to 9 of 9

Thread: ARGH! Equation solver program

  1. #1

    Thread Starter
    Fanatic Member alkatran's Avatar
    Join Date
    Apr 2002
    Location
    Canada
    Posts
    860

    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:
    1. Public Function SolveEquation(ByVal Equation As String)
    2. Dim A As Long
    3. Dim B As Long
    4. Dim C As Long
    5. Dim LastBracket As Long
    6. Dim Level As Long
    7. Dim BracketLevel As Long
    8. Dim Number As Single
    9.  
    10.     'Go through equation, adding parantheses for PEDMAS
    11.     Level = 1
    12.     Do While A <= Len(Equation)
    13.         A = A + 1
    14.         Select Case Mid(Equation, A, 1)
    15.         Case "("
    16.             BracketLevel = BracketLevel + 1
    17.         Case ")"
    18.             BracketLevel = BracketLevel - 1
    19.         Case "+", "-"
    20.             Select Case Level
    21.             Case 2, 3
    22.                 Equation = Left(Equation, A - 1) & ")" & Mid(Equation, A, Len(Equation))
    23.                 BracketLevel = BracketLevel - 1
    24.             End Select
    25.             Level = 1
    26.         Case "/", "\", "*"
    27.             Select Case Level
    28.             Case 1
    29.                 'Open parenthese before number
    30.                 For B = A - 1 To 1 Step -1
    31.                     Select Case Mid(Equation, B, 1)
    32.                     Case "+", "-"
    33.                         Equation = Left(Equation, B) & "(" & Mid(Equation, B + 1, Len(Equation))
    34.                         BracketLevel = BracketLevel + 1
    35.                         B = 0
    36.                     End Select
    37.                     If B = 1 Then
    38.                         Equation = "(" & Equation
    39.                         BracketLevel = BracketLevel + 1
    40.                         B = 0
    41.                     End If
    42.                 Next B
    43.             Case 3
    44.                 Equation = Left(Equation, A - 1) & ")" & Mid(Equation, A, Len(Equation))
    45.                 BracketLevel = BracketLevel - 1
    46.             End Select
    47.             Level = 2
    48.         Case "^"
    49.             Select Case Level
    50.             Case 1, 2
    51.                 'Open parenthese before number
    52.                 For B = A - 1 To 1 Step -1
    53.                     Select Case Mid(Equation, B, 1)
    54.                     Case "+", "-", "*", "/", "\"
    55.                         Equation = Left(Equation, B) & "(" & Mid(Equation, B + 1, Len(Equation))
    56.                         BracketLevel = BracketLevel + 1
    57.                         B = 0
    58.                     End Select
    59.                     If B = 1 Then
    60.                         Equation = "(" & Equation
    61.                         BracketLevel = BracketLevel + 1
    62.                         B = 0
    63.                     End If
    64.                 Next B
    65.             End Select
    66.             Level = 3
    67.         Case " "
    68.         Case Else 'number
    69.         End Select
    70.     Loop
    71.     For BracketLevel = BracketLevel To 1 Step -1
    72.         Equation = Equation & ")"
    73.     Next BracketLevel
    74.     Equation = "(" & Equation & ")"
    75.     'Equation prepped
    76.    
    77.     'Begin solving
    78.     A = 0
    79.     Level = 0
    80.     'Logic: Loop throug equation, looking for ")" and storing "("
    81.     'Solve from last "(" to ")", since it is all on same level
    82.     'Start from beginning of equation with solved bracket
    83.     'Repeat until no brackets left
    84.    
    85.     'Loop through equation
    86.     Do While A <= Len(Equation)
    87.         A = A + 1
    88.         Select Case Mid(Equation, A, 1)
    89.         Case "("
    90.             LastBracket = A
    91.         Case ")"
    92.             'Since brackets are being closed, this is
    93.             'the highest in PEDMAS, solve inside brackets
    94.             B = LastBracket
    95.             'using bracket level as a true/false
    96.             BracketLevel = 0
    97.             Do Until BracketLevel
    98.                 B = B + 1
    99.                 Select Case Mid(Equation, B, 1)
    100.                 Case "+"
    101.                     Level = 1
    102.                 Case "-"
    103.                     Level = 2
    104.                 Case "\", "/"
    105.                     Level = 3
    106.                 Case "*"
    107.                     Level = 4
    108.                 Case "^"
    109.                     Level = 5
    110.                 Case ")"
    111.                     A = B
    112.                     BracketLevel = 1 'exit the loop, bracket solved
    113.                     'remove brackets and leave solved number
    114.                     Equation = Left(Equation, LastBracket - 1) & CStr(Number) & Mid(Equation, A, Len(Equation))
    115.                 Case Else
    116.                     C = 0
    117.                     'Get the entire number, not just 1st digit
    118.                     Do While IsNumeric(Mid(Equation, C + B, 1)) Or Mid(Equation, C + B, 1) = "."
    119.                         C = C + 1
    120.                     Loop
    121.                     'If this isn't the first number (>4< + 5), (1st + 2nd)
    122.                     If Level Then
    123.                         Select Case Level
    124.                         Case 1 'Addition
    125.                             Number = Number + CSng(Mid(Equation, B, C))
    126.                         Case 2 'Subtraction
    127.                             Number = Number - CSng(Mid(Equation, B, C))
    128.                         Case 3 'Division
    129.                             If CSng(Mid(Equation, B, C)) = 0 Then
    130.                                 Number = 0
    131.                             Else
    132.                                 Number = Number / CSng(Mid(Equation, B, C))
    133.                             End If
    134.                         Case 4 'Multiplication
    135.                             Number = Number * CSng(Mid(Equation, B, C))
    136.                         Case 5 'Exponential
    137.                             Number = Number ^ CSng(Mid(Equation, B, C))
    138.                         End Select
    139.                         Number = CSng(Format(Number, "########0.####"))
    140.                         Equation = Left(Equation, LastBracket) & CStr(Number) & Mid(Equation, B + C, Len(Equation))
    141.                         B = LastBracket
    142.                         Level = 0
    143.                     Else
    144.                         Number = CSng(Mid(Equation, B, C))
    145.                         B = B + C - 1 'Put B at the end of the number
    146.                     End If
    147.                 End Select
    148.             Loop
    149.             'get rid of brackets
    150.             Equation = Left(Equation, B - 2) & Mid(Equation, B, Len(Equation))
    151.             'Start solving from start again, with brackets solved
    152.             A = 0
    153.             Level = 0
    154.             LastBracket = 0
    155.         End Select
    156.         'Wouldn't want a nasty inescapable infinite loop
    157.         DoEvents
    158.     Loop
    159.     SolveEquation = Equation
    160. 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.

  2. #2
    Only Slightly Obsessive jemidiah's Avatar
    Join Date
    Apr 2002
    Posts
    2,431
    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.

  3. #3

    Thread Starter
    Fanatic Member alkatran's Avatar
    Join Date
    Apr 2002
    Location
    Canada
    Posts
    860
    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.

  4. #4
    Only Slightly Obsessive jemidiah's Avatar
    Join Date
    Apr 2002
    Posts
    2,431
    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.

  5. #5

    Thread Starter
    Fanatic Member alkatran's Avatar
    Join Date
    Apr 2002
    Location
    Canada
    Posts
    860
    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.

  6. #6
    Frenzied Member Acidic's Avatar
    Join Date
    Sep 2003
    Location
    UK
    Posts
    1,090
    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.

  7. #7

    Thread Starter
    Fanatic Member alkatran's Avatar
    Join Date
    Apr 2002
    Location
    Canada
    Posts
    860
    That is awsome Acidic. But it destroys the point!
    Don't pay attention to this signature, it's contradictory.

  8. #8
    Frenzied Member Acidic's Avatar
    Join Date
    Sep 2003
    Location
    UK
    Posts
    1,090
    Originally posted by alkatran
    That is awsome Acidic. But it destroys the point!
    thx and sorry.
    Have I helped you? Please Rate my posts.

  9. #9
    Only Slightly Obsessive jemidiah's Avatar
    Join Date
    Apr 2002
    Posts
    2,431
    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
  •  



Click Here to Expand Forum to Full Width