tasks a-s
This commit is contained in:
parent
47bf37c096
commit
b83f433714
12433 changed files with 156208 additions and 123 deletions
63
Task/Roman-numerals-Decode/REXX/roman-numerals-decode-1.rexx
Normal file
63
Task/Roman-numerals-Decode/REXX/roman-numerals-decode-1.rexx
Normal file
|
|
@ -0,0 +1,63 @@
|
|||
/* Rexx */
|
||||
|
||||
Do
|
||||
/* 1990 2008 1666 */
|
||||
years = 'MCMXC MMVIII MDCLXVI'
|
||||
|
||||
Do y_ = 1 to words(years)
|
||||
Say right(word(years, y_), 10) || ':' decode(word(years, y_))
|
||||
End y_
|
||||
|
||||
Return
|
||||
End
|
||||
Exit
|
||||
|
||||
decode:
|
||||
Procedure
|
||||
Do
|
||||
Parse upper arg roman .
|
||||
|
||||
If verify(roman, 'MDCLXVI') = 0 then Do
|
||||
|
||||
/* always insert the value of the least significant numeral */
|
||||
decnum = rchar(substr(roman, length(roman), 1))
|
||||
Do d_ = 1 to length(roman) - 1
|
||||
If rchar(substr(roman, d_, 1)) < rchar(substr(roman, d_ + 1, 1)) then Do
|
||||
/* Handle cases where numerals are not in descending order */
|
||||
/* subtract the value of the numeral */
|
||||
decnum = decnum - rchar(substr(roman, d_, 1))
|
||||
End
|
||||
else Do
|
||||
/* Normal case */
|
||||
/* add the value of the numeral */
|
||||
decnum = decnum + rchar(substr(roman, d_, 1))
|
||||
End
|
||||
End d_
|
||||
End
|
||||
else Do
|
||||
decnum = roman 'contains invalid roman numerals'
|
||||
End
|
||||
|
||||
Return decnum
|
||||
End
|
||||
Exit
|
||||
|
||||
rchar:
|
||||
Procedure
|
||||
Do
|
||||
Parse upper arg ch +1 .
|
||||
|
||||
select
|
||||
when ch = 'M' then digit = 1000
|
||||
when ch = 'D' then digit = 500
|
||||
when ch = 'C' then digit = 100
|
||||
when ch = 'L' then digit = 50
|
||||
when ch = 'X' then digit = 10
|
||||
when ch = 'V' then digit = 5
|
||||
when ch = 'I' then digit = 1
|
||||
otherwise digit = 0
|
||||
end
|
||||
|
||||
Return digit
|
||||
End
|
||||
Exit
|
||||
30
Task/Roman-numerals-Decode/REXX/roman-numerals-decode-2.rexx
Normal file
30
Task/Roman-numerals-Decode/REXX/roman-numerals-decode-2.rexx
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
/*REXX program to convert Roman numeral number(s) to Arabic number(s). */
|
||||
|
||||
rYear='MCMXC' ; say right(rYear,9)':' rom2dec(rYear)
|
||||
rYear='mmviii' ; say right(rYear,9)':' rom2dec(rYear)
|
||||
rYear='MDCLXVI' ; say right(rYear,9)':' rom2dec(rYear)
|
||||
exit
|
||||
|
||||
rom2dec: procedure; arg roman .
|
||||
if verify(roman,'MDCLXVI')\==0 then do
|
||||
say 'invalid Roman number:' roman
|
||||
return '***error!***'
|
||||
end
|
||||
#=rChar(right(roman,1)) /*start with the last numeral*/
|
||||
do j=1 for length(roman)-1
|
||||
x=rChar(substr(roman,j ,1)) /*the current Roman numeral. */
|
||||
y=rChar(substr(roman,j+1,1)) /*the next Roman numeral. */
|
||||
if x<y then x=-x /*if x<y, then subtract, ¬add*/
|
||||
#=#+x
|
||||
end
|
||||
return #
|
||||
|
||||
rChar: procedure; arg _ /*convert a Roman number to an Arabic dig*/
|
||||
if _=='M' then return 1000
|
||||
if _=='D' then return 500
|
||||
if _=='C' then return 100
|
||||
if _=='L' then return 50
|
||||
if _=='X' then return 10
|
||||
if _=='V' then return 5
|
||||
if _=='I' then return 1
|
||||
return 0
|
||||
22
Task/Roman-numerals-Decode/REXX/roman-numerals-decode-3.rexx
Normal file
22
Task/Roman-numerals-Decode/REXX/roman-numerals-decode-3.rexx
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
/*REXX program to convert Roman numerals ──► Arabic numerals (numbers).*/
|
||||
numeric digits 1000 /*we can handle big nums.*/
|
||||
y = 'MCMXC' ; say right(y,9)':' rom2dec(y)
|
||||
y = 'mmviii' ; say right(y,9)':' rom2dec(y)
|
||||
y = 'MDCLXVI' ; say right(y,9)':' rom2dec(y)
|
||||
y = 'MDQLXVI' ; say right(y,9)':' rom2dec(y)
|
||||
exit /*stick a fork in it, we're done.*/
|
||||
/*──────────────────────────────────ROM2DEC subroutine──────────────────*/
|
||||
rom2dec: procedure; h='0'x; #=0; $=1; arg n . /*uppercase N.*/
|
||||
n=translate(n,'()()',"[]{}"); _ = verify(n,'MDCLXVUIJ()')
|
||||
if _\==0 then return '***error!*** invalid Roman numeral:' substr(n,_,1)
|
||||
@.=1; @.m=1000; @.d=500; @.c=100; @.l=50; @.x=10; @.u=5; @.v=5
|
||||
|
||||
do k=length(n) to 1 by -1; _=substr(n,k,1) /*examine a Roman numeral*/
|
||||
/*(next) scale up or down*/
|
||||
if _=='(' | _==')' then do; $=$*1000; if _=='(' then $=1; iterate; end
|
||||
_=@._*$ /*scale it if necessary. */
|
||||
if _>h then h=_ /*remember Roman numeral.*/
|
||||
if _<h then #=#-_ /*char>next? Then sub. */
|
||||
else #=#+_ /* else add. */
|
||||
end /*k*/
|
||||
return # /*return Arabic number. */
|
||||
Loading…
Add table
Add a link
Reference in a new issue