Announcement

Collapse

Maintenance

The forum could be offline for 30-60 minutes in the very near future for maintenance (said 3pm Pacific). I was behind on getting this notice. I do apologize.
See more
See less

Calculating mathematical formulas

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Calculating mathematical formulas

    I'm looking to calculate math formulas as follows:

    F = "34 + 62 * 10 - 2"

    X = SomeFunction(F)

    X would contain the value 652

    Is there such a function?

    Thanks, Ron


  • #2
    Ron,

    Could you be a little more explicit? I presume you want to pass
    a string value to function 'SomeFunction(F)' and expect a
    number value in return.

    Does this setup always correspond to the exact same sequence
    ie ("A + B * C - D") or could it be different based upon the string passed, ie ("A * B - C + D") or something like that.

    To answer your question, I know of "NO" standard basic function
    that does this. Would require custom parsing of string and performing whatever action is required.

    Cecil

    ------------------

    Comment


    • #3
      Look at Gafny Jacob's "Evaluate math expression" thread in the
      source code section.

      ------------------

      Comment


      • #4
        I'm looking to evaluate any formula I enter. I'm creating a VB app that has fields that accept formulas like an Excel cell would. I just want it to be able to perform your basic math operations along with parentheses.

        Thanks, Ron

        Originally posted by Cecil Williams:
        Ron,

        Could you be a little more explicit? I presume you want to pass
        a string value to function 'SomeFunction(F)' and expect a
        number value in return.

        Does this setup always correspond to the exact same sequence
        ie ("A + B * C - D") or could it be different based upon the string passed, ie ("A * B - C + D") or something like that.

        To answer your question, I know of "NO" standard basic function
        that does this. Would require custom parsing of string and performing whatever action is required.

        Cecil


        ------------------

        Comment


        • #5
          Ronald,

          Quinn Tyler Jackson has provided a algebraic solver package which compiles in PB. The code is public domain on the ABC web site. Check out his URL http://www.qtj.net/~quinn/ for more info. It is an elegant piece of work.

          Jeff

          ------------------

          Comment


          • #6
            Hi,

            We have a lib program doing this function, the current version
            is for PDS, but we can port to PBDLL about few days and provide
            compiled DLL version.

            Are you interested?

            mailto:[email protected][email protected]</A>


            ------------------

            Comment


            • #7
              Below is a VB app I created to handle simple formulas. It shouldn't take much to convert it to a PBDLL and I'll post it again when I do this myself. It handles the following operators: + - * / ^ ( ). Make sure the operators and numbers are separated by a space. It needs to be cleaned up but it's a good start. The VB objects are defined below:

              txtFormula.text = text box that accepts the formula
              cmdCalculate_Click = command button method to calculate the value.
              value = subroutine to calculate math functions without parentheses.
              lblResult.caption = label that is set to the final value.

              The program starts at the highest level parentheses and calculates the contents. It recreates the formula by calculating the formula within the pars and removing the pars. This process is repeated until the formula is a single value without parenthesis.

              Thanks for all the help, Ron
              Code:
              Private Sub cmdCalculate_Click()
               
                  Dim c As String
                  Dim f As String
                  Dim i As Integer
                  Dim ii As Integer
                  Dim lev As Integer
                  Dim parmaxlev As Integer
                  Dim parcount As Integer
                  Dim pars(100, 3) As String
                   
                  f = txtFormula.Text
                   
              again:
                   
                  ' Locate pars.
                  lev = 0
                  parcount = 0
                  parmaxlev = 0
                  For i = 1 To Len(f)
                   
                      c = Mid(f, i, 1)
                      If c = "(" Then
                          lev = lev + 1
                          parcount = parcount + 1
                          If lev > parmaxlev Then parmaxlev = lev
                          pars(parcount, 1) = c
                          pars(parcount, 2) = Str(lev)
                          pars(parcount, 3) = Str(i)
                      End If
                      If c = ")" Then
                          parcount = parcount + 1
                          pars(parcount, 1) = c
                          pars(parcount, 2) = Str(lev)
                          pars(parcount, 3) = Str(i)
                          lev = lev - 1
                      End If
                   
                  Next i
                   
                  ' Simplify formula.
                  For i = parmaxlev To 1 Step -1
                   
                      For ii = 1 To parcount
                       
                          If pars(ii, 1) = "(" And Val(pars(ii, 2)) = i Then
                           
                              c = Mid(f, Val(pars(ii, 3)) + 1, Val(pars(ii + 1, 3) - Val(pars(ii, 3)) - 1))
                           
                              Call value(c)
                               
                              f = Left(f, Val(pars(ii, 3)) - 1) + c + Right(f, Len(f) - Val(pars(ii + 1, 3)))
                           
                              GoTo again
                          
                          End If
                  
                      Next ii
                  
                  Next i
                  
                  Call value(f)
                  
                  lblResult.Caption = f
                  
                  MsgBox "Formula Calculated"
               
              End Sub
               
              Private Sub value(f As String)
               
                  Dim c As String
                  Dim cl As String
                  Dim i As Integer
                  Dim ii As Integer
                  Dim param(100) As String
                  Dim params As Integer
               
                  f = LTrim(RTrim(f))
                   
              again:
               
                  If f <> "" Then
                   
                      ' Get formula parameters.
                      params = 1
                      For i = 1 To Len(f)
                      
                          cl = c
                          c = Mid(f, i, 1)
                          
                          If c <> " " Then
                          
                              param(params) = param(params) + c
                              
                          End If
                          If c = " " And cl <> " " Then
                          
                              params = params + 1
                              param(params) = ""
                              
                          End If
                          
                      Next i
                      
                      ' Raise number to a power.
                      For i = 1 To params
                      
                          If param(i) = "^" Then
                              param(i) = Val(param(i - 1)) ^ Val(param(i + 1))
                              param(i - 1) = ""
                              param(i + 1) = ""
                              f = ""
                              For ii = 1 To params
                                  f = f + " " + param(ii)
                              Next ii
                              GoTo again
                          End If
                          
                      Next i
                      
                      ' Multiply or divide numbers.
                      For i = 1 To params
                      
                          If param(i) = "*" Then
                              param(i) = Val(param(i - 1)) * Val(param(i + 1))
                              param(i - 1) = ""
                              param(i + 1) = ""
                              f = ""
                              For ii = 1 To params
                                  f = f + " " + param(ii)
                              Next ii
                              GoTo again
                          End If
                          If param(i) = "/" Then
                              param(i) = Val(param(i - 1)) / Val(param(i + 1))
                              param(i - 1) = ""
                              param(i + 1) = ""
                              f = ""
                              For ii = 1 To params
                                  f = f + " " + param(ii)
                              Next ii
                              GoTo again
                          End If
                          
                      Next i
                      
                      ' Add or subtract numbers.
                      For i = 1 To params
                      
                          If param(i) = "+" Then
                              param(i) = Val(param(i - 1)) + Val(param(i + 1))
                              param(i - 1) = ""
                              param(i + 1) = ""
                              f = ""
                              For ii = 1 To params
                                  f = f + " " + param(ii)
                              Next ii
                              GoTo again
                          End If
                          If param(i) = "-" Then
                              param(i) = Val(param(i - 1)) - Val(param(i + 1))
                              param(i - 1) = ""
                              param(i + 1) = ""
                              f = ""
                              For ii = 1 To params
                                  f = f + " " + param(ii)
                              Next ii
                              GoTo again
                          End If
                          
                      Next i
                      
                  End If
                   
              End Sub

              Comment


              • #8
                Yes, there is a component that will allow you to do this. Visit http://www.ucalc.com and download a copy of UCalc Fast Math Parser. It includes direct support for PowerBASIC.

                Daniel Corbier, UCALC
                Daniel Corbier
                uCalc Fast Math Parser
                uCalc Language Builder
                sigpic

                Comment

                Working...
                X