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

Postfix notation is an unambiguous way of writing an arithmetic expression without parentheses. It is defined...
Postfix notation is an unambiguous way of writing an arithmetic expression without parentheses. It is defined so that if “(exp1)op(exp2)” is a normal, fully parenthesized expression whose operation is op, the postfix version of this is “pexp1 pexp2 op”, where pexp1 is the postfix version of exp1 and pexp2 is the postfix version of exp2. The postfix version of a sin- gle number or variable is just that number or variable. For example, the postfix version of "((5+2) * (8-3))/4"...
Write in Java Postfix notation is a way of writing expressions without using parentheses. For example,...
Write in Java 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 operands with the...
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...
2. Convert the following infix form expression into the postfix form expression by using a stack:...
2. Convert the following infix form expression into the postfix form expression by using a stack: A+(B*(C-D)+E)/F-G*H Show the stack after each push/pop.
Convert an infix expression to its postfix representation in JAVA
Convert an infix expression to its postfix representation in JAVA
java Convert the following Infix expression to a Postfix expression. Show all work                            Infix Expres
java Convert the following Infix expression to a Postfix expression. Show all work                            Infix Expression                                                 Stack                             Postfix Expression ---------------------------------------------------------------------------------------------------------------------------- 8 - 9*(2 + 1/4) + 3*7                                                     (empty)                            (blank) Evaluate the following Postfix expression. Once again, show all work.            Postfix                                               Stack                     Result           ---------------------------------------    --------------------    --------------            2 7 + 12 4 / * 8 5 + -
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...
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...
Convert the expression into postfix notation 19 + 2 ∗ 5  + (1 - 6/(1 ∗ 2))
Convert the expression into postfix notation 19 + 2 ∗ 5  + (1 - 6/(1 ∗ 2))
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.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT