Roman numerals are converted mathematically by simply assigning a numerical value to each letter and calculating a total where:
Letters are arranged from left to right in order of decreasing value. The total is the numerical values of all the letters in the sequence. For example,M = 1000 D = 500 C = 100 L = 50 X = 10 V = 5 I = 1
However, the subtraction principle requires that a lower numeral appearing before a higher one be subtracted from the higher value, not added to the total. For example,XVI = 10 + 5 + 1 = 16
rather than 21, which is written asXIX = X + IX = 10 + 9 = 19
Similarly, the Roman numeral for the year 1948 is usually written asXXI = 10 + 10 + 1 = 21
MCMXLVIII = M + CM + XL + VIII = 1000 + 900 + 40 + 8 = 1948
Zeno 1.2 is an interpreter for the Zeno programming language. It is an easy to learn and is suitable for educational purposes.
function roman2int( str : string ) : int
var i, j, k : int
var val : int := 0
var len : int := strlen( str )
if len = 0 then
return -1
end if
k := -1
for i := 1...len do
% get char value
case str[i] of
value 'I', 'i': j := 1
value 'V', 'v': j := 5
value 'X', 'x': j := 10
value 'L', 'l': j := 50
value 'C', 'c': j := 100
value 'D', 'd': j := 500
value 'M', 'm': j := 1000
value: return -1
end case
% add increment
if (k >= 1) and (k < j) then
val := val - k*2
val := val + j
else
val := val + j
end if
% save prev char value
k := j
end for
return val
end function
function int2roman( in : int ) : string
var wh : string := "IVXLCDM"
var number, divisor : int
var i, j, k, p : int
var str : string
number := in
divisor := 1000
p := 1
for decreasing k := 3 ... 0 do
i := number div divisor
number := number mod divisor
case i of
value 0:
% do nothing
value 5:
str[p] := wh[2*k+2]
p := p + 1
value 9:
str[p] := wh[2*k+1]
p := p + 1
str[p] := wh[2*k+3]
p := p + 1
value 4:
str[p] := wh[2*k+1]
p := p + 1
str[p] := wh[2*k+2]
p := p + 1
value :
if i > 5 then
str[p] := wh[2*k+2]
p := p + 1
i := i - 5
end if
for j := 0 ... i-1 do
str[p] := wh[2*k+1]
p := p + 1
end for
end case
divisor := divisor div 10
end for
str[p] := chr( 0 )
return str
end function
integer? 1948 MCMXLVIII Roman numeral? MCMXLVIII 1948
AbCd Classics - free on-line books