Question

In: Computer Science

Using Java 8. Write a program that reads an expression in postfix notation, builds the expression...

Using Java 8. Write a program that reads an expression in postfix notation, builds the expression tree and prints the expression in prefix and infix notation and evaluates the expression. (Hint use a stack)

Solutions

Expert Solution

import java.util.*;

class Solution {

    static boolean isOperand(char x)

{

    return (x >= 'a' && x <= 'z') ||

            (x >= 'A' && x <= 'Z');

}

  

// Get Infix for a given postfix

// expression

static String getInfix(String exp)

{

    Stack<String> s = new Stack<String>();

  

    for (int i = 0; i < exp.length(); i++)

    {

        // Push operands

        if (isOperand(exp.charAt(i)))

        {

        s.push(exp.charAt(i) + "");

        }

  

        // We assume that input is

        // a valid postfix and expect

        // an operator.

        else

        {

            String op1 = s.peek();

            s.pop();

            String op2 = s.peek();

            s.pop();

            s.push("(" + op2 + exp.charAt(i) +

                    op1 + ")");

        }

    }

  

    // There must be a single element

    // in stack now which is the required

    // infix.

    return s.peek();

}

    static boolean isOperator(char x)

    {

        switch (x) {

        case '+':

        case '-':

        case '/':

        case '*':

            return true;

        }

        return false;

    }

    // Convert postfix to Prefix expression

    static String postToPre(String post_exp)

    {

        Stack<String> s = new Stack<String>();

        // length of expression

        int length = post_exp.length();

        // reading from right to left

        for (int i = 0; i < length; i++) {

            // check if symbol is operator

            if (isOperator(post_exp.charAt(i))) {

                // pop two operands from stack

                String op1 = s.peek();

                s.pop();

                String op2 = s.peek();

                s.pop();

                // concat the operands and operator

                String temp

                    = post_exp.charAt(i) + op2 + op1;

                // Push String temp back to stack

                s.push(temp);

            }

            // if symbol is an operand

            else {

                // push the operand to the stack

                s.push(post_exp.charAt(i) + "");

            }

        }

        // concatenate all strings in stack and return the

        // answer

        String ans = "";

        for (String i : s)

            ans += i;

        return ans;

    }

    // Driver Code

    public static void main(String args[])

    {

        String post_exp = "ABC/-AK/L-*";

        // Function call

        System.out.println("Prefix : "

                           + postToPre(post_exp));

        System.out.println( "Infix : "+getInfix(post_exp));

    }

}


Related Solutions

Your assignment for this program is to evaluate a numeric expression in postfix notation using a...
Your assignment for this program is to evaluate a numeric expression in postfix notation using a dynamic (pointer based) stack. As stated in the handout, in order to evaluate a numeric expression, a compiler converts an infix numeric expression to postfix notation and then it uses an algorithm and a stack to evaluate the expression. Your program should implement the pseudocode algorithm described in the attached handout. Your program will read and evaluate expressions stored in an input file (infile.txt)....
(Convert infix to postfix) Note: Postfix notation is a way of writing expression without using parentheses....
(Convert infix to postfix) Note: Postfix notation is a way of writing expression without using parentheses. For example, the expression ( 11 + 12 ) * 13 would be written as 11 12 + 13 * Assume that ALWAYS there is a space between operands and operators in the input expression. Use two stacks, one to store the operands and one to store the operators. Your program only accpets following operators : ( ) + - / * Write a...
Using a stack, write a program that turns a simple infix arithmetic expression into a postfix...
Using a stack, write a program that turns a simple infix arithmetic expression into a postfix expression. For example, 1 + 2 * 3 becomes 2 3 * 1 +. Also, evaluate the expression to ensure the expression is correct.
Write a program that performs the following two tasks in java Reads an arithmetic expression in...
Write a program that performs the following two tasks in java Reads an arithmetic expression in an infix form, stores it in a queue (infix queue) and converts it to a postfix form (saved in a postfix queue). Evaluates the postfix expression. Use linked lists to implement the Queue and Stack ADTs. DO NOT USE BUILT IN JAVA CLASSES
Postfix Evaluation (JAVA PROGRAMMING) Write class PostfixEva1uator that evaluates a postfix expression such as 6 2...
Postfix Evaluation (JAVA PROGRAMMING) Write class PostfixEva1uator that evaluates a postfix expression such as 6 2 + 5 * 8 4 / - The program should read a postfix expression consisting of single digits and operators into a StringBuilder, The program should read the expression and evaluate it (assume it's valid). The algorithm to evaluate a postfix expression is shown below. Use +, -, *, /, and ^. ^ is the exponent. Append a right parenthesis ') ' to the...
**write a java program infix to postfix**showing the expression tree*** I. Input All input data are...
**write a java program infix to postfix**showing the expression tree*** I. Input All input data are from a file "in.dat". The file contains a sequence of infix form expressions, one per line. The character '$' is an end mark. For example, the following file has four infix form expressions: 1 + 2 * 3 ^ ! ( 4 == 5 ) $ 3 * ( 6 - 4 * 5 + 1) + 2 ^ 2 ^ 3 $ 77...
Write a JAVA program that reads a text file into RAM efficiently, takes a regular expression...
Write a JAVA program that reads a text file into RAM efficiently, takes a regular expression from the user, and then prints every line that matches the RE.
Write a program that converts prefix expressions to postfix and postfix expressions to prefix. (java) The...
Write a program that converts prefix expressions to postfix and postfix expressions to prefix. (java) The program for this project should consist of three classes. -The main class should create a GUI that allows the user to input an expression in either prefix or postfix and convert it to postfix or prefix, respectively. -The other class should contain the code to perform the conversions. --->The pseudocode for prefix to postfix, which requires two stacks, is shown below: tokenize the string...
Write a java class program to convert from INFIX TO POSTFIX Using stack operations
Write a java class program to convert from INFIX TO POSTFIX Using stack operations
CS 209 Data Structure (Postfix notation) Postfix notation is a way of writing expressions without using...
CS 209 Data Structure (Postfix notation) Postfix notation is a way of writing expressions without using parentheses. For example, the expression (1 + 2) * 3 would be written as 1 2 + 3 *. A postfix expression is evaluated using a stack. Scan a postfix expression from left to right. A variable or constant is pushed into the stack. When an operator is encountered, apply the operator with the top two operands in the stack and replace the two...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT