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*#!'