Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java中的调车场算法,数字中有多个数字?_Java_Algorithm_Shunting Yard - Fatal编程技术网

Java中的调车场算法,数字中有多个数字?

Java中的调车场算法,数字中有多个数字?,java,algorithm,shunting-yard,Java,Algorithm,Shunting Yard,因此,我试图用Java实现调车场算法,我做得很好,但我的问题是,我希望算法计算的文本字符串包含多个数字的数字 我有一个解决这个问题的办法,但这个办法并没有那么“漂亮”。我能做的是,无论何时(在主for循环中)找到一个数字,我都会将它连接到一个字符串,并将布尔变量转换为true,让程序知道我们在一个数字内。当for循环找到一个非数字字符时,布尔变量将变为false,我们将继续正常的算法 我觉得这个解决方案不是很好,因为每当我陷入困境时,将布尔变量设置为true/false一直是我的首选解决方案 需

因此,我试图用Java实现调车场算法,我做得很好,但我的问题是,我希望算法计算的文本字符串包含多个数字的数字

我有一个解决这个问题的办法,但这个办法并没有那么“漂亮”。我能做的是,无论何时(在主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.
我突出显示了算法中出现问题的那一行(第三行)。上面写着数字,但我实现算法的方式只能捕获数字


谢谢

粘贴你的代码?你怎么看这些数字?我还没写代码。只是伪代码,我解释了一下^^伪代码是“token”,所以它假设您进行了标记化。是的,我可能错过了!谢谢,这样就解决了问题!