RosettaCodeData/Task/Four-is-magic/M2000-Interpreter/four-is-magic.m2000
2025-02-27 18:35:13 -05:00

67 lines
1.7 KiB
Text

module Four_is_magic {
numname=lambda ->{
flush
data "", "one", "two", "three", "four"
data "five", "six", "seven", "eight", "nine", "ten"
data "eleven", "twelve", "thirteen", "fourteen", "fifteen"
data "sixteen", "seventeen","eighteen", "nineteen"
dim lows(), tens(), lev()
lows()=array([])
data "", "", "twenty", "thirty", "forty","fifty", "sixty"
data "seventy", "eighty", "ninety"
tens()=array([])
lev()=("", "thousand", "million", "billion")
=lambda lows(), tens(), lev() (n as long) -> {
if n=0 then ="zero": exit
long i, tr : boolean t : string ret, prefix
if n < 0 then prefix= "negative ": n-! else prefix=""
while n>0 {push n mod 1000: n|div 1000}
t=stack.size>1
while not empty {
tripn="" : read tr : if tr=0 then continue
lt= tr mod 100 : h= tr div 100
if lt<20 then
tripn+=lows(lt)
else
tripn=tens(lt div 10)+if$(lt mod 10 >0 ->"-"+lows(lt mod 10),"")
end if
if h>0 then tripn = lows(h)+" hundred " + tripn
if empty then if t and h = 0 then tripn = " and " + tripn
ret+=tripn+" "+lev(stack.size)+" "
}
=prefix+trim$(ret)
}
}()
TitleStr=lambda (s as string) ->{
=ucase$(left$(s,1))+mid$(s, 2)
}
magic=lambda numname, TitleStr (n as integer)-> {
first$=numname(n)
count$=numname(len(first$))
while first$<>"four"
data first$+" is "+count$
swap first$, count$
count$=numname(len(first$))
end while
data "four is magic."
=TitleStr(array([])#str$(", "))
}
document doc$
for i=0 to 9
doc$ = magic(i)+{
}
next
doc$ = magic(23)+{
}
doc$ = magic(130)+{
}
doc$ = magic(151)+{
}
doc$ = magic(-7)+{
}
doc$ = magic(20140)+{
}
report doc$
clipboard doc$
}
Four_is_magic