Postfix is a mathematical expression evaluator built with Kotlin (compatible with Java). Postfix provides an easy way to evaluate string representations of basic mathematical expressions.
Postfix uses an Expression object to store the expression in a format Postfix can understand. To convert a string to an Expression, you can use Postfix.from(myString).
String expressionAsString = "5/(4+2)";
Expression expression = Postfix.from(expressionAsString);val expressionAsString = "5/(4+2)"
val expression = Postfix.from(expressionAsString)Evaluating an Expression will give you a single Operand as a result. Operand has a value member of type BigDecimal.
To evaluate a valid Expression, use Postfix.evaluate(Expression).
Expression myExpression = Postfix.from("5/(4+2)");
Operand result = Postfix.evaluate(myExpression);
BigDecimal value = result.value;val myExpression = Postfix.from("5/(4+2)")
val result = Postfix.evaluate(myExpression)
val value = result.value- + Plus
- - Minus
- - UnaryMinus
- * Multiply
- / Divide
- ^ Exponent
- () Brackets
- % Modulus
- Functions
- sqrt() Square Root
- Trigonometry
- sin() Sine
- cos() Cosine
- tan() Tangent
- asin() Inverse Sine
- acos() Inverse Cosine
- atan() Inverse Tangent
- rad() Radians
- deg() Degrees
- abs() Absolute Value
- ceil() Ceiling
- floor() Floor
- round() Round
The Expression class inherits ArrayList<ExpressionEntity> so you'll be able to use all the default methods present in ArrayList such as ArrayList<T>.add(T). This means you'll also be able to iterate through the Expression like a regular ArrayList.
Since Operand and Operator both inherit ExpressionEntity, you'll be able to add objects of those types into the Expression. However this is not recommended as the Expression may become an invalid Postfix Expression if you add Operands or Operators.
An Expression object can be either an Infix Expression or a Postfix Expression. The difference is determined by the order of the entities in the Expression object.
For example, Postfix.from(String) will return an Expression in Postfix notation however Postfix.scan(String) will return an Expression in Infix notation.
When evaluating, you must provide a valid Postfix Expression in order for Postfix to evaluate the Expression properly.
To convert an Infix Expression into a Postfix Expression, simply use Postfix.toPostfix(Expression).
Expression infix = Postfix.scan("5/(4+2)");
Expression postfix = Postfix.toPostfix(infix);
Operand result = Postfix.evaluate(postfix);val infix = Postfix.scan("5/(4+2)")
val postfix = Postfix.toPostfix(infix)
val result = Postfix.evaluate(postfix)