Question

In: Computer Science

Convert an infix expression to its postfix representation in JAVA

Convert an infix expression to its postfix representation in JAVA

Solutions

Expert Solution

Java program to convert infix to postfix expression:

**************************************************************************************************************************************************

import java.util.Stack;
import java.util.Scanner;

public class Main {

//precedence function to return the precedence of the operators
static int precedence(char c){
switch (c){
case '+': //when + or - give precedence 1
case '-':
return 1;
case '*'://when / and * give precedence 2
case '/':
return 2;
case '^': //when ^ give precedence 3
return 3;
}
return -1;
}
  
//function to convert infix to postfix
static String infixToPostFix(String expression){

String result = ""; //String result to show the result
Stack<Character> stack = new Stack<>(); //making a stack class object haivng Character
  
for (int i = 0; i <expression.length() ; i++) { //taking a loop for the expression
char c = expression.charAt(i); //Character c at i position

//check if char is operator
if(precedence(c)>0){
while(stack.isEmpty()==false && precedence(stack.peek())>=precedence(c)){
result += stack.pop();
}
stack.push(c);
}else if(c==')'){ //when there is )
char x = stack.pop(); //pop from the stack
while(x!='('){
result += x;
x = stack.pop();
}
}else if(c=='('){
stack.push(c);
}else{
//character is neither operator nor (
result += c;
}
}
for (int i = 0; i <=stack.size() ; i++) {
result += stack.pop();
}
return result;
}

public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter the Infix Expression: "); //prompt the user for input of the expression
String exp = input.nextLine(); //take input
  
System.out.println("Postfix Expression: " + infixToPostFix(exp));//call the infixToPostFix method and print it
}
}

**********************************************************************************************************************************************

Screenshot of the code for further reference:

Output:


Related Solutions

(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...
* 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...
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 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...
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...
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 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...
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...
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.
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