You are not logged in. You can browse in the PowerBASIC Community, but you must click Login (top right) before you can post. If this is your first visit, check out the FAQ or Sign Up.
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.
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.
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.
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
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.
We process personal data about users of our site, through the use of cookies and other technologies, to deliver our services, and to analyze site activity. For additional details, refer to our Privacy Policy.
By clicking "I AGREE" below, you agree to our Privacy Policy and our personal data processing and cookie practices as described therein. You also acknowledge that this forum may be hosted outside your country and you consent to the collection, storage, and processing of your data in the country where this forum is hosted.
Comment