/* package whatever; // don't place package name! */
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
class Ideone
{
public static void main
(String[] args
) { String expression
= " 2*(5*10)+5-20/4"; try {
System.
out.
println(evaluateExpression
(expression
)); System.
out.
println("Wrong expression: " + expression
); }
}
public static int evaluateExpression
(String expression
) { // Create operandStack to store operands
Stack<Integer> operandStack = new Stack<>();
// Create operatorStack to store operators
Stack<Character> operatorStack = new Stack<>();
// Insert blanks around (, ), +, -, / and *
expression = insertBlanks(expression);
// Extract operands and operators
String[] tokens
= expression.
split(" ");
// Phase 1: Scan tokens
if (token.length() == 0) // Blank space
continue; // Back to the while loop to extract the next token
else if (token.charAt(0) == '+' || token.charAt(0) == '-') {
// Process all +, -, *, / in the top of the operator stack
while (
!operatorStack.isEmpty() && (
operatorStack.peek() == '+' ||
operatorStack.peek() == '-' ||
operatorStack.peek() == '*' ||
operatorStack.peek() == '/')) {
processOperator(operandStack, operatorStack);
}
// Push the + or - operator into the operator stack
operatorStack.push(token.charAt(0));
} else if (token.charAt(0) == '*' || token.charAt(0) == '/') {
// Process all *, / in the top the operator stack
while (
!operatorStack.isEmpty() && (
operatorStack.peek() == '*' ||
operatorStack.peek() == '/')) {
processOperator(operandStack, operatorStack);
}
// Push the * or / operator into the operator stack
operatorStack.push(token.charAt(0));
} else if (token.trim().charAt(0) == '(') {
operatorStack.push('('); // Push '(' to stack'
} else if (token.trim().charAt(0) == ')') {
// Process all the operators in the stack until seeing '('
while (operatorStack.peek() != '(') {
processOperator(operandStack, operatorStack);
}
operatorStack.pop(); // Pop the '(' symbol from the stack
} else { // An operand scanned
// Push an operand to the stack
operandStack.
push(new Integer(token
)); }
}
// Phase 2: Process all the remaining operators in the stack
while (!operatorStack.isEmpty()) {
processOperator(operandStack, operatorStack);
}
// Return the result
return operandStack.pop();
}
/**
* Process one operator: take an operator from operatorStack
* and apply it on the operands in the operandStack
*/
public static void processOperator(Stack<Integer> operandStack, Stack<Character> operatorStack) {
char op = operatorStack.pop();
int op1 = operandStack.pop();
int op2 = operandStack.pop();
if (op == '+')
operandStack.push(op2 + op1);
else if (op == '-')
operandStack.push(op2 - op1);
else if (op == '*')
operandStack.push(op2 * op1);
else if (op == '/')
operandStack.push(op2 / op1);
}
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
if (
c == '(' || c == ')' ||
c == '+' || c == '-' ||
c == '*' || c == '/') {
result += " " + c + " ";
} else
result += c;
}
return result;
}
}