Java中的分流码算法,一个数字中的多位数?

时间:2017-06-24 20:38:58

标签: java algorithm shunting-yard

所以我正在尝试用Java实现Shunting-yard算法,我做得很好但我的问题是我希望算法评估的文本字符串包含多个数字数字。

我有解决这个问题但解决方案不是那么“漂亮”。我能做的是每当找到一个数字(在主for循环中)我将它连接到一个字符串并将一个布尔变量转换为true,让程序知道我们在一个数字内。当for循环找到一个非数字字符时,布尔变量将变为false,我们继续正常算法。

我发现这个解决方案并不是很好,因为将布尔变量设置为true / false一直都是我的解决方案,因为每当我遇到困难时都是如此。

请帮忙吗?

    while there are tokens to be read:
    read a token.
    **if the token is a number**, then push it to the output queue.
    if the token is an operator, then:
        while there is an operator at the top of the operator stack with
            greater than or equal to precedence:
                pop operators from the operator stack, onto the output queue;
        push the read operator onto the operator stack.
    if the token is a left bracket (i.e. "("), then:
        push it onto the operator stack.
    if the token is a right bracket (i.e. ")"), then:
        while the operator at the top of the operator stack is not a left bracket:
            pop operators from the operator stack onto the output queue.
        pop the left bracket from the stack.
        /* if the stack runs out without finding a left bracket, then there are
        mismatched parentheses. */
if there are no more tokens to read:
    while there are still operator tokens on the stack:
        /* if the operator token on the top of the stack is a bracket, then
        there are mismatched parentheses. */
        pop the operator onto the output queue.
exit.

我已经突出显示了算法中出现问题的行(第3行)。它说数字,但我实现算法的方式我只能捕获数字。

谢谢!

0 个答案:

没有答案