InfixToPostfix - } } if(!myStack.empty()){ //Character d =...

Info iconThis preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon
Sheet1 Page 1 /*16 points 5. Write an InfixToPostfix class to implement Infix to Postfix conversion as described in the textbook. Operands are single letters and operators are only +, *, and parentheses. */ import java.util.Stack public class InfixToPostfix { public InfixToPostfix(String infix){ System.out.println("Infix Form: " + infix) myStack = new Stack() postEq = "PostFix Form: " char next for(int n = 0 next = infix.charAt(n) //System.out.println(" Character at substring[" + n + "] is " + next ) this.pushChar(next) } while(!myStack.empty()){ Character t = myStack.pop() if(t!='(') postEq+=t //System.out.println(" Character popped on Operator Stack is " + t ) } System.out.println(postEq) } public void pushChar(char a){ if((a == ')' || a == '(' || a == '+' || a == '-') || a == '*'){ pushOp(a) } else{ postEq+=a } } public void pushOp(char c){ Character p = new Character(c) if(!(myStack.empty() ||myStack.peek()== '(' )){ if(c == ')'){ while(myStack.peek()!= '(' ){ Character t = myStack.pop() if(t != '('){ postEq+=t
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 2
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 4
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: } } if(!myStack.empty()){ //Character d = myStack.pop() //this.pushOp(d) //System.out.println(d) } } Sheet1 Page 2 if(c == '+' || c == '-'){ while( !(myStack.empty()) && myStack.peek() != '(' ){ Character t = myStack.pop() if(t != '('){ postEq+=t } } } if(c == '*' || c == '/'){ while( (!myStack.empty()) && myStack.peek()!= '+' && myStack.peek()!= '-' && myStack.peek()!= ')' && myStack.peek()!= '(' ) Character t = myStack.pop() if(t != '('){ postEq+=t } } } } if(p != ')') myStack.push(p) //System.out.println(" Character pushed on Operator Stack is " + p ) } public Stack<Character> myStack public String postEq public static void main( String [ ] args ){ InfixToPostfix test = new InfixToPostfix("4+3*(4-6)") InfixToPostfix test2 = new InfixToPostfix("w-(k*m)") InfixToPostfix test3 = new InfixToPostfix("w-(4+k*l)") InfixToPostfix test4 = new InfixToPostfix("(j+p)*w*(k-m)+9") } } Sheet1 Page 3 n < infix.length() n++){ Sheet1 Page 4...
View Full Document

This note was uploaded on 09/16/2009 for the course CS 3345 taught by Professor Ozbirn during the Spring '08 term at University of Texas at Dallas, Richardson.

Page1 / 4

InfixToPostfix - } } if(!myStack.empty()){ //Character d =...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online