Please give me the code as instructed not other codes!
a ) Write a java program that takes a prefix form of an expression (as
a strings) as an input and output the infix and postfix form of the
expression.
The only operations and operands you need to consider are +;-,/,* ;^ and
{1; 2; 3; … ; 9};
Expert Answer
import java.util.Stack;
public class PostFixConvertion
{
public static void main(String args[]){
String infixExp = “((a+b)*(z+x))”;
System.out.println(“Equivalent postfix expression : ” + printPostfixExp(infixExp));
System.out.println(“Equivalent prefix expression : ” + printPrefixEp(infixExp));
}
public static String printPostfix(String str)
{
Stack stack = new Stack();
String postfix = “”;
for(int i=0;i<str.length();i++)
{
char c = str.charAt(i);
if(Character.isLetter(c))
{
postfix = postfix + c;
}
else if(c == ‘(‘)
{
continue;
}
else if(c == ‘)’)
{
postfix = postfix + ((Character)stack.pop()).toString();
}
else
{
stack.push(c);
}
}
return postfix;
}
public static String printPreFix(String str)
{
Stack stack = new Stack();
String prefix = “”;
for(int i=str.length()-1;i>=0;i–)
{
char c = str.charAt(i);
if(Character.isLetter(c))
{
prefix = ((Character)c).toString() + prefix;
}
else if(c == ‘(‘)
{
prefix = ((Character)stack.pop()).toString() + prefix;
}
else if(c == ‘)’)
{
continue;
}
else
{
stack.push(c);
}
}
return prefix;}
}