News:

Building a 3D Ray Tracer  By stevmjon

Main Menu

This simple math solver doesn't work right!

Started by LemonWizard, April 21, 2014, 12:25:06 AM

Previous topic - Next topic

LemonWizard

Can someone help me?
This is probably the most terrible approach I could take.
But it seems to be the most compact so far. out of all the ones i've used/tried that didn't quite work.

PlayBASIC Code: [Select]
; PROJECT : Smart_Parse
; AUTHOR : LemonWizard
; CREATED : 4/20/2014
; EDITED : 4/20/2014
; ---------------------------------------------------------------------

BB$="10+20+100-8"

print 10+20+100-8

dim arr$(100)
dim operations$(100)



dim BB$(100)
tkns=splittoarray(BB$, "+"+"-"+"/"+"*", arr$(), 0)
found=0
for t=0 to len(BB$)
if mid$(BB$, t, 1)="+"
operations$(found)="add"
found=found+1
endif


if mid$(BB$, t, 1)="-"
operations$(found)="sub"
found=found+1
endif


if mid$(BB$, t, 1)="*"
operations$(found)="mult"
found=found+1
endif

if mid$(BB$, t, 1)="/"
operations$(found)="div"
found=found+1
endif


next t


st=0
for a=0 to tkns+found

if operations$(a)="add"

if a=0
n=val(arr$(a))+val(arr$(a+1))
st=st+2
endif


if a>0
n=n+val(arr$(a))
st=st+1
endif

endif


if operations$(a)="sub"

if a=0
n=val(arr$(a))-val(arr$(a+1))
st=st+2
endif

if a>0
n=(n-val(arr$(st)))
st=st+1
endif

endif




if operations$(a)="mult"

if a=0
n=val(arr$(a))*val(arr$(a+1))
st=st+2
endif

if a>0
n=n*val(arr$(st))
st=st+1
endif

endif

if operations$(a)="div"

if a=0
n=val(arr$(a))/val(arr$(a+1))
st=st+2
endif

if a>0
n=(n/val( arr$(st)) )
st=st+1
endif

endif

//print "step is " +str$(a)
//print "value of N is "+str$(n)
//print "the value of the last operation was " +str$( val( arr$(st)))


next a

print "the answer is " +str$( n)



waitkey
waitnokey







kevin

  
QuoteCan someone help me?

   Pretty much everything you need to know is explained in the Simple (tokenised) Math Formula Tutorial..  

   Here's a tweaked version of your code.   Be aware there's no precedence !

PlayBASIC Code: [Select]
   Constant MathOpp_ADD   =1
Constant MathOpp_SUB =2

BB$="10+20+100-8"
print 10+20+100-8

dim Numbers(100)
dim operations(100)


; ---------------------------------------------------------------------------
; split the start expression on symbols
; ---------------------------------------------------------------------------
tkns=splittoarray(BB$, "+-", numbers(), 0)

NumberOfOperators=0

; ---------------------------------------------------------------------------
; search for operators
; ---------------------------------------------------------------------------
for t=0 to len(BB$)
ThisCHR=mid(BB$, t)
if ThisChr=asc("+")
operations(NumberOfOperators)=MathOpp_ADD
NumberOfOperators++
endif

if ThisChr=asc("-")
operations(NumberOfOperators)=MathOpp_SUB
NumberOfOperators++
endif
next t


; ---------------------------------------------------------------------------
; evaulate expression
; ---------------------------------------------------------------------------

Result =Numbers(0)
NumberINdex=1

For lp=0 to NumberOfOperators-1

ThisValue=Numbers(NumberIndex)
Operation =operations(lp)

Select Operation

case MathOpp_ADD
Result +=ThisValue

print "Add "+str$(Thisvalue)
NumberIndex++

case MathOpp_SUB
Result -=ThisValue
print "Sub "+str$(Thisvalue)
NumberIndex++

default
print " unknown"

endselect


next

print "Result="+Str$(result)



sync
waitkey





kevin

 no worries, but where do i send the invoice ?   :P

LemonWizard


LemonWizard

okay so I tried to tweak the version I was working on based on what I saw you do but no matter what , mine is still throwing messed up results.... can you explain to me what you changed precisely so I can understand where I went wrong? I just wanna know if it's something to do with val or something. you know. Just trying to wrap my head around this. >.<

kevin

   it works like this   Simple (tokenised) Math Formula Tutorial.. 

  part 1)   break the input string into numbers via SplitToArray

  part 2)  skims the original string looking for operator characters, if it one exists, it puts it into an operators array (stack).   

  part 3)  grabs the first value form the numbers (our starting value), the proceeds to scan the operators array.  When it finds a known operator (Addition/subtraction) it grabs the next value from the numbers arrays and either adds or subtract it from the result...

   really you should read the tutorial, this is exactly why i wrote it !