fork(1) download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone
  9. {
  10. public static void main(String[] args) {
  11. String expression = " 2*(5*10)+5-20/4";
  12. try {
  13. System.out.println(evaluateExpression(expression));
  14. } catch (Exception ex) {
  15. System.out.println("Wrong expression: " + expression);
  16. }
  17. }
  18.  
  19. public static int evaluateExpression(String expression) {
  20. // Create operandStack to store operands
  21. Stack<Integer> operandStack = new Stack<>();
  22.  
  23. // Create operatorStack to store operators
  24. Stack<Character> operatorStack = new Stack<>();
  25.  
  26. // Insert blanks around (, ), +, -, / and *
  27. expression = insertBlanks(expression);
  28.  
  29. // Extract operands and operators
  30. String[] tokens = expression.split(" ");
  31.  
  32. // Phase 1: Scan tokens
  33. for (String token : tokens) {
  34. if (token.length() == 0) // Blank space
  35. continue; // Back to the while loop to extract the next token
  36. else if (token.charAt(0) == '+' || token.charAt(0) == '-') {
  37. // Process all +, -, *, / in the top of the operator stack
  38. while (
  39. !operatorStack.isEmpty() && (
  40. operatorStack.peek() == '+' ||
  41. operatorStack.peek() == '-' ||
  42. operatorStack.peek() == '*' ||
  43. operatorStack.peek() == '/')) {
  44. processOperator(operandStack, operatorStack);
  45. }
  46.  
  47. // Push the + or - operator into the operator stack
  48. operatorStack.push(token.charAt(0));
  49. } else if (token.charAt(0) == '*' || token.charAt(0) == '/') {
  50. // Process all *, / in the top the operator stack
  51. while (
  52. !operatorStack.isEmpty() && (
  53. operatorStack.peek() == '*' ||
  54. operatorStack.peek() == '/')) {
  55. processOperator(operandStack, operatorStack);
  56. }
  57.  
  58. // Push the * or / operator into the operator stack
  59. operatorStack.push(token.charAt(0));
  60. } else if (token.trim().charAt(0) == '(') {
  61. operatorStack.push('('); // Push '(' to stack'
  62. } else if (token.trim().charAt(0) == ')') {
  63. // Process all the operators in the stack until seeing '('
  64. while (operatorStack.peek() != '(') {
  65. processOperator(operandStack, operatorStack);
  66. }
  67.  
  68. operatorStack.pop(); // Pop the '(' symbol from the stack
  69. } else { // An operand scanned
  70. // Push an operand to the stack
  71. operandStack.push(new Integer(token));
  72. }
  73. }
  74.  
  75. // Phase 2: Process all the remaining operators in the stack
  76. while (!operatorStack.isEmpty()) {
  77. processOperator(operandStack, operatorStack);
  78. }
  79.  
  80. // Return the result
  81. return operandStack.pop();
  82. }
  83.  
  84. /**
  85.   * Process one operator: take an operator from operatorStack
  86.   * and apply it on the operands in the operandStack
  87.   */
  88. public static void processOperator(Stack<Integer> operandStack, Stack<Character> operatorStack) {
  89. char op = operatorStack.pop();
  90. int op1 = operandStack.pop();
  91. int op2 = operandStack.pop();
  92.  
  93. if (op == '+')
  94. operandStack.push(op2 + op1);
  95. else if (op == '-')
  96. operandStack.push(op2 - op1);
  97. else if (op == '*')
  98. operandStack.push(op2 * op1);
  99. else if (op == '/')
  100. operandStack.push(op2 / op1);
  101. }
  102.  
  103. public static String insertBlanks(String s) {
  104. String result = "";
  105.  
  106. for (int i = 0; i < s.length(); i++) {
  107. char c = s.charAt(i);
  108. if (
  109. c == '(' || c == ')' ||
  110. c == '+' || c == '-' ||
  111. c == '*' || c == '/') {
  112. result += " " + c + " ";
  113. } else
  114. result += c;
  115. }
  116.  
  117. return result;
  118. }
  119. }
Success #stdin #stdout 0.09s 27644KB
stdin
Standard input is empty
stdout
100