Shunting-yard algorithm in c++

﹥>﹥吖頭↗ 提交于 2019-12-06 01:48:57

I personally think you have to study harder about Shunting-yard algorithm

Because you said that the output is like "4 9 * 3 +" , but what I have read about the algorithm and the stack operation, it should be (like " 9 4 * 3 +")

The important issue is that after classifying the number and operators, pop out all from the operator stack and push into the number stack with respect to the set conditions for which operator to be pop out

My suggestion is that you go straight to the relevant Wiki pages that describe

  1. Shunting Yard Algorithm
  2. Reverse Polish Notation

I have implemented the Shunting Yard algorithm in both Java and C++ and found the Wiki pages excellent and a great source of help. They are in sufficient detail so as to enable you to implement the algorithm step by step in whatever programming language you prefer.

Another suggestion: become reasonably familiar with the practical use of stacks and queues, as these are used all over the place in these algorithms.

Please see this blog posting for some C++ and Java implementations of the said Shunting Yard algorithm.

It also contains a further section (in progress) should you wish to include other mathematical operators (sin, cos, log etc) and more complicated expressions and sub-expressions.

Here is (the last version of) the solution. On some step it use Dijkstra's shunting yard algorithm (at the end of traverse() member function output_ member contains reverse polish notation form of the input_ expression, if we traverse it in right way).

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!