Please make sure that these infix and postfix equations have these answers nothi
ID: 3799231 • Letter: P
Question
Please make sure that these infix and postfix equations have these answers nothing else:
Infix:
(3 * 4 - (2 + 5)) * 4 / 2 = valid expression
10 + 6 * 11 -(3 * 2 + 14) / 2 = valid expression
Postfix:
9 3 / 6 / 4 * 10 - = -8
9 3 / 6 / 4 * -10 - = 12
(a) Using java.util.stack to write a java program to validate and calculate the result of each arithmetic Expression from input file (infix.dat). All equations from the input file are in traditional infix notation. Display each expression first. Then, if the arithmetic expression is not valid, display “Invalid expression ” message otherwise display the result of the calculation.
(b) Using java.util.Stack and java.util.StringTokenizer to write a java program to validate and calculate postfix expression from the input data file - postfix.dat
infix.dat
5 * 6 + 4
3 - 2 +
( 3 * 4 - (2 + 5)) * 4 / 2
10 + 6 * 11 -(3 * 2 + 14) / 2
2 * (12 + (3 + 5 ) * 2
postfix.dat
5 2 + 8 5 - *
2 4 - 5 2 * +
5 2 6 3 - * + 4 + 2 3 1 + * 7
5 0 /
9 3 / 6 / 4 * 10 - 3 / +
9 3 / 6 / 4 * 10 -
5 2 6 3 - * + 4 + 2 3 1 + * 7 - *
9 3 / 6 / 4 * -10 -
Explanation / Answer
import java.util.*;
import java.io.*;
class postfix
{
// Function to evaluate postfix expression
public static int postfixEval(String expression) {
Stack<Integer> stc = new Stack<Integer> ();
StringTokenizer tokens = new StringTokenizer(expression);
boolean status = true; //
while (tokens.hasMoreTokens()) //checking if the expression has another token to read
{
String str = tokens.nextToken(); //getting next token
// If its not an operator then push in the Stack
if (!checkOperator(str)) {
stc.push(new Integer(Integer.parseInt(str))); //converting string to integer
}
else
{
int result;
try
{
int num2 = ((Integer) stc.pop()).intValue();
int num1 = ((Integer) stc.pop()).intValue();
//performing operations according to the operator
if (str.equals("+")) {
result = num1 + num2;
stc.push(new Integer(result));
} else if (str.equals("-")) {
result = num1 - num2;
stc.push(new Integer(result));
} else if (str.equals("*")) {
result = num1 * num2;
stc.push(new Integer(result));
} else {
result = num1 / num2;
stc.push(new Integer(result));
}
}
// catching division exception (cannot divide by zero)
catch(ArithmeticException e)
{
System.out.println("Invalid Epression");
status=false;
}
// catching empty stack exception
catch(java.util.EmptyStackException e)
{
System.out.println("Invalid Epression");
status=false;
}
// "+", "-", "*", "/"
}
}
if(status==true)
{
int output = stc.pop();
if(stc.empty()) // If stack is empty then the exprssion is valid
{
return output;
}
else
{
System.out.println("Invalid Expression");
return Integer.MAX_VALUE;
}
}
else
System.out.println("Something");
return Integer.MAX_VALUE;
}
// Function to check whether the token is an operator
private static boolean checkOperator(String op) {
return (
op.equals("+")
|| op.equals("-")
|| op.equals("*")
|| op.equals("/"));
}
public static void main(String[] args) throws IOException
{
String path="postfix.dat";
//File reading
BufferedReader br = new BufferedReader(new FileReader(path));
String line = null;
int res = 0;
while((line = br.readLine()) != null) //checking for the end of file
{
System.out.println("Expression "+line);
res = postfixEval(line);
if(res!= Integer.MAX_VALUE)
System.out.println("Result:"+res);
}
}
}