RosettaCodeData/Task/24-game/EasyLang/24-game.easy
2026-04-30 12:34:36 -04:00

68 lines
1.4 KiB
Text

print "Enter an equation in RPN form using all of, and only "
print "the following single digits which evaluates to 24:"
func game .
len cnt[] 9
write ">> "
for i to 4
h = random 1 9
write h & " "
cnt[h] += 1
.
print ""
s$ = input
print s$
if s$ = ""
return 1
.
for c$ in strchars s$
if c$ = "+" or c$ = "-" or c$ = "*" or c$ = "/"
if len st[] < 2
print "Stack empty"
return 1
.
if c$ = "+"
st[$ - 1] = st[$ - 1] + st[$]
elif c$ = "-"
st[$ - 1] = st[$ - 1] - st[$]
elif c$ = "*"
st[$ - 1] = st[$ - 1] * st[$]
else
st[$ - 1] = st[$ - 1] / st[$]
.
len st[] -1
elif c$ <> " "
h = strcode c$ - 48
if h < 1 or h > 9
print "Wrong command " & c$
return 0
.
if cnt[h] = 0
print "Wrong number " & h
return 0
.
cnt[h] -= 1
st[] &= h
.
.
for c in cnt[]
s += c
.
if s > 0
print "Not all numbers used"
return 0
.
if len st[] > 1
print "Calculation not finished"
return 0
.
print st[1]
if abs (st[1] - 24) < 1e-10
print "Well done"
else
print "Wrong result"
.
.
repeat
print ""
until game = 1
.