Roman numerals/Decode: Difference between revisions

Line 3:
 
Modern Roman numerals are written by expressing each digit separately starting with the left most digit and skipping any digit with a value of zero. In Roman numerals 1990 is rendered: 1000=M, 900=CM, 90=XC; resulting in MCMXC. 2008 is written as 2000=MM, 8=VIII; or MMVIII. 1666 uses each Roman symbol in descending order: MDCLXVI.
 
=={{header|Icon}} and {{header|Unicon}}==
<lang Icon>link numbers
 
procedure main()
every R := "MCMXC"|"MDCLXVI"|"MMVIII" do
write(R, " = ",unroman(R))
end</lang>
 
{{libheader|Icon Programming Library}}
[http://www.cs.arizona.edu/icon/library/src/procs/numbers.icn numbers.icn provides unroman]
 
The code for this procedure is copied below:<lang Icon>procedure unroman(s) #: convert Roman numeral to integer
local nbr,lastVal,val
 
nbr := lastVal := 0
s ? {
while val := case map(move(1)) of {
"m": 1000
"d": 500
"c": 100
"l": 50
"x": 10
"v": 5
"i": 1
} do {
nbr +:= if val <= lastVal then val else val - 2 * lastVal
lastVal := val
}
}
return nbr
end</lang>
 
 
Output:<pre>MCMXC = 1990
MDCLXVI = 1666
MMVIII = 2008</pre>
 
 
=={{header|J}}==
Anonymous user