Question

In: Computer Science

Using java.util.Stack and java.util.StringTokenizer to write a program that converts an infix expression to a postfix...

Using java.util.Stack and java.util.StringTokenizer to write a program that converts an infix expression to a postfix expression using data from infix.dat. Make sure your program can handle negative number. If the input expression can’t be converted, display error message.(1.5 points)

(Should remove parentheses)

infix.dat

5 * 6 + -10
3 - 2 + 4 7
( 3 * 4 - (2 + 5)) * 4 / 2
10 + 6 * 11 -(3 * 2 + 14) / 2
2 * (12 + (3 + 5 ) * 2

Solutions

Expert Solution

Program:

//required classes
import java.util.Stack;
import java.util.StringTokenizer;

//driver class
public class InfixToPostfix
{
    //finding operator precedence
    static int Operator(char op)
    {
        switch (op){
            case '+':
            case '-':
                return 1;
            case '*':
            case '/':
                return 2;
            case '^':
                return 3;
        }
        return -1;
    }
    //method to convert infix to postfix
    static String infixPostfix(String exp)
    {
        // String to store result result
        String output = "";
        // creating empty stack
        Stack<Character> stack = new Stack<>();
        //creating string tokenizer
        StringTokenizer stk=new StringTokenizer(exp," ");
        //checking if more tokens are there
        while(stk.hasMoreTokens()){
            //accessing the expression character
            char ch = stk.nextToken().charAt(0);
            
             // if read character is an operand then add it to output.
            if (Character.isLetterOrDigit(ch))
                output += ch;
             
            // if read character is '(' then push it to stack.
            else if (ch == '(')
                stack.push(ch);
            
            // if read character is ')' then pop all the element from the stack untill '(' is found
            else if (ch == ')')
            {
                while (!stack.isEmpty() && stack.peek() != '(')
                    output += stack.pop();
                
                if (!stack.isEmpty() && stack.peek() != '(')
                    return "The expression is invalid";              
                else
                    stack.pop();
            }
            else//if operator is read
            {
                while (!stack.isEmpty() && Operator(ch) <= Operator(stack.peek()))
                    output += stack.pop();
                stack.push(ch);
            }
     
        }
     
        // popping all the remaining element from the stack to complete the expression
        while (!stack.isEmpty())
            output += stack.pop();
     
        return output;
    }
  
    //main method
    public static void main(String[] args)
    {
        //storing the expression
        String expr = "5 * 6 + -10";
        //showing original expression to user
        System.out.println("Infix expression: "+expr);
        //calling function to convert to postfix and showing result
        System.out.println("Postfix expression: "+infixPostfix(expr));

        expr = "3 - 2 + 4 7";
        //showing original expression to user
        System.out.println("Infix expression: "+expr);
        //calling function to convert to postfix and showing result
        System.out.println("Postfix expression: "+infixPostfix(expr));

        expr = "( 3 * 4 - ( s2 + 5 ) ) * 4 / 2";
        //showing original expression to user
        System.out.println("Infix expression: "+expr);
        //calling function to convert to postfix and showing result
        System.out.println("Postfix expression: "+infixPostfix(expr));

        expr = "10 + 6 * 11 - ( 3 * 2 + 14 ) / 2";
        //showing original expression to user
        System.out.println("Infix expression: "+expr);
        //calling function to convert to postfix and showing result
        System.out.println("Postfix expression: "+infixPostfix(expr));

        expr = "2 * ( 12 + ( 3 + 5 ) * 2";
        //showing original expression to user
        System.out.println("Infix expression: "+expr);
        //calling function to convert to postfix and showing result
        System.out.println("Postfix expression: "+infixPostfix(expr));
    }
}

--------------------------------------------------

Output


Related Solutions

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.
Write a program that takes an infix expression as input and produces a postfix expression. The...
Write a program that takes an infix expression as input and produces a postfix expression. The infix and postfix expressions are in the form of vectors of strings. We will test with a main method that takes a file name as input from the command line. We will test your implementation using printPostFix() method.   There are sample input and output files in this folder. You may edit the header file. Example 1 infix expression = apple + banana * cat...
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.
Using STL stack class, implement in C++ a function that converts an infix expression to postfix...
Using STL stack class, implement in C++ a function that converts an infix expression to postfix expression,
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.).
(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...
**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 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
Implement an infix expression to postfix expression convertor in C++. Note: - Support +, -, *,...
Implement an infix expression to postfix expression convertor in C++. Note: - Support +, -, *, /, ( and ) in infix expressions. - Operands will be nonnegative only. - Assume infix expressions are valid and well formatted (one blank space to separate operand/operator) For example, - 3 * 4 + 5 ➔ 3 4 * 5 + - 3 + 4 * 5 ➔ 3 4 5 * + - (3 + 4) * (5 + 6) ➔ 3...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT