mers/examples/03_Basic_Calculator.mers
2023-10-27 19:19:42 +02:00

28 lines
676 B
Plaintext

"- Calculator -".println
"Type =<num> to set the value to that number.".println
"Type +<num>, -<num> or *<num> to change the value.".println
"Type exit to exit.".println
current := 0.0
{() -> {
("[ ", current, " ]").concat.println
input := ().read_line.trim
num := (input, 1).substring.trim.parse_float.try((
() -> 0.0,
val -> val
))
mode := input.substring(0, 1)
if mode.eq("+") {
&current = (current, num).sum
} else if mode.eq("-") {
&current = (current, (num, -1).product).sum
} else if mode.eq("*") {
&current = (current, num).product
} else if mode.eq("=") {
&current = num
} else if input.eq("exit") {
(())
}
}}.loop