Package com.project.calculator
Class InfixToPostfix
java.lang.Object
com.project.calculator.InfixToPostfix
The
InfixToPostfix
class is responsible for converting an infix expression
to postfix notation and triggering the postfix evaluation process.
This class performs the following tasks:
- Converts an infix expression into postfix notation.
- Handles operator precedence and parentheses.
- Triggers the
PostfixEvaluation
class to calculate the result.
-
Constructor Summary
ConstructorsConstructorDescriptionInfixToPostfix
(String infix, MainUI mainUiInstance) Constructs anInfixToPostfix
object and performs infix to postfix conversion. -
Method Summary
Modifier and TypeMethodDescriptioninfixToPostfixConversion
(String infix) Converts the given infix expression into postfix notation.private boolean
Checks if the given token is an operand (numeric value).private boolean
isOperator
(String token) Checks if the given token is a mathematical operator.private int
precedence
(String operator) Checks the precedence of the operator.
-
Constructor Details
-
InfixToPostfix
Constructs anInfixToPostfix
object and performs infix to postfix conversion.This constructor:
- Converts the provided infix expression to postfix.
- Initializes the
PostfixEvaluation
to evaluate the result.
- Parameters:
infix
- The infix expression to be converted.mainUiInstance
- TheMainUI
instance for result handling.
-
-
Method Details
-
infixToPostfixConversion
Converts the given infix expression into postfix notation.The conversion is based on the standard infix-to-postfix algorithm:
- Operands are added directly to the postfix list.
- Operators are pushed to a stack while respecting precedence.
- Parentheses are handled properly to maintain order of operations.
- Parameters:
infix
- The infix expression as aString
.- Returns:
- A
List<String>
representing the postfix expression.
-
isOperand
Checks if the given token is an operand (numeric value).An operand is any valid number. This method uses
Double.parseDouble
to determine whether the token is numeric.- Parameters:
token
- The token to be checked.- Returns:
true
if the token is an operand,false
otherwise.
-
isOperator
Checks if the given token is a mathematical operator.Supported operators:
+, -, *, /, %, ^
- Parameters:
token
- The token to be checked.- Returns:
true
if the token is an operator,false
otherwise.
-
precedence
Checks the precedence of the operator.Supported operators:
+, -, *, /, %, ^
- Parameters:
operator
- The operator to be checked.- Returns:
int
based on the operator.
-