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
MainUI
instance.
-
Constructor Summary
ConstructorsConstructorDescriptionPostfixEvaluation
(List<String> expression, MainUI mainUiInstance) Constructs aPostfixEvaluation
object and evaluates the given postfix expression. -
Method Summary
Modifier and TypeMethodDescriptionprivate double
evaluatePostfix
(List<String> expression) Evaluates the given postfix expression.static boolean
isOperator
(String token) Checks if the given token is a mathematical operator.static double
performOperation
(double num1, double num2, String operator) Performs a mathematical operation on two operands based on the given operator.
-
Constructor Details
-
PostfixEvaluation
Constructs aPostfixEvaluation
object and evaluates the given postfix expression.The result is stored in the
MainUI
instance for display.- Parameters:
expression
- The postfix expression to evaluate (as aList<String>
).mainUiInstance
- TheMainUI
instance 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:
true
if the token is an operator,false
otherwise.
-
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
.
-