Class InfixToPostfix

java.lang.Object
com.project.calculator.InfixToPostfix

public class InfixToPostfix extends Object
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 Details

    • InfixToPostfix

      InfixToPostfix(String infix, MainUI mainUiInstance)
      Constructs an InfixToPostfix 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 - The MainUI instance for result handling.
  • Method Details

    • infixToPostfixConversion

      private List<String> infixToPostfixConversion(String infix)
      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 a String.
      Returns:
      A List<String> representing the postfix expression.
    • isOperand

      private boolean isOperand(String token)
      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

      private boolean isOperator(String token)
      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

      private int precedence(String operator)
      Checks the precedence of the operator.

      Supported operators: +, -, *, /, %, ^

      Parameters:
      operator - The operator to be checked.
      Returns:
      int based on the operator.