2023-10-19 19:38:13 +02:00
|
|
|
"- Calculator -".println
|
|
|
|
"Type =<num> to set the value to that number.".println
|
2023-11-08 15:54:46 +01:00
|
|
|
"Type +<num>, -<num>, *<num> or /<num> to change the value.".println
|
2024-06-19 13:58:30 +02:00
|
|
|
"Type exit or press CTRL+D to exit.".println
|
2023-10-19 19:38:13 +02:00
|
|
|
|
|
|
|
current := 0.0
|
|
|
|
|
2024-02-22 20:21:11 +01:00
|
|
|
loop {
|
2023-10-19 19:38:13 +02:00
|
|
|
("[ ", current, " ]").concat.println
|
2024-06-19 13:58:30 +02:00
|
|
|
().read_line.try(
|
|
|
|
(line) -> {
|
|
|
|
input := line.trim
|
|
|
|
num := (input, 1).substring.trim.parse_float.try(
|
|
|
|
(val) -> val
|
|
|
|
() -> 0.0
|
|
|
|
)
|
|
|
|
mode := input.substring(0, 1)
|
|
|
|
if mode.eq("+") {
|
|
|
|
¤t = (current, num).sum
|
|
|
|
} else if mode.eq("-") {
|
|
|
|
¤t = (current, (num, -1).product).sum
|
|
|
|
} else if mode.eq("*") {
|
|
|
|
¤t = (current, num).product
|
|
|
|
} else if mode.eq("/") {
|
|
|
|
¤t = (current, num).div
|
|
|
|
} else if mode.eq("=") {
|
|
|
|
¤t = num
|
|
|
|
} else if input.eq("exit") {
|
|
|
|
(())
|
|
|
|
} else {
|
|
|
|
"Expected one of +-*/= followed by a number".eprintln
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// CTRL+D
|
|
|
|
() -> (())
|
2024-03-22 16:50:01 +01:00
|
|
|
)
|
2024-02-22 20:21:11 +01:00
|
|
|
}
|