简体   繁体   English

Java分流场算法内存不足错误

[英]Java Shunting Yard Algorithm OutOfMemory Error

I want to convert from infix to postfix notation, therefore i would like to use the Shunting Yard Algorithm. 我想从infix转换为postfix表示法,因此我想使用Shunting Yard算法。 I parse the String token for token and use a Stack to store Operators. 我将String令牌解析为令牌,并使用堆栈来存储Operators。 But when i run my program i got following Exception: 但是当我运行程序时,出现以下异常:

Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
at java.util.Arrays.copyOf(Unknown Source)
at java.lang.AbstractStringBuilder.ensureCapacityInternal(Unknown Source)
at java.lang.AbstractStringBuilder.append(Unknown Source)
at java.lang.StringBuilder.append(Unknown Source)
at  ShuntingYardAlgorithm.toPostfix( ShuntingYardAlgorithm.java:46)
at  ShuntingYardAlgorithm.main( ShuntingYardAlgorithm.java:59)

Edit: It seems that my method OperantValues always return -1, but i dont' know how to fix it. 编辑:似乎我的方法OperantValues总是返回-1,但是我不知道如何解决它。

Here is my Code: 这是我的代码:

public class ShuntingYardAlgorithm {

static int OperantValues(char c) {

    switch (c) {
    case '+':
        return 1;
    case '-':
        return 1;
    case '*':
        return 2;
    case '/':
        return 2;
    }
    return -1;
}

public static String toPostfix(String expr) {

    Stack<Character> stack = new Stack<Character>();
    StringBuilder sb = new StringBuilder();

    for (int i = 0; i < expr.length(); i++) {

        char c = expr.charAt(i);

        if (Character.isDigit(c)) {
            sb.append(" " + c);
        }

        if (c == '(') {
            stack.push('(');
        }

        else if (c == ')') {

            while (!stack.isEmpty() && stack.peek() != '(')
                sb.append("" + stack.pop());

            stack.pop();

        } else {// Operator

            while (!stack.isEmpty() && OperantValues(c) <= 
 OperantValues(stack.peek()))
                sb.append(c);
            stack.push(c);
        }
    }

    while (!stack.isEmpty())
        sb.append(stack.pop());
    return sb.toString();
 }

public static void main(String[] args) {

    String expr = "(2+3)*((4+7)*3) ";
    System.out.println(toPostfix(expr));
 }

}

Why i got this Exception? 为什么我收到此异常?

Any help would be appreciated. 任何帮助,将不胜感激。

Two issues with this code: 此代码有两个问题:

  1. Execution should only enter one branch of the if -chain, so there is an else missing: 执行应该只进入的一个分支if α链,所以有一个else丢失:

     if (Character.isDigit(c)) { sb.append(" " + c); } else // insert else here if (c == '(') { stack.push('('); } else if ... 
  2. The code does not handle spaces. 该代码不处理空格。 To do this, add an extra clause: 为此,添加一个额外的子句:

      ... stack.pop(); } else if (OperantValues(c) > -1) { // Operator while (!stack.isEmpty() && OperantValues(c) <= OperantValues(stack.peek())) sb.append(c); stack.push(c); } else // other characters including space { continue; // we'll ignore them for now } 

    This was also the cause of OperantValues(c) "always" returning -1 , because the code got stuck on the space at the end of your input string. 这也是OperantValues(c) “总是”返回-1原因,因为代码被卡在输入字符串末尾的空格中。

Applying the above fixes does the trick. 应用上述修补程序可以解决问题。 The output becomes: 输出变为:

 2 3+ 4 7+ 3**

As expected. 如预期的那样。

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

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