Write a program that takes as input an infix expression and outputs the equivale
ID: 3546371 • Letter: W
Question
Write a program that takes as input an infix expression and outputs the equivalent post-fix expression. Assume that the input may contain numbers, variables, and arithmetic operations (+, -, *, /), as well as parentheses. However, the expressions need not be fully parenthesized, and when parentheses are missing, the usual Java precedence rules are used to determine the order of evaluation. Your program should allow the user to enter additional expressions until the user wishes to end the program. You can assume that the expressions are well-formed.
(((A+7)*(B/C))-(2*D)) basic algorithm
Provide a top-down design for your program here. This template is a guide for the information you could include. Do not put code in this template. For your algorithms, use a list format or pseudocode. Use as much or as little of this template as you deem appropriate. Be sure to provide a test plan and test data.
(Repeat this section for each of your methods). Provide the method signature here.
(Document your test plan in this section. Include the data sets you intend to test with and consider the boundary values for this problem.)
Explanation / Answer
import java.util.Scanner;
import java.util.Stack;
public class InFix2PostFix
{
// the main method ask the user to enter infix
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
System.out.print("Enter the infix(type 'quit' to exit): ");
String infix = scan.nextLine().trim();
while (!infix.equalsIgnoreCase("quit"))
{
String postfix = infix2Postfix(infix);
System.out.println("Postfix: " + postfix);
System.out.println();
System.out.print("Enter the infix(type 'quit' to exit): ");
infix = scan.nextLine().trim();
}
}
// check whether the character is operator
public static boolean isOperator(char ch)
{
if (ch == '+' || ch == '-' || ch == '*' || ch == '/')
return true;
return false;
}
// get the priority of the operator
public static int getPriority(char ch)
{
if (ch == '*' || ch == '/')
return 2;
if (ch == '+' || ch == '-')
return 1;
return 0;
}
// convert the infix to postfix
public static String infix2Postfix(String infix)
{
Stack<Character> stack = new Stack<Character>();
String post = ""; // result
for (int i = 0; i < infix.length(); i++)
{
char ch = infix.charAt(i);
if (ch == '(')
{
stack.push('(');
}
else if (ch == ')')
{
while (!stack.isEmpty() && stack.peek() != '(')
{
post += stack.pop() + " ";
}
if (stack.isEmpty())
return "Error miss (";
stack.pop();
}
else if (isOperator(ch))
{
while (!stack.isEmpty() && getPriority(stack.peek()) >= getPriority(ch))
post += stack.pop() + " ";
stack.push(ch);
}
else if (Character.isLetterOrDigit(ch))
{
post = post + ch;
while ((i+1) < infix.length() && Character.isLetterOrDigit(infix.charAt(i+1)))
{
i++;
post = post + infix.charAt(i);
}
post = post + " ";
}
}
while (!stack.isEmpty())
{
char tmp = stack.pop();
if (tmp != '(')
post += tmp + " ";
}
return post;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.