quote:
Originally posted by Orkblutt
/xcalc 666/77*8
=1.08 ==> FALSE !!!
/xcalc (666/77)*8 ==> TRUE !!!
=69.20
That makes me wonder how you do the calculation and what method you use... because first of all in many language of today there is already an evaluation function for such things (dunno about C++ though). If you use that, you only need to pass the string and you would never have problems like that....
If you do seperate the numbers yourself then, again, this is some basic flaw in your algorithm, as it would be very easy to do such things in a home-made function:
* use a recursive function
* get the deepest linked parenthesis
* calculate the result of that:
"*" and "/" coming first, then "+" and "-"
Also calculate from LEFT to RIGHT!!! <== the error you made ( * or / doesn't take precedence over each other, just like + and - doesnt....)
1) 666 / 77
2) 8.64 * 8
not:
1) 77 * 8
2) 666 / 616
* update the "string"
* exit the function and go up 1 level
This is a realy realy basic way in doing it (without optimazations), and you would never have a problem with parenthesis...
note about using "-": it is best that you don't implement substractions, but only additions! If a minus is used, it should be considered part of the number, not a math-function. If you do this you also wont have problems with: "8 - 5" because your function would calculate "8 + -5", but most importantly, you can do "8 * -5" without any additional coding...
So... I'm realy interested in the method you use...
PS:
http://www.codeproject.com/cpp/FastMathParser.asp
http://www.codeproject.com/cpp/RPNcalculator.asp
http://www.programmersheaven.com/zone3/cat415/30915.htm
http://www.informit.com/articles/article.asp?p=21484
http://trubetskoy1.narod.ru/english/ppne.html <= implement this and you also have a PPN calculator...