C++带小数点的后缀评估
我已经实现了调车场算法(使用维基百科的代码,修改为使用 stl 堆栈/队列),但现在我想知道它将如何评估我从除法中获得的小数。 scriptasylum.com 上的 javascript 应用程序(无法链接)工作得很好,并给出了预期的输出:
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
我在这里找到了一个后缀评估的示例,但它尚未完成,并且没有考虑小数点。
这是我当前的代码: http://codepad.org/zDXnOELK
I've got the shunting yard algorithm implemented (using code from wikipedia, modified to use stl stacks/queues), but now I'm wondering 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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
最简单的事情(这就是 Javascript impl 凭借语言语义所做的事情)是将所有数字在内部表示为
double
。如果您想做一些更聪明的事情,我必须警告您,这是更多的代码和更多的硬设计决策,我建议您首先阅读方案数字塔。
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.