tasks a-s

This commit is contained in:
Ingy döt Net 2013-04-10 23:57:08 -07:00
parent 47bf37c096
commit b83f433714
12433 changed files with 156208 additions and 123 deletions

View file

@ -0,0 +1,70 @@
SUBROUTINE NumberToWords(number)
CHARACTER outP*255, small*130, tens*80, big*80
REAL :: decimal_places = 7
INIT( APPENDIX("#literals"), small, tens, big)
num = ABS( INT(number) )
order = 0
outP = ' '
DO i = 1, num + 1
tmp = MOD(num, 100)
IF(tmp > 19) THEN
EDIT(Text=tens, ITeM=INT(MOD(tmp/10, 10)), Parse=medium)
IF( MOD(tmp, 10) ) THEN
EDIT(Text=small, ITeM=MOD(tmp,10)+1, Parse=mini)
outP = medium // '-' // mini // ' ' // outP
ELSE
outP = medium // ' ' // outP
ENDIF
ELSEIF(tmp > 0) THEN
EDIT(Text=small, ITeM=tmp+1, Parse=mini)
outP = mini // ' '// outP
ELSEIF(number == 0) THEN
outP = 'zero'
ENDIF
tmp = INT(MOD(num, 1000) / 100)
IF(tmp) THEN
EDIT(Text=small, ITeM=tmp+1, Parse=oneto19)
outP = oneto19 // ' hundred ' // outP
ENDIF
num = INT(num /1000)
IF( num == 0) THEN
IF(number < 0) outP = 'minus ' // outP
fraction = ABS( MOD(number, 1) )
IF(fraction) WRITE(Text=outP, APPend) ' point'
DO j = 1, decimal_places
IF( fraction >= 10^(-decimal_places) ) THEN
num = INT( 10.01 * fraction )
EDIT(Text=small, ITeM=num+1, Parse=digit)
WRITE(Text=outP, APPend) ' ', digit
fraction = 10*fraction - num
ENDIF
ENDDO
OPEN(FIle="temp.txt", APPend)
WRITE(FIle="temp.txt", Format='F10, " = ", A', CLoSe=1) number, outP
RETURN
ENDIF
order = order + 1
EDIT(Text=big, ITeM=order, Parse=kilo)
IF( MOD(num, 1000) ) outP = kilo // ' and '// outP
ENDDO
END
CALL NumberToWords( 0 )
CALL NumberToWords( 1234 )
CALL NumberToWords( 1234/100 )
CALL NumberToWords( 10000000 + 1.2 )
CALL NumberToWords( 2^15 )
CALL NumberToWords( 0.001 )
CALL NumberToWords( -EXP(1) )
#literals
SMALL= zero one two three four five six seven eight nine ten &
eleven twelve thirteen fourteen fifteen sixteen seventeen eighteen nineteen
TENS=ten twenty thirty forty fifty sixty seventy eighty ninety
BIG=thousand million billion trillion quadrillion

View file

@ -0,0 +1,7 @@
0 = zero
1234 = one thousand and two hundred thirty-four
12.34 = twelve point three four
10000001.2 = ten million and one point two
32768 = thirty-two thousand and seven hundred sixty-eight
1E-3 = point zero zero one
-2.7182818 = minus two point seven one eight two eight one eight

View file

@ -0,0 +1,6 @@
link numbers # commas, spell
procedure main(arglist)
every x := !arglist do
write(commas(x), " -> ",spell(x))
end

View file

@ -0,0 +1,37 @@
procedure spell(n) #: spell out integer (short scale)
local m, i
static scale
initial {
scale := [ "thousand", "million", "billion", "trillion", "quadrillion", "quintillion", "sextillion","septillion"]
every scale[i := 1 to *scale ] := [ integer(repl("999",i + 1)), -3 * i, " "||scale[i] ]
push(scale,[999,2," hundred"])
}
n := integer(n) | stop(image(n)," is not an integer")
if n < 0 then return "negative " || spell(-n)
if n <= 12 then return {
"0zero,1one,2two,3three,4four,5five,6six,7seven,8eight,_
9nine,10ten,11eleven,12twelve," ? {
tab(find(n))
move(*n)
tab(find(","))
}
}
else if n <= 19 then return {
spell(n[2] || "0") ?
(if ="for" then "four" else tab(find("ty"))) || "teen"
}
else if n <= 99 then return {
"2twen,3thir,4for,5fif,6six,7seven,8eigh,9nine," ? {
tab(find(n[1]))
move(1)
tab(find(",")) || "ty" ||
(if n[2] ~= 0 then "-" || spell(n[2]) else "")
}
}
else if n <= scale[i := 1 to *scale,1] then return { # generalize based on scale
spell(n[1:scale[i,2]]) || scale[i,3] ||
(if (m := n[scale[i,2]:0]) ~= 0 then " and " || spell(m) else "")
}
else fail # really big
end