Advertisement
Guest User

Untitled

a guest
Apr 24th, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.85 KB | None | 0 0
  1. package kata;
  2.  
  3. import java.util.Stack;
  4.  
  5. public class Calculator {
  6.     private Stack<String> _operators;
  7.     private Stack<Double> _operands;
  8.    
  9.     public Calculator() {
  10.         _operators = new Stack<String>();
  11.         _operands = new Stack<Double>();
  12.     }
  13.    
  14.     public void operand(Double operand) {
  15.         _operands.push(operand);
  16.     }
  17.    
  18.     public void operator(String operator) {
  19.         while (!_operators.isEmpty() && priority(_operators.peek()) >= priority(operator)){
  20.             _operands.push(evaluate());
  21.         }
  22.        
  23.         _operators.push(operator);
  24.     }
  25.    
  26.     public void leftParenthesis() {
  27.         _operators.push("(");
  28.     }
  29.    
  30.     public void rigthParenthesis() {
  31.         while (!_operators.peek().equals("(")) {   
  32.             _operands.push(evaluate());
  33.         }          
  34.     }
  35.    
  36.     private Double evaluate() {
  37.         String operator = _operators.pop();
  38.         Double operandRight = _operands.pop();
  39.         Double operandLeft = _operands.pop();
  40.         Double evaluationResult = 0.0;
  41.        
  42.         switch (operator) {
  43.             case "+":
  44.                 evaluationResult = operandLeft + operandRight;
  45.                 break;
  46.                
  47.             case "-":
  48.                 evaluationResult = operandLeft - operandRight;
  49.                 break;
  50.                
  51.             case "*":
  52.                 evaluationResult = operandLeft * operandRight;
  53.                 break;
  54.                
  55.             case "/":
  56.                 if (operandRight == 0.0) {
  57.                     throw new IllegalArgumentException();
  58.                 }
  59.                
  60.                 evaluationResult = operandLeft / operandRight;
  61.                 break;
  62.         }
  63.        
  64.         return evaluationResult;
  65.     }
  66.    
  67.     private int priority(String operator) {
  68.         if (operator.equals("(") || operator.equals(")")) {
  69.             return 0;
  70.         }
  71.         else if (operator.equals("^")) {
  72.             return 4;
  73.         }
  74.         else if (operator.equals("*") || operator.equals("/")) {
  75.             return 3;
  76.         }
  77.         else if (operator.equals("+") || operator.equals("-")) {
  78.             return 2;
  79.         }
  80.         else {
  81.             return 0;
  82.         }
  83.     }
  84.    
  85.     public Double pop() {
  86.         return _operands.pop();
  87.     }
  88.    
  89.     public void clear() {
  90.         _operators.clear();
  91.         _operands.clear();
  92.     }
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement