SHARE
TWEET

Untitled

a guest Aug 21st, 2019 71 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. public int calculate(String s) {
  2.         if (s == null || s.length() == 0) {
  3.             throw new IllegalArgumentException("Invalid input");
  4.         }
  5.  
  6.         int i = 0;
  7.         Stack<Integer> operands = new Stack<>();
  8.         Stack<Character> operators = new Stack<>();
  9.         StringBuilder number = new StringBuilder();
  10.  
  11.         while (i < s.length()) {
  12.             char c = s.charAt(i);
  13.             if (c == ' ') {
  14.                 i++;
  15.                 continue;
  16.             }
  17.  
  18.             if (Character.isDigit(c)) {
  19.                 number.append(c);
  20.             } else if (c == '(') {
  21.                 operators.push(c);
  22.             } else if (c == ')') {
  23.                 // push the current number to the operands stack
  24.                 if (number.length() != 0) {
  25.                     operands.push(Integer.parseInt(number.toString()));
  26.                     number = new StringBuilder();
  27.                 }
  28.  
  29.                 if (!operators.isEmpty() && operators.peek() != '(') {
  30.                     char op = operators.pop();
  31.                     operands.push(this.calculateValue(operands, op));
  32.                 }
  33.                 if (!operators.isEmpty()) { // pop the left bracket
  34.                     operators.pop();
  35.                 }
  36.             } else if (c == '+' || c == '-') {
  37.                 if (number.length() != 0) {
  38.                     operands.push(Integer.parseInt(number.toString()));
  39.                     number = new StringBuilder();
  40.                 }
  41.                
  42.                 if (!operators.isEmpty() && operators.peek() != '(') {
  43.                     operands.push(this.calculateValue(operands, operators.pop()));
  44.                 }
  45.  
  46.                 operators.push(c);
  47.             }
  48.             i++;
  49.         }
  50.  
  51.  
  52.         if (number.length() != 0) {
  53.             operands.push(Integer.parseInt(number.toString()));
  54.         }
  55.  
  56.         // need this for the 1+1 case, don't need a while since we are only one step away
  57.         if (!operators.isEmpty()) {
  58.             operands.push(this.calculateValue(operands, operators.pop()));
  59.         }
  60.  
  61.         return operands.pop();
  62.     }
  63.  
  64.  
  65.     private int calculateValue(Stack<Integer> operands, char operator) {
  66.         // Pay attention to the stack order
  67.         int o2 = operands.pop();
  68.         int o1 = operands.pop();
  69.  
  70.         if (operator == '+') {
  71.             return o1 + o2;
  72.         } else if (operator == '-') {
  73.             return o1 - o2;
  74.         } else {
  75.             throw new IllegalArgumentException("invalid operator!");
  76.         }
  77.     }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top