When deciding, he used the HMO. Here is my code:

public static double eval(String inputString) { LinkedList<Double> st = new LinkedList<>(); LinkedList<Character> op = new LinkedList<>(); for (int i = 0; i < inputString.length(); i++) { char character = inputString.charAt(i); if (isDelim(character)) continue; if (character == '(') op.add('('); else if (character == ')') { while (op.getLast() != '(') processOperator(st, op.removeLast()); op.removeLast(); } else if (isOperator(character)) { while (!op.isEmpty() && priority(op.getLast()) >= priority(character)) processOperator(st, op.removeLast()); op.add(character); } else { String operand = ""; while (i < inputString.length() && Character.isDigit(inputString.charAt(i))) operand += inputString.charAt(i++); --i; st.add(Double.parseDouble(operand)); } } while (!op.isEmpty()) processOperator(st, op.removeLast()); return st.get(0); 

}

There is a problem with this type - (89 + 100), -3 + 2. Ie when minus is unary. Do not tell me how best to beat it?

    1 answer 1

    If you meet a unary minus, then subtract from zero, that is, you need to add 0 to your left of the minus, in your case there will be not - (89 + 100), -3 + 2, but 0- (89 + 100), 0-3 + 2. And where to decide yourself, either before parsing the string, place the zeros, or in the process.