130 likes | 437 Views
Postfix Evaluation. public String postEval(String e) { Stack<Integer> s = new Stack<Integer>(); Token op; MyScanner scan = new MyScanner(e); while ((op = scan.next()) != null) { if (op.isInt()) s.push(op.intVal()); else if (op.isOperator() && s.size() > 1) doOp(op.operator(), s);
E N D
Postfix Evaluation public String postEval(String e) { Stack<Integer> s = new Stack<Integer>(); Token op; MyScanner scan = new MyScanner(e); while ((op = scan.next()) != null) { if (op.isInt()) s.push(op.intVal()); else if (op.isOperator() && s.size() > 1) doOp(op.operator(), s); else return "Illegal Expression"; } if (s.size() == 1) return ""+s.pop(); else return "Illegal Expression"; }
Infix to Postfix Translation public String inToPost(String e) { Stack<Character> s = new Stack<Character>(); String postfix = new String(""); Token op; MyScanner scan = new MyScanner(e); while ((op = scan.next()) != null) { if (op.isInt()) postfix = postfix + ' ' + op.intVal(); else if (op.operator() == ')') { while (!s.empty() && s.peek() != '(') postfix = postfix + ' ' + s.pop(); if (s.empty()) return "Illegal Expression"; else s.pop(); }
Infix to Postfix Translation else if (op.operator() == '(') s.push('('); else { while (!s.empty() && s.peek() != '(' && lowerPred(op.operator(), s.peek())) postfix = postfix + ' ' + s.pop(); s.push(op.operator()); } } while (!s.empty()) { char operator = s.pop(); if (operator == '(') return "Illegal Expression"; postfix = postfix + ' ' + operator; } return postfix; }
Infix Evaluation public String infixEval(String e) { Stack<Character> operators = new Stack<Character>(); Stack<Integer> operands = new Stack<Integer>(); Token op; MyScanner scan = new MyScanner(e); while ((op = scan.next()) != null) { System.out.println(op.tok); if (op.isInt()) operands.push(op.intVal()); else if (op.operator() == ')') { while (!operators.empty() && operators.peek() != '(') doOp(operators.pop(), operands); if (operators.empty()) return "Illegal Expression"; else operators.pop(); }
Infix to Postfix Translation else if (op.operator() == '(') operators.push('('); else { while (!operators.empty() && operators.peek() != '(' && lowerPred(op.operator(), operators.peek())) doOp(operators.pop(), operands); operators.push(op.operator()); } } while (!operators.empty()) { char operator = operators.pop(); if (operator == '(') return "Illegal Expression"; doOp(operator, operands); } return ""+operands.pop(); }
doOp private void doOp(char op, Stack<Integer> s) { int k = 0; int i = s.pop(); int j = s.pop(); switch (op) { case '*': k = i*j; break; case '/' : k = j/i; break; case '+' : k = i+j; break; case '-': k = j-i; break; case '%': k = j%i; break; case '^': k = (int) Math.pow(j,i); } s.push(k); }
lowerPred private boolean lowerPred(char op1, char op2) { int p1 = precedence(op1); int p2 = precedence(op2); if (p1 < p2) return true; if (p1 == p2 && op1!= '^') return true; return false; }
precedence private int precedence(char op) { switch (op) { case '+': case '-': return 1; case '*': case '/': case '%': return 2; case '^': return 3; } return 0; //should not be reached }