Negative numbers must be enclosed in brackets, so that it knows that it's not a minus sign. It does not use BODMAS, but brackets can simulate this.
If you use this function in your code, remember to put an extra ) at the end of the sum. (It does it in this example for you)
Compressed to only 7 lines:
input "Enter a TRICKY sum here: ",sum$ : answer# = equation(sum$+")") : print "Answer: ",answer# : wait key : end
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$ = "(" : inc count : endif : if nchar$ = ")" : dec count : endif : 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#
Uncompressed(110 lines):
input "Enter a TRICKY sum here: ",sum$
answer# = equation(sum$+")")
print "Answer: ",answer#
wait key
end
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#
I hope it's useful