Question

In: Computer Science

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 containing the prefix expression

while there are more tokens

push the token onto a reversal stack if it is not a space

while the reversal stack is not empty

pop the next token from the reversal stack

if it is an operand

push it onto the operand stack

else it is an operator

pop two operands off of the operand stack

create a string with the two operands followed

the operator

push that string onto the operand stack

pop the postfix expression off the stack

The pseudocode for the postfix to prefix conversion, which requires only one stack, is shown below:

tokenize the string containing the postfix expression

while there are more tokens

get the next token

if it is a space

skip it

else if it is an operand

push it onto the operand stack

else it is an operator

pop two operands off of the operand stack

create a string with the operator followed by

two operands

push that string onto the operand stack

pop the prefix expression off the stack

A checked exception SyntaxError should be thrown by the methods that perform the conversions if an empty stack is ever popped or the stack is not empty once the conversion is complete. That exception should be caught in the main class, where a JOptionPane window should be displayed containing an error message.

Solutions

Expert Solution

PROGRAM

import javax.swing.JOptionPane;
import java.util.Stack;
public class postfixprefix {
        
        static boolean Operator(char x){
        switch (x){
            case '-':
            case '+':
            case '/':
            case '*':
            case '^':
                return true;
        }
        return false;
    }
        
        public static String convert_Prifix(String expression){

        Stack<String> stack = new Stack<>();
        for (int i = 0; i <expression.length() ; i++) {

            char c = expression.charAt(i);

            if(Operator(c)){
                String s1 = stack.pop();
                String s2 = stack.pop();
                String temp = c + s2 + s1;
                stack.push(temp);
            }else{
                stack.push(c+"");
            }
        }
        String result = stack.pop();
        return result;
    }
         public static String convert_Postfix(String expression){

                Stack<String> stack = new Stack<String>();
                for (int i = expression.length()-1; i >=0 ; i--) {

                    char c = expression.charAt(i);

                    if(Operator(c)){
                        String s1 = stack.pop();
                        String s2 = stack.pop();
                        String temp = s1 + s2 + c;
                        stack.push(temp);
                    }else{
                        stack.push(c+"");
                    }
                }

                String result = stack.pop();
                return result;
            }

        public static void main(String[] args) {
                                String postfix,prefix,input,expression;
                                int n;
                                expression = JOptionPane.showInputDialog("Enter Expression");
                                input= JOptionPane.showInputDialog("Enter 1 for prefix and 2 for postfix");
                                n = Integer.parseInt(input);
                                if(n==1) {
                                        prefix= convert_Prifix(expression);
                                    JOptionPane.showMessageDialog(null,"Prefix Expression: "+ prefix);
                                }
                                else {
                                        postfix= convert_Postfix(expression);
                                    JOptionPane.showMessageDialog(null,"Postfix Expression: "+ postfix);
                                }
                                
        }

}

OUTPUT


Related Solutions

write a java program tht evaluates postfix expressions. args[0] = the output file the answer will...
write a java program tht evaluates postfix expressions. args[0] = the output file the answer will print to
C++ Data Structure Write a program to change following infix expressions to postfix expressions using a...
C++ Data Structure Write a program to change following infix expressions to postfix expressions using a stack a) D-B+C b) C*D+A*B c) (A*B)*C+D*F-C d) (A-4*(B-C)-D/E)*F
Write a program that will recognize and evaluate prefix expressions. First design and implement a class...
Write a program that will recognize and evaluate prefix expressions. First design and implement a class of prefix expressions. This class should contain methods to recognize and evaluate prefix expressions. This chapter discusses the algorithms you will need to implement these methods. Walls and Mirrors 3rd edition Chapter 6 Programming Practice 8. Programming Language: Java.
C++ program that can take in EITHER "infix" or "prefix" (NOT POSTFIX) and transforms it into...
C++ program that can take in EITHER "infix" or "prefix" (NOT POSTFIX) and transforms it into the other one. Should use stack and have infixToPrefix and prefixToInfix functions with a simple main function for execution.
Write a C++ function that takes in an arithmetic expression in prefix notation and converts it...
Write a C++ function that takes in an arithmetic expression in prefix notation and converts it into a binary tree, such that each operation is stored in a node whose left subtree stores the left operand, and whose right subtree stores the right operand.
* the Algorithm: * write a java program to convert infix to postfix create(OpStk) OpStk.push("#") token...
* the Algorithm: * write a java program to convert infix to postfix create(OpStk) OpStk.push("#") token = nextToken() while (token != "#") if (token is an operand) append token to postfix expression else if (token is "(") // Left paren - Start of sub-expr OpStk.push( token ) else if (token is ")") // Right paren - End of sub-expr pop operators off the stack and append to the postfix expression - stop when you've popped a "(" else (token is...
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)
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
Write Java program that asks a user to input a letter, converts the user input to...
Write Java program that asks a user to input a letter, converts the user input to uppercase if the user types the letter in lowercase, and based on the letter the user the user enters, display a message showing the number that matches the letter entered. For letters A or B or C display 2 For letter D or E or F display 3 For letter G or H or I display 4 For letter J or K or L...
Write a class PostFix that has one method covert that converts an infix expression (as a...
Write a class PostFix that has one method covert that converts an infix expression (as a string input) to a postfix. Then Test it in a different class. code in java.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT