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}")
}
-
+
addition5 + 5
= 10 -
-
substration5 - 5
= 0 -
*
multiplication5 * 5
= 25 -
*
division5 / 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