typealias Operation = (Double, Double) -> Double
fun calculate(
a: Double,
b: Double,
operation: Operation
): Double {
return operation(a,b)
}
fun main() {
val operations = mapOf<String, Operation>(
"+" to { a,b -> a+b },
"-" to { a,b -> a-b },
"*" to { a,b -> a*b },
"/" to { a,b ->
if(b == 0.0)
Double.NaN
else
a/b
},
"%" to { a,b -> a%b },
"^" to { a,b ->
Math.pow(a,b)
},
"max" to { a,b ->
maxOf(a,b)
}
)
print("Введите первое число: ")
val a = readln().toDouble()
print("Введите операцию: ")
val symbol = readln()
print("Введите второе число: ")
val b = readln().toDouble()
val operation = operations[symbol]
if(operation != null){
val result = calculate(a,b,operation)
println("Ответ: $result")
} else {
println("Такой операции нет")
}
}