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
PostfixEvaluationclass to calculate the result.
-
Constructor Summary
ConstructorsConstructorDescriptionInfixToPostfix(String infix, MainUI mainUiInstance) Constructs anInfixToPostfixobject and performs infix to postfix conversion. -
Method Summary
Modifier and TypeMethodDescriptioninfixToPostfixConversion(String infix) Converts the given infix expression into postfix notation.private booleanChecks if the given token is an operand (numeric value).private booleanisOperator(String token) Checks if the given token is a mathematical operator.private intprecedence(String operator) Checks the precedence of the operator.
-
Constructor Details
-
InfixToPostfix
Constructs anInfixToPostfixobject and performs infix to postfix conversion.This constructor:
- Converts the provided infix expression to postfix.
- Initializes the
PostfixEvaluationto evaluate the result.
- Parameters:
infix- The infix expression to be converted.mainUiInstance- TheMainUIinstance 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.parseDoubleto determine whether the token is numeric.- Parameters:
token- The token to be checked.- Returns:
trueif the token is an operand,falseotherwise.
-
isOperator
Checks if the given token is a mathematical operator.Supported operators:
+, -, *, /, %, ^- Parameters:
token- The token to be checked.- Returns:
trueif the token is an operator,falseotherwise.
-
precedence
Checks the precedence of the operator.Supported operators:
+, -, *, /, %, ^- Parameters:
operator- The operator to be checked.- Returns:
intbased on the operator.
-