Question

In: Computer Science

Write a program that performs the following two tasks: Reads an arithmetic expression in an infix...

Write a program that performs the following two tasks:

  1. 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).
  2. Evaluates the postfix expression.

Use the algorithms described in class/ lecture notes. Use linked lists to implement the Queue and Stack ADTs. Using Java built-in classes will result in 0 points. You must use your own Stack and Queue classes (my code is a good starting point). Submit the code + example runs to validate your code. Submit UML chart to show the program design.

TO ANSWERER: Please give a clear, complete, and detailed program in Java. Thank you!

Solutions

Expert Solution

import java.io.*;

        class Stack
        {
        char a[]=new char[100];
        int top=-1;
        
        void push(char c)
        {
        try
        {
        a[++top]= c;
        }
        catch(StringIndexOutOfBoundsException e)
        {
        System.out.println("Stack full , no room to push , size=100");
        System.exit(0);
        }
        }

        char pop()
        {
        return a[top--];
        }

        boolean isEmpty()
        {
        return (top==-1)?true:false;
        }

        char peek()
        {
        return a[top];
        }

        }       


        public class InfixToPostfix
        {

        static Stack operators = new Stack();
                
        public static void main(String argv[]) throws IOException
        {
                String infix;
        
                //create an input stream object
                BufferedReader keyboard = new BufferedReader (new InputStreamReader(System.in));
                
                //get input from user
                System.out.print("\nEnter the algebraic expression in infix: ");
                infix = keyboard.readLine();
                
                //output as postfix
                System.out.println("The expression in postfix is:" + toPostfix(infix));
                
        }
        
        private static String toPostfix(String infix)
        //converts an infix expression to postfix
        {
                char symbol;
                String postfix = "";
                
                for(int i=0;i<infix.length();++i)
                                        //while there is input to be read
                {
                        symbol = infix.charAt(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;
        }
        
        
        static int prec(char x)
        {
                if (x == '+' || x == '-')
                        return 1;
                if (x == '*' || x == '/' || x == '%')
                        return 2;
                return 0;
        }
}
Let me know if you have any doubts or if you need anything to change. 

If you are satisfied with the solution, please leave a +ve feedback : ) Let me know for any help with any other questions.

Thank You!
===========================================================================

Related Solutions

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
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.
In Java, write class InfixToPostfixConverter co convert an ordinary infix arithmetic expression (assume a valid expression...
In Java, write class InfixToPostfixConverter co convert an ordinary infix arithmetic expression (assume a valid expression is entered) with single-digit integers (to make things easier) such as (6 + 2) • 5 - 8 / 4 to a postfix expression. The postfix version (no parentheses are needed) of this infix expression is 6 2 + 5 * 8 4 / - The program should read the expression into StringBuilder or String infix and use the Stack and Stack Interface class...
Write a C++ program that converts an infix expression, which includes (, ), +, -, *,...
Write a C++ program that converts an infix expression, which includes (, ), +, -, *, and / operations to postfix notation. The program should allow the user to enter an infix expression using lower case characters, then it will display the result of conversion on the screen. (Note: Use the STL stack class to accomplish the solution.).
You should write a small C++ program that performs the following tasks: First, your program should...
You should write a small C++ program that performs the following tasks: First, your program should read in a set of 5 integers from “standard in” (remember cin). The numbers that you read in will be either zero or positive. If at least one of the five numbers entered is non-zero then your program should calculate the sum of the numbers and report that back to the user using “standard output” (remember cout). Then your program should be ready to...
An arithmetic expression can be represented in three different formats: infix, prefix, and postfix. In the...
An arithmetic expression can be represented in three different formats: infix, prefix, and postfix. In the infix notation, the operator comes between two operands. In the postfix notation, the operator comes after its two operands. For example, an expression a/b can be transformed to ab/. The following are some examples of the postfix expressions: 2+6*4 Its postfix notation is 2 6 4 * + 2-3+5/4*9-1 Its postfix expression is 2 3 – 5 4 / 9 * + 1 -...
Following is an infix expression.                                       &n
Following is an infix expression.                                           ((A ^ B) ^ C ^ M * W / X ) ^ Y ^ Z Convert it into postfix and prefix using stack and verify through binary tree. Evaluate infix, postfix and prefix with the following values. A = 2, B = 2, C = 3, M = 1, W = 4, X = 8, Y = 1, Z = 3
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)
: In this assignment you will write a C++ program that evaluates an arithmetic expression (represented...
: In this assignment you will write a C++ program that evaluates an arithmetic expression (represented with an infix notation), then outputs this expression in prefix form and also outputs the result of the calculation. The program will first convert the input infix expression to a prefix expression (using the Stack ADT) and then calculate the result (again, using the Stack ADT). The details are provided in the following sections.
Write a program (preferably in Java) that, given an arithmetic expression, first transforms it to a...
Write a program (preferably in Java) that, given an arithmetic expression, first transforms it to a postfix form, and then computes its value (by using stack-based algorithms). Assume that all the numbers in the arithmetic expression are one-digit numbers, i.e., each of these numbers is either 0, or 1, or 2, ..., or 9. For example, your program should correctly process expressions like 2+3*4, but there is no need to process expressions like 11+22.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT