Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

DarkBASIC Professional Discussion / I have this question...

Author
Message
The Exponentiator
19
Years of Service
User Offline
Joined: 16th Feb 2006
Location:
Posted: 16th Feb 2006 20:11
This might sound kinda dumb, but has anyone got any ideas about taking a string like, "6*x^2+4*x+2" which could be brought from file, making x a variable which could be used in calculation or graph making.

I wondered if there was a way to 'take the quotes off' of a string variabl and performing the calculation without the complexity that my program is quickly developing, with operator precedants and multiple terms etc. ???

REM Project: calcfunc2
REM Created: 13/02/2006 10:54:35
REM
REM ***** Main Source File *****
REM
Dim Term(10)
instr$ = "6*x^2+4*x+2"
print instr$
print "-------------------------------------------------------"
m=1
t=0
x=4

Repeat
for p = m to len(instr$)
SS$ = SS$ + Mid$(instr$, p)
m=m+1
If (Mid$(instr$, p+1) = "+" or Mid$(instr$, p+1) = "-") then goto exitfor

next p

exitfor:
Print SS$
Print m

Term(t) = WorkOutTerm(SS$, x)

SS$ = ""
t=t+1
Until m=len(instr$)+1

Print "-----------------------------------------------"

For u = 0 to t-1
Print Term(u)
Next u

For u = 0 to 4
y=y+Term(u)
Next u

Print "WHEN x = " + Str$(x) + ", y=" + Str$(y)

Wait key

end

Function WorkOutTerm(SubString$, x)
Print "PassedString = " + SubString$
q=0
`eg 6*x^2 where x=3
Dim Termarr(5)

For p = 1 to len(SubString$)



If Mid$(SubString$,p) = "^"
Termarr(q) = p
if Mid$(SubString$,p-1) = "x" then temp = x ^ Val(Mid$(SubString$,p+1))
if Mid$(SubString$,p+1) = "x" then temp = Val(Mid$(SubString$,p-1)) ^ x
if Mid$(SubString$,p-1) <> "x" AND Mid$(SubString$,p+1) <> "x" then temp = Val(Mid$(SubString$,p-1)) ^ Val(Mid$(SubString$,p+1))
q=q+1
expon=1
expon_val = p
Endif

If Mid$(SubString$,p) = "*"

If expon=0

if Mid$(SubString$,p-1) = "x" then temp = x * Val(Mid$(SubString$,p+1))
if Mid$(SubString$,p+1) = "x" then temp = Val(Mid$(SubString$,p-1)) * x
if Mid$(SubString$,p-1) <> "x" AND Mid$(SubString$,p+1) <> "x" then temp = Val(Mid$(SubString$,p-1)) * Val(Mid$(SubString$,p-1))

Endif

If expon=1
`This gets a bit runny...
` temp = temp * get_new_val(expon_val)
` if Mid$(SubString$,p-1) = "x" then temp = x * temp
` if Mid$(SubString$,p+1) = "x" then temp = temp * x
` if Mid$(SubString$,p-1) <> "x" AND Mid$(SubString$,p+1) <> "x" then temp = Val(Mid$(SubString$,p-1)) * Val(Mid$(SubString$,p+1))

Endif

q=q+1
Endif

Next p

const=1
For p = 1 to len(SubString$)
if Mid$(SubString$, p)="^" or Mid$(SubString$, p)="*" then const = 0
Next p

If const = 1
For p = 1 to len(Substring$)
RSubString$ = RSubString$ + Mid$(Substring$, p+1)
Next p
Result = Val(RSubString$)
Else
Result = temp
Endif

EndFunction Result

Function get_new_val(expon_val)

Endfunction 0

In the beginning was the word, it was 'damn*#!'
BatVink
Moderator
22
Years of Service
User Offline
Joined: 4th Apr 2003
Location: Gods own County, UK
Posted: 16th Feb 2006 21:10
I don't know the answer, but do a search on t'internet for parsing methodologies, you might just find what you need.

Ric
20
Years of Service
User Offline
Joined: 11th Jul 2004
Location: object position x
Posted: 17th Feb 2006 02:12 Edited at: 17th Feb 2006 02:20
I've had a think about it - and I think there IS a shortcut to get it to do the calculation without parsing the string into operations as you are doing - but you may not like it - it will involve using the string to write a new .dba file, which will then need to be manually compiled and executed. Give me a minute and I'll post an example.

Ric
20
Years of Service
User Offline
Joined: 11th Jul 2004
Location: object position x
Posted: 17th Feb 2006 02:24 Edited at: 17th Feb 2006 02:26


OK - this takes your equation stored in the string, and some value for x, and writes a new program which solves the equation for x, then saves the program as a dba file. You would then need to compile the new program in order to view the solution. It's no good for an application you plan to release, as it requires dbpro for compiling the solution program, but it would do for personal use.

Other than that, I'm afraid, I can't see any way to turn the string into a command other that parsing it and telling it what to do for each operator - ie. what you're already doing.

Pheonixx
21
Years of Service
User Offline
Joined: 6th Oct 2003
Location: Columbus, Ohio
Posted: 17th Feb 2006 08:56
In c there is a function called ATOI()

integer = atoi(string$)

closest thing I can think of is the VAL(), but I'd have to play with it to see how operands are treated.

All else fails, evaluate the string peice by peice and convert ASCII codes into integer values and operations. If CHR$(27) = 1 then "user pressed the escape key"

http://ausukusa.breakset.com
The Exponentiator
19
Years of Service
User Offline
Joined: 16th Feb 2006
Location:
Posted: 17th Feb 2006 10:56
Would atoi work in C on functions as well as just the numbers? I did learn quite a bit of C++ a while ago and suppose it could work if the string was saved as a text file and DBPRO would save the string file, then call a C++/C program to work out the function in the string e.g. 6x^2 then return that as a seperate text file to be used by the DBPRO, unless there is an easier method of interaction?

This is all on theassumption that DBPRO will run independant .exe files, im sure i read somewhere that it could...

In the beginning was the word, it was 'damn*#!'
Ric
20
Years of Service
User Offline
Joined: 11th Jul 2004
Location: object position x
Posted: 17th Feb 2006 13:23
Quote: "closest thing I can think of is the VAL()"


That's the first thing I thought of too ..... it doesn't work though. Val() just finds the first number it comes across in the string, but it doesn't do any calculations, or even recognise the operators.

Pheonixx
21
Years of Service
User Offline
Joined: 6th Oct 2003
Location: Columbus, Ohio
Posted: 17th Feb 2006 14:52
sorry that's not what I meant... you would have to scan the string for operands and use them to trigger different function calls, using VAL or ATOI is just a fast way of getting the integer part extracted.

start at the beginning of the string, put the numbers into float A, read the operand and put that function ID in queu, 0 for equals, 1 for add, 2 for subtract, etc. put the next group of numbers into float B, etc until you have finished the string. Run the function queu and print the result.

thestring$ = "123+456="



function calculatestring(thestring$)

DIM FunctionList(10) As Integer
DIM Values(10) As Float

extract the first group of numbers and store them into index 0
Values(0) = val("123")

extract the operand
FunctionList(0) = 1
ProcessLoop = ProcessLoop + 1

extract the second group of numbers and store them into index 1
Values(1) = val("456")

extract the operand
FunctionList(1) = 0
ProcessLoop = ProcessLoop + 1

FunctionList(n) = 0 so now run thru the function list...

For Process = 0 to ProcessLoop
Select FunctionList(Process)
Case 0
Results# = Values(0)
exit
EndCase
Case 1
values(Process) = values(Process)+values(Process+1)
Endcase
Case 2
values(Process) = values(Process)-values(Process+1)
Endcase
EndSelect
Next Process

UNDIM FunctionList(0)
UNDIM Values(0)

EndFunction Results#

Something like that outta do the trick. You'll have to get fancy with orders of operations and other such stuff, probably some temp variables to store different calculation results withen parethesis and then put them all together in the end. Not an easy task beyond straight line basic operands.

http://ausukusa.breakset.com
The Exponentiator
19
Years of Service
User Offline
Joined: 16th Feb 2006
Location:
Posted: 17th Feb 2006 20:27
Would this be a reasonable program for calculating a function, it only allows single values, no variables, no negative signs. Ill see what I can do, a get_operator function may fix the single value, no negative values part...



Thanks for all the help, some great ideas!!!

In the beginning was the word, it was 'damn*#!'

Login to post a reply

Server time is: 2025-06-04 18:29:26
Your offset time is: 2025-06-04 18:29:26