Here is a maths parser that I wrote:
function equation(equat$)
for d = 1 to len(equat$)
char$ = mid$(equat$,d)
if char$ = "+"
tot# = dosum(tot#,equat$,d,sumtype)
equat$ = right$(equat$,len(equat$)-d)
d = 0
sumtype = 1
endif
if char$ = "-"
tot# = dosum(tot#,equat$,d,sumtype)
equat$ = right$(equat$,len(equat$)-d)
d = 0
sumtype = 2
endif
if char$ = "*"
tot# = dosum(tot#,equat$,d,sumtype)
equat$ = right$(equat$,len(equat$)-d)
d = 0
sumtype = 3
endif
if char$ = "/"
tot# = dosum(tot#,equat$,d,sumtype)
equat$ = right$(equat$,len(equat$)-d)
d = 0
sumtype = 4
endif
if char$ = "^"
tot# = dosum(tot#,equat$,d,sumtype)
equat$ = right$(equat$,len(equat$)-d)
d = 0
sumtype = 5
endif
if char$ = "("
equat$ = right$(equat$,len(equat$)-d)
if sumtype = 0
tot# = equation(equat$)
else
if sumtype = 1
tot# = tot#+equation(equat$)
else
if sumtype = 2
tot# = tot#-equation(equat$)
else
if sumtype = 3
tot# = tot#*equation(equat$)
else
if sumtype = 4
tot# = tot#/equation(equat$)
else
tot# = tot#^equation(equat$)
endif
endif
endif
endif
endif
sumtype = 0
count = 1
while count <> 0
dec d
nchar$ = left$(equat$,1)
equat$ = right$(equat$,len(equat$)-1)
if nchar$ = "(" then inc count
if nchar$ = ")" then dec count
endwhile
endif
if char$ = ")"
tot# = dosum(tot#,equat$,d,sumtype)
equat$ = right$(equat$,len(equat$)-d)
sumtype = 0
d = 0
exitfunction tot#
endif
next d
endfunction tot#
function dosum(tot#,equat$,d,sumtype)
if sumtype = 0
tot# = tot#+val(left$(equat$,d-1))
else
if sumtype = 1
tot# = tot#+val(left$(equat$,d-1))
else
if sumtype = 2
tot# = tot#-val(left$(equat$,d-1))
else
if sumtype = 3
tot# = tot#*val(left$(equat$,d-1))
else
if sumtype = 4
tot# = tot#/val(left$(equat$,d-1))
else
tot# = tot#^val(left$(equat$,d-1))
endif
endif
endif
endif
endif
endfunction tot#
It is designed for use in a scripting system, and although it does not use BIDMAS (or BODMAS) you can still use brackets.
One thing is, that when you call it with your equation in a string, you must add an extra ) at the end, so that it knows to return.
It recognises these symbols: + - * / ^ ( )
It will return a real number.
The function you must call is:
equation(equat$)
equat$ is a string containing the equation to calculate.
If you find any bugs, plz tell me as I will be using it in my latest game.