please write ajava program Infix-to-Postfix Converter Human beings generally wri
ID: 3631704 • Letter: P
Question
please write ajava program
Infix-to-Postfix Converter
Human beings generally write expressions like
3 + 4 and 7 / 9
in which the operator ( + and / ) are written between the operands – this is called infix notation .
Computers prefer postfix notation in which the operators appear after the operands, for example
3 4 + and 7 9 /
To evaluate a complex infix expression, a compiler would first convert the expression to postfix and then evaluate the postfix expression.
In this assignment you will write a Java program that will convert an infix expression to a postfix expression
Write the class InfixToPostfixConverter to convert an ordinary infix arithmatic expression (for the purposes of this assignment, you can assume a valid expression has been entered) with single-digit integers such as
(6 + 2) * 5 - 8 / 4
to a postfix expression. The postfix expression of the previous example is (note that no parenthesis are needed):
6 2 + 5 * 8 4 / -
The program should read the expression into StringBuffer infix and use a Stack class to help you to create a postfix expression in StringBuffer postfix.
The algorithm for creating a postfix expression is as follows:
1. Push a left parenthesis ’(’ onto the stack
2. Append a right parenthesis ’)’ to the end of infix
3. While the stack is not empty, read infix from left-to-right and do the following
o If the current character in infix is a digit, append it to the postfix
o If the current character in infix is a left-parenthesis, push it onto the stack
o If the current character in infix is an operator
? Pop operators (if there are any) at the top of the stack while they are equal or higher precedence than the current operator, and append the popped operators to postfix.
? Push the current character in infix onto the stack
o If the current character in infix is a right parenthesis
? Pop operators from the top of the stack and append them to postfix until a parenthesis is at the top of the stack
? Pop (and discard) the left parenthesis from the stack
The following arithmetic operations are allowed in an expression:
+ addition
- subtraction
* multiplication
/ division
? exponentiation
% remainder
The stack should be maintained with stack nodes that each contain an instance variable and a reference to the next stack node.
Some of the methods you will need to implement are as follows:
• Method convertToPostfix, which converts the infix expression to postfix
o this method is public and static
o will accept a StringBuffer object (the infix)
o will return a StringBuffer object (the postfix)
• Method isOperator, which determines whether a character is an operator
• this method is public and static
• will accept a char
• will return a boolean
• Method precedence, which determines whether the precedence of operator1 (from the infix expression) is less than, equal to, or greater than that of operator2 (from the stack).
The method returns true if operator1 has lower precedence than operator2. Otherwise, false is returned.
o this method is public and static
o will return a boolean
• Method peek, which returns the top value of the stack without popping the stack.
Examples:
File listing
Note:
user entered values are shown in bold.
% ls %
InfixToPostfixConverter.java GenericLinkedStack.java EmptyStackException.java %
Compile the Program
% javac *.* %
Example 1
% java InfixToPostfixConverter Please enter an infix expression: 5 + 2 The original infix expression is: 5 + 2 The expression in postfix notation is: 5 2 + %
Example 2
% java InfixToPostfixConverter Please enter an infix expression: 5 * 8 ^ 3 The original infix expression is: 5 * 8 ^ 3 The expression in postfix notation is: 5 8 3 ^ * %
Example 3
% java InfixToPostfixConverter Please enter an infix expression: (5 + 2) * 3 / 2 % 1 The original infix expression is: (5 + 2) * 3 / 2 % 1 The expression in postfix notation is: 5 2 + 3 2 1 % / * %
The Program
The program will be divided up into the following
The Stack
The stack should be maintained with stack nodes that each contain an instance variable and a reference to the next stack node.
The Stack class should:
• be implemented in a file named GenericLinkedStack.java
• be generic
• be implemented using a linked list
• contain the standard stack methods
o isEmpty
o pop
o push
o peek
• throw a EmptyStackException exception if a pop or peek operation were attempted on an empty stack
InfixToPostfixConverter
Some of the methods you will need to implement for this class are as follows:
• Method convertToPostfix , which converts the infix expression to postfix
o this method is public and static
o will accept a StringBuffer object (the infix)
o will return a StringBuffer object (the postfix)
• Method isOperator , which determines whether a character is an operator
• this method is public and static
• will accept a char
• will return a boolean
• Method precedence , which determines whether the precedence of operator1 (from the infix expression) is less than, equal to, or greater than that of operator2 (from the stack).
The method returns true if operator1 has lower precedence than operator2 . Otherwise, false is returned.
o this method is public and static
o will return a boolean
Explanation / Answer
import java.util.Scanner; import java.util.Stack; public class Infix2Postfix { private Stack stack; private String infixExp; private String postfixExp = ""; public Infix2Postfix(String exp){ String str = ""; infixExp = exp; stack = new Stack(); for (int i=0;iRelated Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.