Note that there are some explanatory texts on larger screens.

plurals
  1. POConverting expressions from Prefix to Postfix ( Facing Problems with Precedence)
    text
    copied!<p>I have been attempting to write a code that converts Prefix expressions to Postfix expressions.</p> <p>So far here's what I have made (note that I am new and hence may not be efficient!) </p> <pre><code>/*************************************************** NOTE: This code fails to realise the precedence of parentheses and $ (exponential) expressions ****************************************************/ import java.util.Scanner; class Stack{ //Private Declarations private int top; private String a[] = new String [100]; //Public Declarations public Stack(){ top = -1; a[0] = "\0"; } public void push(String x){ if (top != 99){ a[++top] = x; } else{ System.out.println("Stack overflow"); } }; public String pop(){ if (top == -1){ System.out.println("\nStack empty!"); return "\n"; } else{ return a[top--]; } }; public int ret_top(){ return top; }; } public class Prefix2Postfix { public static void main(String[] args) { //Declaration Scanner in = new Scanner (System.in); Stack op = new Stack (); Stack sym = new Stack (); char ch; int i; String exp, str, str1, str2, str3; //Taking input from the user System.out.println("Enter the prefix expression : "); exp = in.next(); for (i=0; i&lt;exp.length(); i++){ ch = exp.charAt(i); if((ch == '+') ||(ch == '-') ||(ch == '*') ||(ch == '/') ||(ch == '$')){ str = Character.toString(ch); op.push(str); } else{ str = Character.toString(ch); sym.push(str); if (sym.ret_top() == 1){ str1 = sym.pop(); str2 = sym.pop(); str3 = op.pop(); str1 = str2.concat(str1); str1 = str1.concat(str3); sym.push(str1); } } } //Output str = sym.pop(); System.out.println("After conversion to postfix" + ": " + str); in.close(); } } </code></pre> <p>As I have already commented in the code, my code fails to realize the precedence in the case of prefix expressions.</p> <p>For example:</p> <pre><code>Infix : a + (b*c) Prefix : +a*bc Postfix : abc*+ //Expected output Output I get : ab*c+ </code></pre> <p>Is something wrong in the logic I am using or is there something I need to add? Can you please suggest some method by means of which my code can work properly? If anybody can help I will be grateful.</p> <p>NB: Our professor has advised us to write our own stack class and code. Please also note that this is kind of a homework assignment.</p> <p>Thanks in advance.</p> <hr> <p><strong>Edit:</strong> In order to make sure that my STACK works fine I wrote the following piece of code and it works perfectly alright.</p> <pre><code>import java.util.Scanner; class STACK { //Private Declarations private int top; private int elem[] = new int [100]; //Public Declarations public STACK (){ top = -1; elem[0] = 0; } public void push(int x){ if (top != 99){ elem[++top] = x; } else{ System.out.println("Stack overflow"); } }; public int pop(){ if (top == -1){ System.out.println("Stack empty!"); return 0; } else{ return elem[top--]; } }; } public class StackPushPop { public static void main(String[] args) { STACK s = new STACK(); Scanner in = new Scanner (System.in); int choice, x; do{ System.out.println("Menu Options :"); System.out.println("1 -&gt; Push an element"); System.out.println("2 -&gt; Pop an element"); System.out.println("3 -&gt; Empty complete stack"); System.out.println("Any other input for exit"); System.out.println("Your choice : "); choice = in.nextInt(); switch(choice){ case 1: System.out.println("\nEnter element : "); x = in.nextInt(); s.push(x); break; case 2: System.out.print("\nPopping element : "); x = s.pop(); if (x != 0){ System.out.println(x); } break; case 3: System.out.println("\nEmptying stack!"); x = 1; while (x!= 0){ x = s.pop(); if(x != 0){ System.out.print(x + " "); } } break; default: choice = 0; } }while (choice != 0); } } </code></pre> <hr> <p><strong>EDIT</strong> I finally managed to create a program that works perfectly fine.</p> <pre><code>import java.util.Scanner; class STACK{ private int top, MAX; private String a[] = new String [1000]; public STACK(){ top = -1; MAX = 1000; a[0] = ""; } public void push(String x){ if (top &lt;= MAX-1){ a[++top] = x; } else{ System.out.println("Stack overflow"); } }; public String pop(){ if (top == -1){ System.out.println("\nStack empty!"); return "\n"; } else{ return a[top--]; } }; public int getTop(){ return top; }; } public class Prefix2Postfix_STACK{ static boolean isOperator (char ch){ switch (ch){ case '+': case '-': case '*': case '/': case '$': return true; default : return false; } } public static void main(String[] args) { //declarations Scanner in = new Scanner (System.in); String exp; int i; STACK s = new STACK (); String exp_str[] = new String[100]; String postfix_exp = "\n"; //input System.out.println("Enter prefix expression (No spaces or brackets) : "); exp = in.next(); //create a string array of all characters but in reverse for(i=0; i&lt;=exp.length()-1; i++){ exp_str[exp.length()-1-i]=Character.toString(exp.charAt(i)); } //computing postfix: i=0; do{ if (!isOperator(exp_str[i].charAt(0))) s.push(exp_str[i]); else{ String str1 = s.pop(); String str2 = s.pop(); str1 = str1 + str2 + exp_str[i]; postfix_exp = str1; s.push(str1); } i++; }while(s.getTop()&gt;=0 &amp;&amp; i!=exp.length()); //Output System.out.println("After converting to postfix : " + postfix_exp); in.close(); } } </code></pre>
 

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