Skip to content

Latest commit

 

History

History
41 lines (35 loc) · 1.15 KB

README.MD

File metadata and controls

41 lines (35 loc) · 1.15 KB

Convert expression to Reverse Polish notation. Simple library which is able to run the expression and calculate the result.

As an example, the following expression "1 + ( 2 * 3 ) + 4" will be transformed to: "1 2 3 * + 4 +"

Expression can contain variables:

"{{var1}} + {{var2}}"

Sample usage

fun main(args: Array<String>) {
    val expRunner = RPNRunner()
    val expression = "(2*3)+4/2"
    println("Example for expression $expression :")
    val result = expRunner.calculate(expression)
    println("Result: $result")
}

In case the expression contains variables, the RPN runner will use a ValueProvider object to get the values - by the variable names Example:

class MyValueProvider: ValueProvider {
    override fun getValue(variableName: String): Double {
        return 1.2
    }
}

fun main(args: Array<String>) {
    val myProvider = MyValueProvider()
	// set the provider for runner
    val expRunner = RPNRunner(myProvider)
    val expression = "(2*3)+4/var2"
    println("Example for expression $expression :")
    val result = expRunner.calculate(expression)
    println("Result: $result")
}