In: Computer Science
JAVA PROGRAM
Question : Design and implement two classes called InfixToPostfix and PostFixCalculator. The InfixToPrefix class converts an infix expression to a postfix expression. The PostFixCalculator class evaluates a postfix expression. This means that the expressions will have already been converted into correct postfix form. Write a main method that prompts the user to enter an expression in the infix form, converts it into postfix, displays the postfix expression as well as it's evaluation. For simplicity, use only these operators, + , - , * , / and %.
I need help answering the programming question, not sure how this really functions. If you can please explain how it works with comments. Thank you I greatly appreciate the help.
The below code illustartes two methods.
infixToPostfix : converts infix expression to postfix expression
postFixCalculator : calculates the value of postfix expression.
The code is commented for understanding each logic.
import java.util.*;
class Main
{
// this method returns the precedence of operators
static int Preced(char c)
{
switch (c) // define some integer value as per the order of precedence
{
case '+':
case '-':
return 1;
case '*':
case '/':
case '%':
return 2;
case '^':
return 3;
}
return 0;
}
// this method converts infix expression to postfix expression
static String infixToPostfix(String infix)
{
// initialize one empty stack
Stack<Character> st = new Stack<>();
// initialize one empty string
String postfix = new String("");
for (int i = 0; i<infix.length(); i++)
{
char c = infix.charAt(i);
// if the character is operand, add it to output
if (Character.isLetterOrDigit(c)) // this built in function returns true if the char is letter or digit
postfix += c;
// if the character is '(', push it to stack
else if (c == '(')
st.push(c);
// if the character is ')', pop and add it to output string until '(' is found
else if (c == ')')
{
while (!st.isEmpty() && st.peek() != '(')
postfix = postfix+ st.pop();
// if '(' is not found at the end then its not an infix expression
if (!st.isEmpty() && st.peek() != '(')
return "Wrong expression, not an infix expression"; // invalid expression
else
st.pop();
}
else // if an operator is found
{
// check the precedence, while the precedence of the current operator is less than the precedence of operators in stack,
//remove the operators from stack and add them to the result
while (!st.isEmpty() && Preced(c) <= Preced(st.peek())){
if(st.peek() == '(')
return "Wrong expression, not an infix expression";
postfix = postfix + st.pop();
}
st.push(c); // at the end push the current operator into the stack
}
}
// at the end pop all the operators from stack and add them to the result
while (!st.isEmpty()){
if(st.peek() == '(')
return "Wrong expression, not an infix expression";
postfix = postfix + st.pop();
}
return postfix;
}
static int postfixCalculator(String postfix)
{
Stack<Integer> st=new Stack<>();
// loop through all the characters
for(int i=0;i<postfix.length();i++)
{
char c=postfix.charAt(i);
// if operand (digit) push into stack
if(Character.isDigit(c))
st.push(c - '0');
// if operator, pop two elements from top and do the calculation
// push the result back to the stack
else
{
int a= st.pop();
int b = st.pop();
switch(c)
{
case '+':
st.push(b+a);
break;
case '-':
st.push(b- a);
break;
case '/':
st.push(b/a);
break;
case '*':
st.push(b*a);
break;
}
}
}
return st.pop(); // at the end return the single element from the stack which is the result
}
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter the infix expression: ");
String infix = sc.nextLine();
String postfix = infixToPostfix(infix);
System.out.println("The postfix expression is: "+postfix );
int res = postfixCalculator(postfix);
System.out.println("The value of the postfix expression is: "+ res);
}
}
Output sample screenshot: