Question

In: Computer Science

how do I write a class PostFix that has one method covert that converts an infix...

how do I write a class PostFix that has one method covert that converts an infix expression (as a string input) to postfix in java?

Solutions

Expert Solution

Sample Output:

The screenshots are attached below for reference.

Please follow them for proper output.

Program code to copy:

import java.util.*;
public class PostFix{
public static int precedence(char c){
if(c=='+' || c=='-')//return the precedence value of operators
return 1;
if(c=='*' || c=='/')
return 2;
else
return 3;
}
public static String convert(String e){
String res=new String("");
char c;
Stack<Character> s=new Stack<>();//Use the stack to convert to PostFix form
for(int i=0;i<e.length();i++){//iterate through the string
c=e.charAt(i);
  
if(Character.isLetterOrDigit(c))//if it not operator add it to res string
res=res+c;
  
else{
while(!s.isEmpty() && precedence(c)<=precedence(s.peek())){
res=res+s.pop();//pop the stack if character is a operator for less precedence values
}
s.push(c);//push character after popping all the operators of less precedence
}
}
while(!s.isEmpty()){
res=res+s.pop();//empty the stack

}
return res;
}
public static void main(String[] args){
String e="1+2-3+6+5";//create string in infix form
System.out.println(convert(e));//call function and print returned string
}
}


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.
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 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 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...
**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 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.).
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.
* 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...
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
Postfix Evaluation (JAVA PROGRAMMING) Write class PostfixEva1uator that evaluates a postfix expression such as 6 2...
Postfix Evaluation (JAVA PROGRAMMING) Write class PostfixEva1uator that evaluates a postfix expression such as 6 2 + 5 * 8 4 / - The program should read a postfix expression consisting of single digits and operators into a StringBuilder, The program should read the expression and evaluate it (assume it's valid). The algorithm to evaluate a postfix expression is shown below. Use +, -, *, /, and ^. ^ is the exponent. Append a right parenthesis ') ' to the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT