Skip to content

agasy18/kotlin-calculator

Repository files navigation

Kotlin Simple Calculator with 40 lines

Code

Usage

val str = "5 + 5"
print(eval(str)) //10

With exception handling

try {
    println(eval(str))
} catch (e: IllegalExpressionException) {
    println("Error '${e.message}' index = ${e.index}")
}

Features

  • + addition

    5 + 5 = 10

  • - substration

    5 - 5 = 0

  • * multiplication

    5 * 5 = 25

  • * division

    5 / 5 = 1

  • * sign support

    +5 - -5 = 10

  • floating number support

    10.5 - 0.5 = 10

  • priority support

    5 + 5 * 5 = 30

  • priority bracket support

    5 * (5 + 5) = 50

  • spacing support

    5 * ( 5+5) = 50

  • wrong expression handled

    5+++5 -> IllegalExpressionException(index=1, "Invalid number")

  • Calculation with single string iteration

Releases

No releases published

Packages

No packages published

Languages