# math evaluation

I need a routine to evaluate a mathematical expression that would be entered as a string in QB45. Such as 4^2+9(2x+3)^(1/2)

• : I need a routine to evaluate a mathematical expression that would be entered as a string in QB45. Such as 4^2+9(2x+3)^(1/2)

try to substitute the parts between brackets () with the evaluation

of that piece, that wouldnt be too difficult. then divide

the formula in terms that are seperated by + signs.

then do so for -'s , * and /, and ^.

you could do it recursively, like:

function eval(formula\$)

' now loop till(if) you find a + and then

' call the part in front part1\$ and the

' other part2\$

eval = eval(part1\$) + eval(part2\$)

return eval (?? i dont know if it's necessary in QB)

' if you didnt find a + then now search

' for a - .

' and so forth. at the bottom of the function

' (in other words, when there are no non-digits

' left) use val() to evaluate the string of digits

end function

so...it will probably look like this...

function to_get_the_brackets_gone

' the function itself

end function

function eval(formula\$)

if(you_find_by_looping_a_+)

eval = eval(part1) + eval(part2)

return eval

elseif(you_find_by_looping_a_-)

eval = eval(part1) - eval(part2)

return eval

elseif... ' the other signs (* / ^ etc)

else eval = string2number(formula\$)

return eval

end function

function string2number(formula\$)

if len(formula\$) = 1 then string2number = val(formula\$) else

string2number = 10 * string2number(left\$(formula\$, len(formula\$) - 1)) + val(right\$(formula\$,1))

end if

return string2number

end function

i dont know if Qb can handle recursive functions,

but probably it can.

if you have any problems, dont hesitate to plant a message on

the message board. or if you want to ask something personnally, just mail.

ps: sorry about the messy way of the program, but you'll get the big picture, i hope.

if you're lucky i'll have the program ready by monday (i like the problem and i want to see if my solution works)

• : I need a routine to evaluate a mathematical expression that would be entered as a string in QB45. Such as 4^2+9(2x+3)^(1/2)

I 've written a little bas prg to do this, but it is only operational with proper closing marks.

If you write me an e-mail I 'll try to post it back to you in a few weeks. I am interested in it too.