繁体   English   中英

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

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

我有一个工作正常的调车场算法,但我注意到一个特殊的怪癖:

1 + ( 3 * ( 4 + 5 ) )

正确解析为

1 3 4 5 + * +,

1 + (3 * (4 + 5))
失败,并解析为

  1 * + 5))+ 

我想让它正确地解析第二个问题,以便结果与第一个相同。 我该怎么做?

注意:我从Wikipedia派生了算法: http : //en.wikipedia.org/wiki/Shunting-yard_algorithm#The_algorithm_in_detail

我的算法代码是:

string switchingYard(string input)

编辑:好的,所以我有了一个主意。 , 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)

 string switchingYard(string input, int depth) 
 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)); } 
被添加到while循环的末尾。 有什么想法吗?

您的问题是解析器使用以下命令读取字符串:

io >> token;

我认为,简单的解决方案是一次只读取一个字符。 例如

char ch;

io >> ch;

我实际上会编写一个读取令牌的函数-它会知道诸如“数字序列就是数字”之类的东西,并分离出运算符,括号等。它将返回持有“类型”的对象(类或结构类型)元素”和“值”(如果有)-因此可以是“数字”和“ 4711”值,也可以是“运算符”,“ +”值。

您需要为令牌生成器提供一个“状态”,其中应包含一个“超前”字符(一个字符就足够了),以便您可以在超过数字末尾时停止,然后选择一个下次“不再是数字”。

暂无
暂无

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

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