In: Computer Science
Using java.util.Stack and java.util.StringTokenizer to write a program that converts an infix expression to a postfix expression using data from infix.dat. Make sure your program can handle negative number. If the input expression can’t be converted, display error message.(1.5 points)
(Should remove parentheses)
infix.dat
5 * 6 + -10
3 - 2 + 4 7
( 3 * 4 - (2 + 5)) * 4 / 2
10 + 6 * 11 -(3 * 2 + 14) / 2
2 * (12 + (3 + 5 ) * 2
Program:
//required classes
import java.util.Stack;
import java.util.StringTokenizer;
//driver class
public class InfixToPostfix
{
//finding operator precedence
static int Operator(char op)
{
switch (op){
case '+':
case '-':
return 1;
case '*':
case '/':
return 2;
case '^':
return 3;
}
return -1;
}
//method to convert infix to postfix
static String infixPostfix(String exp)
{
// String to store
result result
String output =
"";
// creating empty
stack
Stack<Character>
stack = new Stack<>();
//creating string
tokenizer
StringTokenizer stk=new
StringTokenizer(exp," ");
//checking if more
tokens are there
while(stk.hasMoreTokens()){
//accessing the expression character
char ch = stk.nextToken().charAt(0);
// if read character is an operand then add it to output.
if (Character.isLetterOrDigit(ch))
output += ch;
// if read character is '(' then push it to stack.
else if (ch == '(')
stack.push(ch);
// if read character is ')' then pop all the element from the stack
untill '(' is found
else if (ch == ')')
{
while (!stack.isEmpty() && stack.peek() != '(')
output += stack.pop();
if (!stack.isEmpty() && stack.peek() != '(')
return "The expression is
invalid";
else
stack.pop();
}
else//if operator is read
{
while (!stack.isEmpty() && Operator(ch) <=
Operator(stack.peek()))
output += stack.pop();
stack.push(ch);
}
}
// popping all the
remaining element from the stack to complete the expression
while
(!stack.isEmpty())
output += stack.pop();
return output;
}
//main method
public static void main(String[] args)
{
//storing the
expression
String expr = "5 * 6 +
-10";
//showing original
expression to user
System.out.println("Infix expression: "+expr);
//calling function to
convert to postfix and showing result
System.out.println("Postfix expression: "+infixPostfix(expr));
expr = "3 - 2 + 4
7";
//showing original
expression to user
System.out.println("Infix expression: "+expr);
//calling function to
convert to postfix and showing result
System.out.println("Postfix expression: "+infixPostfix(expr));
expr = "( 3 * 4 - (
s2 + 5 ) ) * 4 / 2";
//showing original
expression to user
System.out.println("Infix expression: "+expr);
//calling function to
convert to postfix and showing result
System.out.println("Postfix expression: "+infixPostfix(expr));
expr = "10 + 6 * 11 -
( 3 * 2 + 14 ) / 2";
//showing original
expression to user
System.out.println("Infix expression: "+expr);
//calling function to
convert to postfix and showing result
System.out.println("Postfix expression: "+infixPostfix(expr));
expr = "2 * ( 12 + (
3 + 5 ) * 2";
//showing original
expression to user
System.out.println("Infix expression: "+expr);
//calling function to
convert to postfix and showing result
System.out.println("Postfix expression:
"+infixPostfix(expr));
}
}
--------------------------------------------------
Output