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

This question is for \" Data Structure course \". ( check the image below ) I pr

ID: 3812225 • Letter: T

Question

This question is for "Data Structure course". (check the image below)

I prefer a brief answer & If you are going to use any programming language to solve this problem, then use JAVA language.

** Please don't provide a handwritten solution or a picture of the solution. Just post the answer here so I can read it easily.

Expression: a * - b + c

Problem 4 Write the postfix form of the infix expression below. Then describe how each token is processed to generate postfix expression out by using stack expression a b c

Explanation / Answer

Hi, Pleqase find my implementation.

Please let me know inn case of any issue.

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) || Character.isDigit(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");

   }

  

}

/*

Sample run:

Enter the Infix expresion(of enter Q/q to stop)

(3 * 4 - (2 + 5)) * 4 / 2 = valid expression

Postfix Expression:- 3* 4- 2+ 5 * 4/ 2 =valid expression

Enter the Infix expresion(of enter Q/q to stop)

10 + 6 * 11 -(3 * 2 + 14) / 2 = valid expression

Postfix Expression:- 10+ 6* 113* 2+ 14- / 2 =valid expression

Enter the Infix expresion(of enter Q/q to stop)

( 3 * 4 - (2 + 5)) * 4 / 2

Postfix Expression:- 3* 4- 2+ 5 * 4/ 2

Enter the Infix expresion(of enter Q/q to stop)

10 + 6 * 11 -(3 * 2 + 14) / 2

Postfix Expression:- 10+ 6* 113* 2+ 14- / 2

Enter the Infix expresion(of enter Q/q to stop)

2 * (12 + (3 + 5 ) * 2

Postfix Expression:- 2* 12+ 3+ 5 * 2(

Enter the Infix expresion(of enter Q/q to stop)

q

*/

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote