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.




BEGIN
 
    INTEGER PROCEDURE FROMROMAN(S); TEXT S;
    BEGIN
        PROCEDURE P(INTVAL, NUM); INTEGER INTVAL; TEXT NUM;
        BEGIN
            INTEGER NLEN;
            NLEN := NUM.LENGTH;
            WHILE INDEX + NLEN - 1 <= SLEN AND THEN
                S.SUB(INDEX, NLEN) = NUM DO
            BEGIN
                RESULT := RESULT + INTVAL;
                INDEX := INDEX + NLEN;
            END WHILE;
        END P;
        INTEGER RESULT, INDEX, SLEN;
        SLEN := S.LENGTH;
        INDEX := 1;
        P( 1000, "M"  );
        P(  900, "CM" );
        P(  500, "D"  );
        P(  400, "CD" );
        P(  100, "C"  );
        P(   90, "XC" );
        P(   50, "L"  );
        P(   40, "XL" );
        P(   10, "X"  );
        P(    9, "IX" );
        P(    5, "V"  );
        P(    4, "IV" );
        P(    1, "I"  );
        FROMROMAN := RESULT;
    END FROMROMAN;
 
    TEXT T;
    FOR T :- "MCMXC", "MMVIII", "MDCLXVI" DO
    BEGIN
        OUTTEXT("ROMAN """);
        OUTTEXT(T);
        OUTTEXT(""" => ");
        OUTINT(FROMROMAN(T), 0);
        OUTIMAGE;
    END FOR;
 
END PROGRAM;
 