Note that there are some explanatory texts on larger screens.

plurals
  1. POConverting infix expressions to postfix expressions
    primarykey
    data
    text
    <p>I have a problem i can't find the wrong in this function it sometimes do well with any input but it cached when there is parentheses in the input {i want to know where is the wrong in this code and how to fix it , and Is there another better way instead this way}</p> <pre><code> public static String Converting_infix_expressions_to_postfix_expressions(String infix) throws Exception{ StringTokenizer st = new StringTokenizer(infix); int numOF_tokens = st.countTokens(); String postfix = "" ; for (int i = 1; i &lt;= numOF_tokens; i++) { String term = st.nextToken(); try { // if it is an Float there is no problem will happen float x = Float.parseFloat(term); postfix += x +" " ; System.out.println("term is number " + term); } catch (Exception e) { System.out.println("term is symbol " + term); if(stack.isEmpty()) stack.push(term); else if(term == "(") stack.push(term); else if(term == ")"){ while((String)stack.peek() != "(") postfix += stack.pop() +" "; stack.pop(); } else{ int x = 0,y = 0; switch(term){ case "+": x = 1; break; case "-": x = 1; break; case "*": x = 2; break; case "/": x = 2; break; } switch((String)stack.peek()){ case "+": y = 1; break; case "-": y = 1; break; case "*": y = 2; break; case "/": y = 2; break; } if(x &gt; y) stack.push(term); else { int x1 = x , y1 = y; boolean puchedBefore = false; while(x1 &lt;= y1){ postfix += stack.pop() +" "; if(stack.isEmpty() || stack.peek() == "(" ){ stack.push(term); puchedBefore = true; break; } else{ switch(term){ case "+": x1 = 1; break; case "-": x1 = 1; break; case "*": x1 = 2; break; case "/": x1 = 2; break; } switch((String)stack.peek()){ case "+": y1 = 1; break; case "-": y1 = 1; break; case "*": y1 = 2; break; case "/": y1 = 2; break; } } } if(!puchedBefore) stack.push(term); } } } } while(!stack.isEmpty()){ postfix += stack.pop() +" "; } System.out.println("The postfix expression is : " + postfix); return postfix; } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload