C++ postfix evaluation with decimal points
I've got the shunting yard algorithm implemented (using code from wikipedia, modified to use stl stacks/queues), but now I'm wond开发者_运维问答ering how it's going to evaluate decimals that I get from division. The javascript app at scriptasylum.com (can't link) works just fine and gives the expected output:
Infix: 1/6*((1/4)/(1/2)+1/2)
Postfix: 16/14/12//12/+*
Eval: 0.16666666666666666 (1/6)
Infix: 0.5+1
Postfix: 0.51+
Eval: 1.5
I found one example of postfix evaluation on here, but it wasn't finished and didn't account for decimal points.
Here's my current code: http://codepad.org/zDXnOELK
The easy thing (which is what the Javascript impl is doing, by virtue of the language semantics) is to represent all numbers internally as double
.
If you want to do something cleverer, I gotta warn you that it's a lot more code and a lot more hard design decisions, and I recommend you start by reading up on the Scheme numeric tower.
精彩评论