function [x]=roman2decimal(s)

s1=substring(s,1,1);

s2=substring(s,2,2);

s=substring(s,3,numel(s));

numbers1=[4 9 40 90 400 900];

letters1=['I' 'I' 'X' 'X' 'C' 'C'];

letters2=['V' 'X' 'L' 'C' 'D' 'M'];

numbers3=[ 1 5 10 50 100 500 1000];

letters3=['I' 'V' 'X' 'L' 'C' 'D' 'M'];

x=0;

s1=substring(s,1,1), s2=substring(s,2,2), s=substring(s,3,numel(s));

while (s~='')

case1=false;

case2=false;

i = 1;

while(i<=numel(numbers1) &~case1)

if (strcmp(s1,letters1(i)) & strcmp(s2,letters2(i)))

case1=true

x = x+numbers1(i)

end

end

if(case1)

s1=substring(s,1,1), s2=substring(s,2,2), s=substring(s,3,length(s))

else

while(i<= numel(numbers3) & ~case2)

if (strcmp(s1,letters3(i)))

case2=true

x = x+numbers3(i)

end

end

end

if(case2),

s1=s2;

s2=substring(s,1,1),

s=substring(s,2,length(s));

if(~case1 && ~case2) error('Incorrect roman numeral')

end

end

end

## 0 Comments

Sign in to comment.