java - Stacks convertion from postfix to infix -


hello practicing stacks on java , trying problem concerning stacks. trying write method takes postfix notation , converts infix. have far:

` public void convertion() {         stack<integer> stack;          // evaluating expression.         stack = new stack<integer>();  // make new, empty stack.          scanner scan = new scanner(postfix);          int t1, t2 = 0;     //operands          boolean check = false;           while (scan.hasnext() && !check) {             if (scan.hasnextint()) {                 int operand = scan.nextint();                 stack.push(operand);             } else {                 char operator = scan.next().charat(0);                 try {                          while(stack.)                   } catch (emptystackexception e) {                     answer = "malformed postfix expression";                     check = true;                 }              }         }         scan.close();         try {             answer = "" + stack.pop();         } catch (emptystackexception e) {             answer = "malformed postfix expression";         }     } ` 

the part im having trouble on should put on try part. im pushing numbers find stack, once find operator, how merge 2 operands , operator.

thanks.

you want pop top 2 stack elements, perform appropriate operation on them , push result:

try {     int o1 = stack.pop().intvalue();     int o2 = stack.pop().intvalue();     switch (operator) {         case '+': stack.push(new integer(o1 + o2));                   break;         case '-': stack.push(new integer(o1 - o2));                   break;         ...     } } catch (emptystackexception e) {     ... 

Comments

Popular posts from this blog

monitor web browser programmatically in Android? -

Shrink a YouTube video to responsive width -

wpf - PdfWriter.GetInstance throws System.NullReferenceException -