/Calculator

A calculator made in java using Reverse Polish notation and REGEX

Primary LanguageJavaMIT LicenseMIT

Calculator

A calculator made in java using Reverse Polish Notation (Shunting yard algorithm) and REGEX. Its purpose was to help me learn and understand better OOP concepts. The GUI is made with JavaFX.

Demo

Calculator.2022-09-22.11-57-50.mp4

How does it work

The calculator takes the input (which is the mathematical expression) from the user, converts the string to a list using regex and then converts the list into a queue that contains the RPN form of the expression.

REGEX

ConvertorExpressionToList - used for converting the string into a list using regex.

  • The regex used for delimitating operands: (\d+(\.\d+)?i?)
    • \d+ -> matches a digit between one and unlimited times. This is the whole part
    • (\.\d+)? -> This is the fractional part that can exist or not
    • i? -> matches letter "i" (imaginary unit) zero or one time.
  • The regex used for delimitating operators: (\+|-|\*|/|max|min|\^|%|sqrt|ln|exp|\(|\)|,|i) (built at runtime)
    • matches one of the available operations

Reverse Polish Notation

ConvertorInfixToPostfix - used for converting the infix form(a list) into a postfix form (a queue)

  • This convertor is an improved and detailed implementation of Shunting Yard algorithm
  • Every operator and symbol has either common or special logic for processing the queue.
  • Details about operators and symbols for processing can be found in MathSymbol. PEMDAS rule

Evaluation of RPN

The way each operation is implemented can be found in the Calculations class. Every number is perceived as a ComplexNumber and the operations are written for complex numbers. Decimal numbers are also accepted (or both at the same time).

  • Operators are saved in Binary/UnaryOperatorEnum, where each operator is associated a sign, an operation from Calculations and a regex form for the first mentioned convertor.
  • CalculatorService calculates the postfix expression

Installation and Libraries

Execute JAR

<path_to_java> -jar <path_to_project>\Calculator\Calculator\target\Calculator-1.0-SNAPSHOT-shaded.jar