Package com.project.calculator
Class PostfixEvaluation
java.lang.Object
com.project.calculator.PostfixEvaluation
The
PostfixEvaluation class evaluates a mathematical expression
in postfix notation using a stack-based approach.
This class performs the following tasks:
- Processes postfix expressions containing operands and operators.
- Evaluates the expression using a stack.
- Updates the result in the
MainUIinstance.
-
Constructor Summary
ConstructorsConstructorDescriptionPostfixEvaluation(List<String> expression, MainUI mainUiInstance) Constructs aPostfixEvaluationobject and evaluates the given postfix expression. -
Method Summary
Modifier and TypeMethodDescriptionprivate doubleevaluatePostfix(List<String> expression) Evaluates the given postfix expression.static booleanisOperator(String token) Checks if the given token is a mathematical operator.static doubleperformOperation(double num1, double num2, String operator) Performs a mathematical operation on two operands based on the given operator.
-
Constructor Details
-
PostfixEvaluation
Constructs aPostfixEvaluationobject and evaluates the given postfix expression.The result is stored in the
MainUIinstance for display.- Parameters:
expression- The postfix expression to evaluate (as aList<String>).mainUiInstance- TheMainUIinstance to store the result.
-
-
Method Details
-
evaluatePostfix
Evaluates the given postfix expression.The method uses a stack to:
- Push operands onto the stack.
- Pop two operands when an operator is encountered.
- Perform the operation and push the result back onto the stack.
- Parameters:
expression- The postfix expression as aList<String>.- Returns:
- The result of the evaluated expression as a
double.
-
isOperator
Checks if the given token is a mathematical operator.Supported operators:
+, -, *, /, %.- Parameters:
token- The token to check.- Returns:
trueif the token is an operator,falseotherwise.
-
performOperation
Performs a mathematical operation on two operands based on the given operator.The supported operations include:
+→ Addition-→ Subtraction*→ Multiplication/→ Division%→ Modulus
- Parameters:
num1- The first operand.num2- The second operand.operator- The mathematical operator as aString.- Returns:
- The result of the operation as a
double.
-