Skip to content
/ exp2rpn Public

Kotlin, convert expression to Reverse Polish Notation and run it.

License

Notifications You must be signed in to change notification settings

mxrpr/exp2rpn

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

16 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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")
}

About

Kotlin, convert expression to Reverse Polish Notation and run it.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages