Advertisement
lpuarmy

Postfixeval | Stack Java

Dec 3rd, 2013
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.61 KB | None | 0 0
  1. package lat04;
  2. import java.util.Stack;
  3.  
  4. public class PostfixEval {
  5.     private String postfixExpression;
  6.     private Stack<Integer> operandStack;
  7.    
  8.     public PostfixEval() {
  9.         postfixExpression = new String();
  10.         operandStack = new Stack<>();
  11.     }
  12.     public void setPostfixExp(String postfixExp) {
  13.         postfixExpression = postfixExp;
  14.     }
  15.     public int evaluate(){
  16.         for (int i = 0; i < postfixExpression.length(); i++){
  17.             char ch = postfixExpression.charAt(i); // get the current char
  18.  
  19.             if (Character.isDigit(ch))
  20.                 operandStack.push(ch - '0'); // put integer on stack
  21.             else if (isOperator(ch)) {
  22.                 int right = getOperand(); // pop right operand
  23.                 int left = getOperand();  // pop left operand
  24.                 int result = compute(left, right, ch);
  25.                 operandStack.push(result); // push result
  26.             }
  27.             else if (!Character.isWhitespace(ch)) {    // other  chars can be whitespace
  28.                 throw new ArithmeticException("PostfixEval: Improper char");
  29.             }
  30.         }
  31.  
  32.         int exprValue = ((Integer) operandStack.pop()).intValue(); // pop expr value
  33.         if (!operandStack.isEmpty()) // stack should be empty
  34.             throw new ArithmeticException("PostfixEval: Too many operands");
  35.         return exprValue;
  36.     }
  37.     private boolean isOperator(char ch) {
  38.         return ch == '+' || ch == '-' || ch == '*' || ch == '%' || ch == '/' || ch == '^';
  39.     }
  40.     private int getOperand() {
  41.         if(operandStack.isEmpty())
  42.             throw new ArithmeticException("PostfixEval: Too many operators");
  43.         return operandStack.pop();
  44.     }
  45.     public int compute(int left, int right, char op) {
  46.         int value = 0;
  47.         // evaluate "left op right"
  48.         switch (op) {
  49.             case '+':
  50.                 value = left + right; break;
  51.             case '-':
  52.                 value = left - right; break;
  53.             case '*':
  54.                 value = left * right; break;
  55.             case '^':
  56.                 value = (int)Math.pow(left, right);
  57.                 break;
  58.             case '%':
  59.                 if (right == 0) {
  60.                     throw new ArithmeticException("PostfixEval: divide by 0");
  61.                 }
  62.                 value = left % right;
  63.                 break;
  64.             case '/':
  65.                 if (right == 0) {
  66.                     throw new ArithmeticException("PostfixEval: divide by 0");
  67.                 }
  68.                 value = left / right;
  69.                 break;
  70.         }
  71.         return value;
  72.     }
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement