Write a program called PostfixToInfix.java to solve the following problem. Write
ID: 3772479 • Letter: W
Question
Write a program called PostfixToInfix.java to solve the following problem.
Write a program to read in a postfix expression and output a corresponding infix expression. You may use any architecture and machinery developed and explained in class at length, as well as code from your solution to the last lab. Your output expression may have as many parethentheses as you decide are necessary for its unambigous evaluation without assuming anything about operator precedence. A postfix expression is defined as in the last lab, and all other details are defined as in class. Error messages for nonsense postfix expressions should be produced as defined in the last lab. You may not use any classes supplied with Java apart from those used in teaching this class. Your program should have behavior as illustrated by the following instance.
java PostfixToInfix
1 2 3 + *
1*(2+3)
Submit PostfixToInfix.java to moodle as your solution.
Explanation / Answer
1)
package conversions6;
import java.util.*;
public class Main
{
public static boolean operand(String s)
{
if(s.charAt(0) == '*' || s.charAt(0) == '/' || s.charAt(0) == '$' ||
s.charAt(0) == '%' || s.charAt(0) == '+' || s.charAt(0) == '-')
{
return false;
}
return true;
}
public static String preToIn(String s)
{
ArrayStack stack = new ArrayStack(20);
String a, b, test;
for(int i = 0;i!= s.length();i++)
{
test= "" + s.charAt(i);
if(!operand(test))
{
stack.push(test);
}
else
{
if(operand(stack.top()))
{
a = stack.pop();
b = stack.pop();
test = "(" + a + b + test + ")";
}
stack.push(test);
}
}
return stack.pop();
}
public static void main(String[] args)
{
Scanner kb = new Scanner(System.in);
ArrayStack stack = new ArrayStack(20);
int count = 0;
String test, garbage;
while(count != 2)
{
System.out.print("Please enter your exp<b></b>ression : ");
test = kb.nextLine();
System.out.println("Prefix to Infix 1)");
System.out.println("Exit 2) ");
System.out.print("Please enter your selection : ");
garbage = kb.nextLine();
count = Integer.parseInt(garbage);
switch(count)
{
case 1: System.out.print(" Infix : ");
System.out.println(preToIn(test));
break;
case 2: break;
}
}
}
}
2)
package conversions6;
public class ArrayStack
{
private int top;
private int size;
private String [] items;
public ArrayStack()
{
top = 0;
items = new String[20];
}
public ArrayStack(int s)
{
top = 0;
size = s;
items = new String[s];
}
public int getTop()
{
return top;
}
public void setTop(int top)
{
this.top = top;
}
public String[] getItems()
{
return items;
}
public String top()
{
return items[top - 1];
}
public void setItems(String[] items)
{
this.items = items;
}
public int getSize()
{
return size;
}
public void setSize(int size)
{
this.size = size;
}
public void push(String obj)
{
if (top != size)
items[top++] = obj;
}
public String pop()
{
if(top != 0)
return items[--top];
return null;
}
}
thank you
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.