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,17 @@
roman: procedure
arg number
/* handle only 1 to 3999, else return ? */
if number >= 4000 | number <= 0 then return "?"
romans = " M CM D CD C XC L XL X IX V IV I"
arabic = "1000 900 500 400 100 90 50 40 10 9 5 4 1"
result = ""
do i = 1 to words(romans)
do while number >= word(arabic,i)
result = result || word(romans,i)
number = number - word(arabic,i)
end
end
return result

View file

@ -0,0 +1,64 @@
/*REXX program converts (Arabic) decimal numbers (≥0) ──► Roman numerals*/
numeric digits 10000 /*could be higher if wanted*/
parse arg nums
if nums='' then do /*not specified? Gen some.*/
do j=0 by 11 to 111
nums=nums j
end /*j*/
nums=nums 49
do k=88 by 100 to 1200
nums=nums k
end /*k*/
nums=nums 1000 2000 3000 4000 5000 6000
do m=88 by 200 to 1200
nums=nums m
end /*m*/
nums=nums 1304 1405 1506 1607 1708 1809 1910 2011
do p=4 to 50 /*there is no limit to this*/
nums=nums 10**p
end /*p*/
end /*end generation of numbers*/
do i=1 for words(nums); x=word(nums,i)
say right(x,55) dec2rom(x)
end /*i*/
exit /*stick a fork in it, we're done.*/
/*───────────────────────────DEC2ROM subroutine─────────────────────────*/
dec2rom: procedure; parse arg n,# /*get number, assign # to a null. */
n=space(translate(n,,','),0) /*remove any commas from number. */
nulla='ZEPHIRUM NULLAE NULLA NIHIL' /*Roman words for nothing or none.*/
if n==0 then return word(nulla,1) /*return a Roman word for zero. */
maxnp=(length(n)-1)%3 /*find max(+1) # of parens to use.*/
highPos=(maxnp+1)*3 /*highest position of number. */
nn=reverse(right(n,highPos,0)) /*digits for Arabic───►Roman conv.*/
nine=9
four=4; do j=highPos to 1 by -3
_=substr(nn,j,1); select
when _==nine then hx='CM'
when _>= 5 then hx='D'copies("C",_-5)
when _==four then hx='CD'
otherwise hx=copies('C',_)
end
_=substr(nn,j-1,1); select
when _==nine then tx='XC'
when _>= 5 then tx='L'copies("X",_-5)
when _==four then tx='XL'
otherwise tx=copies('X',_)
end
_=substr(nn,j-2,1); select
when _==nine then ux='IX'
when _>= 5 then ux='V'copies("I",_-5)
when _==four then ux='IV'
otherwise ux=copies('I',_)
end
xx=hx || tx || ux
if xx\=='' then #=# ||copies('(',(j-1)%3)xx ||copies(')',(j-1)%3)
end /*j*/
if pos('(I',#)\==0 then do i=1 for 4 /*special case: M,MM,MMM,MMMM.*/
if i==4 then _ = '(IV)'
else _ = '('copies("I",i)')'
if pos(_,#)\==0 then #=changestr(_,#,copies('M',i))
end /*i*/
return #