java - Creating a postfix converter -


i trying create program takes in infix notation , prints out postfix form. problem output still in infix form, a+b*c output a+b*c. believe problem lies first while loop. goal in while loop if character operator while stack not empty priority method called should pop , print out operators of greater value, peek top of stack check priority. put stack.push() method outside while loop. hoped doing after priority established or if stack empty operator pushed onto stack. while second while loop should pop , print out left in stack. guys can give me , appreciate time people take guide programming greenhorn such myself. here code far:

import java.util.*;  public class postfixconverter {  public static void main(string[] args) {     scanner sc = new scanner(system.in);      stack<character> stack = new stack<character>();      system.out.print("enter infix expression: ");      string expression = new string(sc.nextline());      (int = 0; < expression.length(); i++) {         /*          * ch variable in place of expression.charat(i) seems          * idea, ch easier write          */         char ch = expression.charat(i);          /*          * hope prints out not parentheses or          * operator          */         if (ch != '(' & !(isoperator(ch)))             system.out.print(ch);          if (ch == '(') {             /*              * push '(' onto stack              */             stack.push(ch);         } else if (ch == ')') {             stack.pop();         }          /*          * if character operator, want enter while          * loop          */         if (isoperator(ch)) {             /*              * while loop should check operator priority, pop               * operator if greater operator being                            * pushed onto stack peek @ top of stack              */             while (!(stack.isempty())) {                 if (priority(ch) <= priority(stack.peek()))                     system.out.print(stack.pop());                 stack.peek();             }             stack.push(ch);          }          while (!(stack.isempty())) {             system.out.print(stack.pop());         }     } }  /*  * both of methods below use switch statement check priority ,  * whether char operator  */  public static int priority(char operator) {      switch (operator) {     case '+':         return 1;     case '-':         return 1;     case '*':         return 2;     case '/':         return 2;     case '^':         return 3;     default:         return 0;     } }  public static boolean isoperator(char operator) {     switch (operator) {     case '+':         return true;     case '-':         return true;     case '*':         return true;     case '/':         return true;     case '^':         return true;     default:         return false;      } } } 

you printing , clearing stack every character of input expression. that's not want.

you have in parsing loop:

while (!(stack.isempty())) {     system.out.print(stack.pop()); } 

having bit prints out current character (which pushed onto empty stack) clears stack (by popping until empty).

you mean move outside loop, after it.

you want parse whole expression , build stack, then print it, should in code.


Comments

Popular posts from this blog

java - WrongTypeOfReturnValue exception thrown when unit testing using mockito -

php - Magento - Deleted Base url key -

android - How to disable Button if EditText is empty ? -