Task

Create a function that takes a Roman numeral as its argument and returns its value as a numeric decimal integer.

You don't need to validate the form of the Roman numeral.

Modern Roman numerals are written by expressing each decimal digit of the number to be encoded separately,

starting with the leftmost decimal digit and skipping any 0s   (zeroes).

1990 is rendered as   MCMXC     (1000 = M,   900 = CM,   90 = XC)     and

2008 is rendered as   MMVIII       (2000 = MM,   8 = VIII).

The Roman numeral for 1666,   MDCLXVI,   uses each letter in descending order.




$ include "seed7_05.s7i";
 
const func integer: ROMAN parse (in string: roman) is func
  result
    var integer: arabic is 0;
  local
    var integer: index is 0;
    var integer: number is 0;
    var integer: lastval is 0;
  begin
    for index range length(roman) downto 1 do
      case roman[index] of
        when {'M', 'm'}: number := 1000;
        when {'D', 'd'}: number :=  500;
        when {'C', 'c'}: number :=  100;
        when {'L', 'l'}: number :=   50;
        when {'X', 'x'}: number :=   10;
        when {'V', 'v'}: number :=    5;
        when {'I', 'i'}: number :=    1;
        otherwise:       raise RANGE_ERROR;
      end case;
      if number < lastval then
        arabic -:= number;
      else
        arabic +:= number;
      end if;
      lastval := number;
    end for;
  end func;
 
const proc: main is func
  begin
    writeln(ROMAN parse "MCMXC");
    writeln(ROMAN parse "MMVIII");
    writeln(ROMAN parse "MDCLXVI");
  end func;