1 / 8

Postfix Evaluation

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);

tanek-vega
Download Presentation

Postfix Evaluation

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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"; }

  2. 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(); }

  3. 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; }

  4. 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(); }

  5. 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(); }

  6. 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); }

  7. 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; }

  8. 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 }

More Related