JAVA Using the algorithm provided in the \"Converting Infix Expressions to Postf
ID: 3691469 • Letter: J
Question
JAVA
Using the algorithm provided in the "Converting Infix Expressions to Postfix" lecture, write a program that converts an infix expression into an equivalent postfix expression. You will not be evaluating the result of the expression - just printing the equation as a postfix equation. Place the following Infix expressions in an external file and read them from the file to test your program: 1. A+ B-C 2. (A B) C 4. A (BC)* (E-F)-G) (H-) 5. A+B*(C+D) EF*G+H When you run your program, the postfix output for each should look like the following: 1. AB+C- 2. AB+C 3. AB+CD- 4. ABC+EF-*G-HI-/+ 5. ABCD+ +EF/G*-H+Explanation / Answer
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class InfixToPostfixConverter {
public String convert(char[] infix)
// converts an infix expression to postfix
{
Stack<Character> operators = new Stack<Character>();
char symbol;
String postfix = "";
for (int i = 0; i < infix.length; ++i)
// while there is input to be read
{
symbol = infix[i];
// if it's an operand, add it to the string
if (Character.isLetter(symbol))
postfix = postfix + symbol;
else if (symbol == '(')
// push (
{
operators.push(symbol);
} else if (symbol == ')')
// push everything back to (
{
while (operators.peek() != '(') {
postfix = postfix + operators.pop();
}
operators.pop(); // remove '('
} else
// print operators occurring before it that have greater precedence
{
while (!operators.isEmpty() && !(operators.peek() == '(')
&& prec(symbol) <= prec(operators.peek()))
postfix = postfix + operators.pop();
operators.push(symbol);
}
}
while (!operators.isEmpty())
postfix = postfix + operators.pop();
return postfix;
}
public int prec(char x) {
if (x == '+' || x == '-')
return 1;
if (x == '*' || x == '/' || x == '%')
return 2;
return 0;
}
// main method
public static void main(String[] args)throws IOException
{
// declaring object
PostfixEvaluator postfixEval = new PostfixEvaluator();
InfixToPostfixConverter infixToPostfix = new InfixToPostfixConverter();
String input;
while(true)
{
System.out.println("Enter the Infix expresion(of enter Q/q to stop)");
input=getString();
if(input.equalsIgnoreCase("q"))
break;
char[] infix = input.trim().toCharArray();
String postfix = infixToPostfix.convert(infix);
System.out.println("Postfix Expression:- "+postfix);
}
}
public static String getString()throws IOException
{
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);
String s = br.readLine();
return s;
}
}
/*
Sample run:
Enter the Infix expresion(of enter Q/q to stop)
A+B-C
Postfix Expression:- AB+C-
Enter the Infix expresion(of enter Q/q to stop)
(A+B)*C
Postfix Expression:- AB+C*
Enter the Infix expresion(of enter Q/q to stop)
(A+B)/(C+D)
Postfix Expression:- AB+CD+/
Enter the Infix expresion(of enter Q/q to stop)
A+((B+C)*(E-F)-G)/(H-I)
Postfix Expression:- ABC+EF-*G-HI-/+
Enter the Infix expresion(of enter Q/q to stop)
A+B*(C+D)-E/F*G+H
Postfix Expression:- ABCD+*+EF/G*-H+
Enter the Infix expresion(of enter Q/q to stop)
q
*/
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.