RPN表达式java中元素之间的空格

时间:2015-02-24 19:25:44

标签: java stringbuilder rpn

我有一个方法 getRPNString(),它返回反向波兰表示法字符串。我想用空格键分割这个字符串来计算它。现在我无法理解如何在我的RNP字符串中添加空格键,因为它不能使用两位数字。

public class Calc1 {

public static void main(String[] args) {

    String in = "((5+3*(4+2)*12)+3)/(1+3)+5";
    String out = getRPNString(in);
    System.out.println(out);

}

private static String getRPNString(String in) {
    LinkedList<Character> oplist = new LinkedList<>();
    StringBuilder out = new StringBuilder();

    for (int i = 0; i < in.length(); i++) {
        char op = in.charAt(i);
        if (op == ')') {
            while (oplist.getLast() != '(') {
                out.append(oplist.removeLast());
            }
            oplist.removeLast();
        }

        if (Character.isDigit(op)) {

            out.append(op);

            /*int j = i + 1;
            for (; j < in.length(); j++) {
                if (!Character.isDigit(j)) {
                    break;
                }
                i++;
            }
            out.append(in.substring(i, j));*/

        }

        if (op == '(') {
            oplist.add(op);
        }

        if (isOperator(op)) {
            if (oplist.isEmpty()) {
                oplist.add(op);
            } else {
                int priority = getPriority(op);
                if (priority > getPriority(oplist.getLast())) {
                    oplist.add(op);
                } else {
                    while (!oplist.isEmpty()
                            && priority <= getPriority(oplist.getLast())) {
                        out.append(oplist.removeLast());
                    }
                    oplist.add(op);
                }
            }
        }

    }

    while (!oplist.isEmpty()) {
        out.append(oplist.removeLast());
    }

    return out.toString();
}

private static boolean isOperator(char c) {
    return c == '+' || c == '-' || c == '*' || c == '/' || c == '%';
}

private static int getPriority(char op) {
    switch (op) {

    case '*':
    case '/':
        return 3;

    case '+':
    case '-':
        return 2;

    case '(':
        return 1;

    default:
        return -1;
    }
}

}

我尝试通过在StringBuilder变量中追加('')来添加空格键。但两位数字不对。我想我完全不明白如何制作它。

例如,如果输入是String in =“((5 + 3 *(4 + 2)* 12)+3)/(1 + 3)+5”;我将 5342 + 12 + 3 + 13 + / 5 + ,当我向所有来电添加空格键时 out.append('')** out是** 5 3 4 2 + * 1 2 * + 3 + 1 3 + / 5 + ,所以像“12”的数字变成“1 2”。 你能帮忙吗?

2 个答案:

答案 0 :(得分:0)

只需在Character.isDigit(op)之后更改已注释掉的代码:

int j = i + 1;
int oldI = i;//this is so you save the old value
for (; j < in.length(); j++) {
    if (!Character.isDigit(in.charAt(j))) {
        break;
    }
    i++;
}
out.append(in.substring(oldI, j));
out.append(' ');

答案 1 :(得分:0)

我改变了方法,现在工作正常。我在写作时弄错了我的错误 !Character.isDigit(j)但需要!Character.isDigit( in.charAt(j))。

private static String getRPNString(String in) {
    LinkedList<Character> oplist = new LinkedList<>();
    StringBuilder out = new StringBuilder();

    for (int i = 0; i < in.length(); i++) {
        char op = in.charAt(i);
        if (op == ')') {
            while (oplist.getLast() != '(') {
                out.append(oplist.removeLast()).append(' ');
            }
            oplist.removeLast();
        }

        if (Character.isDigit(op)) {

            int j = i + 1;
            int oldI = i;//this is so you save the old value
            for (; j < in.length(); j++) {
                if (!Character.isDigit(in.charAt(j))) {
                    break;
                }

                i++;
            }

            out.append(in.substring(oldI, j));
            out.append(' ');

        }

        if (op == '(') {
            oplist.add(op);
        }

        if (isOperator(op)) {
            if (oplist.isEmpty()) {
                oplist.add(op);
            } else {
                int priority = getPriority(op);
                if (priority > getPriority(oplist.getLast())) {
                    oplist.add(op);
                } else {
                    while (!oplist.isEmpty()
                            && priority <= getPriority(oplist.getLast())) {
                        out.append(oplist.removeLast()).append(' ');
                    }
                    oplist.add(op);
                }
            }
        }

    }

    while (!oplist.isEmpty()) {
        out.append(oplist.removeLast()).append(' ');
    }

    return out.toString();
}

现在它产生正确的表达。 测试:输入:((5 + 3 *(4 + 2)* 12)+3)/(1 + 3)+5 输出: 5 3 4 2 + * 12 * + 3 + 1 3 + / 5 +