简体   繁体   English

修改调车场算法(c ++)

[英]Modifying the Shunting Yard Algorithm (c++)

I have a shunting yard algorithm in proper working order, but I noticed a special quirk: 我有一个工作正常的调车场算法,但我注意到一个特殊的怪癖:

1 + ( 3 * ( 4 + 5 ) )

parses correctly to 正确解析为

1 3 4 5 + * +,

but

1 + (3 * (4 + 5))
fails, and parses to 失败,并解析为

 1 * + 5)) + 1 * + 5))+ 

I want to get it to parse the second problem properly, so that the result is the same as the first. 我想让它正确地解析第二个问题,以便结果与第一个相同。 How can I accomplish this? 我该怎么做?

Notes: I derived my algorithm from wikipedia: http://en.wikipedia.org/wiki/Shunting-yard_algorithm#The_algorithm_in_detail 注意:我从Wikipedia派生了算法: http : //en.wikipedia.org/wiki/Shunting-yard_algorithm#The_algorithm_in_detail

My algorithm code is: 我的算法代码是:

string switchingYard(string input)

Edit: Ok, so I just got an idea. 编辑:好的,所以我有了一个主意。 So when the parser encounters the '(3' token, it would otherwise treat the two characters as one, and discard the whole thing, but what if I called the function recursively , passing in the substring of the input string which starts at the '3' character? I would then only need to add the shunted string to the output vector, and call ignore on the stringstream! I'm talking about making these changes: 因此,当解析器遇到'(3'令牌时,它将以其他方式将两个字符视为一个,并丢弃整个内容,但是如果我递归地调用该函数,传入以'开始的输入字符串的子字符串,该怎么办? 3'字符?然后我只需要将已分流的字符串添加到输出向量,并在字符串流上调用ignore!我正在谈论进行以下更改:

string switchingYard(string input, int depth)

becomes

 string switchingYard(string input, int depth) 
and
 if((token[0] == '(' || token[0] == ')') && (isdigit(token[1]) || token[1] == '.' || isOperator(token[1])) { string shunted_recur = out.push_back(switchingYard(input.substr(io.tellg()+1),depth+1)); } 
gets added to the end of the while loop. 被添加到while循环的末尾。 Thoughts? 有什么想法吗?

Your problem is that the parser reads strings with: 您的问题是解析器使用以下命令读取字符串:

io >> token;

The simple solution, in my opinion, would be to simply read a character at a time. 我认为,简单的解决方案是一次只读取一个字符。 Eg 例如

char ch;

io >> ch;

I would actually write a function that reads a token - it would know things like "a sequence of digits is a number" and separate out operators, parenthesis, etc. It would return an object (class or structure type) that holds an "type of element" and "value (if relevant) - so it could be type "number" and value "4711", or type "operator", value "+". 我实际上会编写一个读取令牌的函数-它会知道诸如“数字序列就是数字”之类的东西,并分离出运算符,括号等。它将返回持有“类型”的对象(类或结构类型)元素”和“值”(如果有)-因此可以是“数字”和“ 4711”值,也可以是“运算符”,“ +”值。

You will need a "state" for your tokeniser, which will include a "lookahead" character (one char should be enough), so that you can stop when you have gone past the end of a number, and then pick up the character that "stopped being a number" next time around. 您需要为令牌生成器提供一个“状态”,其中应包含一个“超前”字符(一个字符就足够了),以便您可以在超过数字末尾时停止,然后选择一个下次“不再是数字”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM