Hello All,
I am trying to implement a program that takes a prefix expression/string and converts it into a postfix string. I looked up the general algorithim for this converson and tried to implement it myself into this program. The following code is taken from my main java file. My output is displaying only the characters, not any of the operators in their correct place.

Example, I have '+abc' and the correct output should be 'ab+c'. In this case my current output reads as 'abc'. Any help would be appreciated. Code is below:

public class PreToPostPrac {
   public static void main(String[] args) {

      String s1 = "*+abc";
      String s2 = "+-ab*-cde";
      String s3 = "+*-ab-*cd*ef-gh";
      String s4 = "-*+a-bc*+def-/gh*k1";

      // System.out.printf("Prefix: %s\nPostfix: %s\n\n", s1,preToPost(s1));
      System.out.printf("Prefix: %s\nPostfix: %s\n\n", s2,preToPost(s2));
      // System.out.printf("Prefix: %s\nPostfix: %s\n\n", s3,preToPost(s3));
      // System.out.printf("Prefix: %s\nPostfix: %s\n\n", s4,preToPost(s4));

   }

   public static String preToPost(String infix) {
      char curChar;
      int infixLength = infix.length();
      String postFix = "";

      CharStackPrac newStack = new CharStackPrac(infixLength);

      for (int x = 0; x < infix.length(); x++) {
         curChar = infix.charAt(x);
         if (isOperator(curChar) == true) {
            newStack.push(curChar);
         } else {
            if (isOperator(curChar) == false) 
              postFix = postFix + curChar;

            while (!newStack.isEmpty() && newStack.peek() == curChar) {
               newStack.pop();
               postFix = postFix + newStack.pop();
               newStack.pop();
            }
          newStack.push(curChar);
         }
      }
    return postFix;
   }               

   public static boolean isOperator(char Op) {
      if (Op == '+' || Op == '-' || Op == '*' || Op  == '/') {
         return true;
      } else {
         return false;
      }
   }

}

Recommended Answers

All 6 Replies

I'm a bit at a loss here..

I have '+abc' and the correct output should be 'ab+c'.

If it's supposed to become a postfix, shouldn't it become 'abc+' ?
Can you please explain your logic more clearly?

commented: Given prefix expression: '*+abc' which should be converted to 'ab+c*' +0

In postfix notation I thought that the operators would be placed after the operands .. In my general summary the asterik or multiplication symbol in this case did not print :(

I still don't really see your logic. Anyway, all we can do is guess, since you don't show all the classes used.
What is the implementation of CharStackPrac, for instance?

Just a few pointers to make your code 'shorter', and easier to maintain:

public static boolean isOperator(char Op) {
      if (Op == '+' || Op == '-' || Op == '*' || Op  == '/') {
         return true;
      } else {
         return false;
      }
   }

can be replaced by:

public static boolean isOperator(char op){ // also look at naming conventions
// don't start the names of variables with upper case
return op == '+' || op == '-' || op == '*' || op == '/';
}

and the calls to this method:

if (isOperator(curChar) == false)

and

if (isOperator(curChar) == true)

can be written as

if (isOperator(curChar) ) -> true
if (!isOperator(curChar) ) -> false

commented: Gotcha, I will reply back with my CharStack java file that has my methods for pop, peek, etc. Also is it possible to edit your original post .? +0

Here is my code from my CharStackPac java file:

public class CharStackPrac {

   private char[] charStack;
   private int top;
   private int maxSize;

   public CharStackPrac(int size) {
       maxSize = size;
       charStack = new char[maxSize];
       top = -1; // no char items in the array
   }

   public void push(char character) {
       charStack[++top] = character;   
    }


   public char pop() {
       return charStack[top--]; 
   }


   public char peek() {
       return charStack[top];
   }

   public boolean isEmpty() {
       return top == -1;

   }     
}

`

Then I still don't get the logic. when should it be what (not just in one case, but for all cases).
As for editing the posts, that is only possible for a limited amount of time after posting.

You also shouldn't use the comments on the vote to reply to a post. Use them to point out why you like or don't like a reply.

After looking at my algorithim again I found that I should have pushed the character '#' instead pushing the current character from my string. This was also the case in my while loop .. but I should have checked to see if "newStack.peek() == '#'". Revised/working code is below:

public class PreToPostPrac {
   public static void main(String[] args) {

      String s1 = "*+abc";
      String s2 = "+-ab*-cde";
      String s3 = "+*-ab-*cd*ef-gh";
      String s4 = "-*+a-bc*+def-/gh*k1";

      System.out.printf("Prefix: %s\nPostfix: %s\n\n", s1,preToPost(s1));
      System.out.printf("Prefix: %s\nPostfix: %s\n\n", s2,preToPost(s2));
      System.out.printf("Prefix: %s\nPostfix: %s\n\n", s3,preToPost(s3));
      System.out.printf("Prefix: %s\nPostfix: %s\n\n", s4,preToPost(s4));

   }

   public static String preToPost(String infix) {
      char curChar;
      int infixLength = infix.length();
      String postFix = "";

      CharStackPrac newStack = new CharStackPrac(infixLength);

      for (int x = 0; x < infix.length(); x++) {
         curChar = infix.charAt(x);
         if (isOperator(curChar)) {
            newStack.push(curChar);
         } else {
            if (!isOperator(curChar)) 
              postFix = postFix + curChar;

            while (!newStack.isEmpty() && newStack.peek() == '#') {
               newStack.pop();
               postFix = postFix + newStack.pop();
               // newStack.pop();
            }
          newStack.push('#');
         }
      }
    return postFix;
   }               

   public static boolean isOperator(char op) {
      if (op == '+' || op == '-' || op == '*' || op  == '/') {
         return true;
      } else {
         return false;
      }
   }

}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.