Question

Write a java program that converts a prefix expression to a postfix expression. Input The input...

Write a java program that converts a prefix expression to a postfix expression.

Input

The input file "Prefix.in" contains a series of error-free, simple arithmetic expressions in prefix notation. There is one expression per line. The expressions are made up of two types of symbols: (1) single-letter operands and (2) the operators +, -, *, and /. There may be zero, one, or more blanks between symbols.

Output

All output should be written to the output file, "Postfix.out". Each prefix expression from the input should be echo-printed, with an appropriate label. After your program converts the input expression to its postfix equivalent, the postfix expression should be printed to the output file, with an appropriate label.

Sample:

Prefix:         + * A B / C D

Postfix:        A B * C D / +

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Program:

import java.io.*;
import java.util.Stack;
import java.util.Scanner;

//class PrefixToPostfix
public class PrefixToPostfix
{
   //method to check operator
   public static boolean isOperator(char c)
   {
       switch(c)
       {
           case '+': case '-': case '/': case '*':
               return true;
       }
       return false;
   }
  
   //method to convert from prefix to postfix expression
   public static String conversion(String s)
   {
       //create a stack
       Stack<String> stk = new Stack<String>();
      
       for(int i = s.length()-1; i>=0; i--)
       {
           //extract ith character
           char ch = s.charAt(i);
          
           //continue is blank space
           if(ch==' ') continue;
          
           //check for operator
           if(isOperator(ch))
           {
               //pop two operands from the stack
               String opr1 = stk.pop();
               String opr2 = stk.pop();
              
               String t = opr1 + " " + opr2 + " " + ch;
              
               //push to the stack
               stk.push(t);
           }
           //for operand
           else
           {
               stk.push("" + ch);
           }
       }
      
       String post = stk.pop();
      
       //return postfix expression
       return post;
   }
  
   //main method
   public static void main (String[] args) throws IOException
   {
       //create instance of Scanner class
       Scanner sc = new Scanner(new File("Prefix.in"));
      
       //create instance of FileWriter class
       FileWriter fw = new FileWriter(new File("Postfix.out"));
      
       //read the file until end of file
       while(sc.hasNext())
       {
           //read a line from the file
           String pre = sc.nextLine();
          
           System.out.println("Prefix expression: " + pre);
          
           //convert from prefix to postfix
           String post = conversion(pre);
          
           System.out.println("Postfix expression: " + post);
          
           //write to the file
           fw.write(post + "\n");
       }
      
       //close the files
       sc.close();
       fw.close();
   }
}

Output:

Add a comment
Know the answer?
Add Answer to:
Write a java program that converts a prefix expression to a postfix expression. Input The input...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT