Question

In: Computer Science

(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 method to converts an infix expression into a postfix expression using the following method:

String infixToPostfix(String expression)

For example, the method should convert the infix expression

( 13 + 25 ) * 34 to 13 25 + 34 *

and

20 * ( 10 + 30 ) to 20 10 30 + *.

import java.util.*;
import java.lang.*;
import java.io.*;

class InfixToPostfix{
public String infixToPostfix(String expression) {
  
   }
}
class DriverMain{
   public static void main(String args[]){
Scanner input = new Scanner(System.in);
InfixToPostfix postfix = new InfixToPostfix();
try {
System.out.println(postfix.infixToPostfix(input.nextLine()));
}
catch (Exception ex) {
System.out.println("Wrong expression");
}
   }
}

Solutions

Expert Solution

/* Java implementation to convert infix expression to postfix*/
// Note that here we use Stack class for Stack operations 

import java.util.Stack; 

class Test 
{ 
        // A utility function to return precedence of a given operator 
        // Higher returned value means higher precedence 
        static int Prec(char ch) 
        { 
                switch (ch) 
                { 
                case '+': 
                case '-': 
                        return 1; 
        
                case '*': 
                case '/': 
                        return 2; 
        
                case '^': 
                        return 3; 
                } 
                return -1; 
        } 
        
        // The main method that converts given infix expression 
        // to postfix expression. 
        static String infixToPostfix(String exp) 
        { 
                // initializing empty String for result 
                String result = new String(""); 
                
                // initializing empty stack 
                Stack<Character> stack = new Stack<>(); 
                
                for (int i = 0; i<exp.length(); ++i) 
                { 
                        char c = exp.charAt(i); 
                        
                        // If the scanned character is an operand, add it to output. 
                        if (Character.isLetterOrDigit(c)) 
                                result += c; 
                        
                        // If the scanned character is an '(', push it to the stack. 
                        else if (c == '(') 
                                stack.push(c); 
                        
                        // If the scanned character is an ')', pop and output from the stack 
                        // until an '(' is encountered. 
                        else if (c == ')') 
                        { 
                                while (!stack.isEmpty() && stack.peek() != '(') 
                                        result += stack.pop(); 
                                
                                if (!stack.isEmpty() && stack.peek() != '(') 
                                        return "Invalid Expression"; // invalid expression                               
                                else
                                        stack.pop(); 
                        } 
                        else // an operator is encountered 
                        { 
                                while (!stack.isEmpty() && Prec(c) <= Prec(stack.peek())){ 
                                        if(stack.peek() == '(') 
                                                return "Invalid Expression"; 
                                        result += stack.pop(); 
                        } 
                                stack.push(c); 
                        } 
        
                } 
        
                // pop all the operators from the stack 
                while (!stack.isEmpty()){ 
                        if(stack.peek() == '(') 
                                return "Invalid Expression"; 
                        result += stack.pop(); 
                } 
                return result; 
        } 
        
        // Driver method 
        public static void main(String[] args) 
        { 
                String exp = "a+b*(c^d-e)^(f+g*h)-i"; 
                System.out.println(infixToPostfix(exp)); 
        } 
} 

Output:

abcd^e-fgh*+^*+i-

Related Solutions

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...
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 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)
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)....
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
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,
1) Consider the following infix expressions. What is the equivalent postfix (reverse Polish notation) expression? 16/(5+3)b)...
1) Consider the following infix expressions. What is the equivalent postfix (reverse Polish notation) expression? 16/(5+3)b) A*B+C*Dc) X × Y + W × Z + V × U 2) Consider the postfix (reverse Polish notation) 10 5 + 6 3 - /. What is the equivalent infix expression?
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.
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 -...
* 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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT