RosettaCodeData/Task/24-game/11l/24-game.11l
2024-04-19 16:56:29 -07:00

59 lines
1.3 KiB
Text
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

T Error
String message
F (message)
.message = message
T RPNParse
[Float] stk
[Int] digits
F op(f)
I .stk.len < 2
X.throw Error(Improperly written expression)
V b = .stk.pop()
V a = .stk.pop()
.stk.append(f(a, b))
F parse(s)
L(c) s
I c C 0..9
.stk.append(Float(c))
.digits.append(Int(c))
E I c == + {.op((a, b) -> a + b)}
E I c == - {.op((a, b) -> a - b)}
E I c == * {.op((a, b) -> a * b)}
E I c == / {.op((a, b) -> a / b)}
E I c !=
X.throw Error(Wrong char: c)
F get_result()
I .stk.len != 1
X.throw Error(Improperly written expression)
R .stk.last
[Int] digits
print(Make 24 with the digits:, end' )
L 4
V n = random:(1..9)
print( n, end' )
digits.append(n)
print()
V parser = RPNParse()
X.try
parser.parse(input())
V r = parser.get_result()
I sorted(digits) != sorted(parser.digits)
print(Error: Not using the given digits)
E
print(Result: r)
I r C 23.999<.<24.001
print(Good job!)
E
print(Try again.)
X.catch Error error
print(Error: error.message)