Search code examples
javastringbuilderrpn

Spaces between elements in RPN expression java


I have a method getRPNString(), which returns Reverse Polish Notation string. I want to split this string by spacebars to calculate it. Now I can't understand how to add spacebars in my RNP string right, because it's not working with two digits numbers.

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;
    }
}

}

I tried to add spacebars by append(' ') in my StringBuilder variable out. But it' s not right with two digits. I think I totally do not understand how to make it.

For example if input is String in = "((5+3*(4+2)*12)+3)/(1+3)+5"; the out will be 5342+12+3+13+/5+, when I add spacebars to all calls out.append(' ')**out is **5 3 4 2 + * 1 2 * + 3 + 1 3 + / 5 +, so numbers like "12" became "1 2". Can you help?


Solution

  • Just change the code that you have commented out, right after Character.isDigit(op) to:

    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(' ');