Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Infix: (3 * 4 - (2 + 5)) * 4 / 2 = valid expression 10 + 6 * 11 -(3 * 2 + 14) /

ID: 3864288 • Letter: I

Question

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 and 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

Hi Friend, You have not mentioned about programming language.

Please try to mention all details.

I have implemented Q1 : convertion of infix to postfix.

Please let me know in case of any issue in Q1.

I have implemented in Java.

import java.io.BufferedReader;

import java.io.BufferedWriter;

import java.io.FileReader;

import java.io.FileWriter;

import java.io.IOException;

import java.util.Stack;

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();

       System.out.println("Reading from in.dat file");

      

       String input;

       FileReader isr = new FileReader("in.dat");

       BufferedReader br = new BufferedReader(isr);

       FileWriter fr = new FileWriter("post.dat");

       BufferedWriter bw = new BufferedWriter(fr);

       while(true)

       {

           //System.out.println("Enter the Infix expresion(of enter Q/q to stop)");

          

           input = br.readLine();

           //input=getString();

           if(input.equalsIgnoreCase("q"))

               break;

           char[] infix = input.trim().toCharArray();

           String postfix = infixToPostfix.convert(infix);

          

           bw.write(postfix);

           bw.newLine();

          

           //System.out.println("Postfix Expression:- "+postfix);

       }

      

       System.out.println("Successfully written in post.dat");

   }

  

}