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 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 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...
For this assignment you will write a class that transforms a Postfix expression (interpreted as a...
For this assignment you will write a class that transforms a Postfix expression (interpreted as a sequence of method calls) into an expression tree, and provides methods that process the tree in different ways. We will test this using our own program that instantiates your class and calls the expected methods. Do not use another class besides the tester and the ExpressionTree class. All work must be done in the class ExpressionTree. Your class must be called ExpressionTree and have...
How do I covert a char from a sting that is made up of hex values...
How do I covert a char from a sting that is made up of hex values into a long int, without using istringstream and istring in c++?
Java OOP - how do I avoid using getter and setter method in player class for...
Java OOP - how do I avoid using getter and setter method in player class for better privacy? I am told to use regular method instead. but I dont know how Thank you ----------------------------------------------------------------------------------------------------------- package MainPackage; import java.util.ArrayList; public class SoccerTeam { private ArrayList<Player> list; public SoccerTeam(int maxSubmission) { list = new ArrayList<>(maxSubmission); } public boolean addPlayer(String newPlayer) { if(newPlayer == null || newPlayer.isEmpty() == true) { return false; } for(Player player : list) { if(player.getName() == newPlayer) { return...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT