mers/examples/03_Basic_Calculator.mers

30 lines
756 B
Plaintext
Raw Normal View History

2023-10-19 19:38:13 +02:00
"- Calculator -".println
"Type =<num> to set the value to that number.".println
"Type +<num>, -<num>, *<num> or /<num> to change the value.".println
2023-10-19 19:38:13 +02:00
"Type exit to exit.".println
current := 0.0
loop {
2023-10-19 19:38:13 +02:00
("[ ", current, " ]").concat.println
input := ().read_line.trim
2024-03-22 16:50:01 +01:00
num := (input, 1).substring.trim.parse_float.try(
(val) -> val
() -> 0.0
2024-03-22 16:50:01 +01:00
)
2023-10-27 19:19:42 +02:00
mode := input.substring(0, 1)
if mode.eq("+") {
2023-10-19 19:38:13 +02:00
&current = (current, num).sum
2023-10-27 19:19:42 +02:00
} else if mode.eq("-") {
2023-10-19 19:38:13 +02:00
&current = (current, (num, -1).product).sum
2023-10-27 19:19:42 +02:00
} else if mode.eq("*") {
2023-10-19 19:38:13 +02:00
&current = (current, num).product
} else if mode.eq("/") {
&current = (current, num).div
2023-10-27 19:19:42 +02:00
} else if mode.eq("=") {
2023-10-19 19:38:13 +02:00
&current = num
} else if (input.eq("exit"), input.eq("")).any {
2023-10-19 19:38:13 +02:00
(())
}
}