|
alias romain {
var %a $replace($1,I,1,V,5,X,10,L,50,C,100,D,500,M,1000),%e $1,%f,%h,%i 1,%j,%m 1,%n tokenize 8
if $1 isnum {
while %e {
while %m < 11 {
%n $gettok(1000M500D100C50L10X5V4IIII3III2II1I,%m,5)
if %e >= $1 {
%f = $+($2,,$1)
break
}
inc %m
}
%n %f
%h = %h $+ $1
dec %e $2
}
while $mid(%h,%i,4) {
var %f $ifmatch,%g,%e $right(%j,1)
if (%f = IIII) %g = $iif(%e = V,IX,%e $+ IV)
if (%f = XXXX) %g = $iif(%e = L,XC,%e $+ XL)
if (%f = CCCC) %g = $iif(%e = D,CM,%e $+ CD)
if %g {
%j = $left(%j,-1) $+ %g
inc %i 3
}
else %j = %j $+ $left(%f,1)
inc %i
}
return %j
}
else {
%n %a
while $($+($,%i),2) {
var %c $ifmatch,%f %i + 1,%h = $+(%h,$iif(%c < $($+($,%f),2),-,+),%c)
inc %i
}
return $calc(%h)
}
}
| |