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.




// Main program for testing the function
//
do {
    Get_Input(10, "Enter a roman numeral: ", NOCR+STATLINE)
    Call("Roman_to_Arabic")
    Reg_Type(10) Message("	= ") Num_Type(#1)
} while(#1)
Return
 
// Convert Roman numeral into numeric value
//  in:  @10 = Roman numeral
//  out: #1  = numeric value
//
:Roman_to_Arabic:
    Buf_Switch(Buf_Free)
    Ins_Text("M1000 D500 C100 L50 X10 V5 I1") Ins_Newline
    Reg_Ins(10) Ins_Char(' ')
    #1 = #2 = 0
 
    Repeat(ALL) {
        #3 = #2                                  // #3 = previous character
        Goto_Line(2)                             // roman numeral to be converted
        if (At_EOL) {
            Break                                // all done
        }
        Reg_Copy_Block(11, CP, CP+1, DELETE)     // next character in roman numeral
        if (Search(@11, BEGIN+ADVANCE+NOERR)) {  // find character from the table
            #2 = Num_Eval(SUPPRESS)              // corresponding numeric value
            if (#2 > #3) {                       // larger than previous digit?
                #1 -= #3                         // substract previous digit
            } else {
                #1 += #3                         // add previous digit
            }
        }
    }
    Reg_Empty(11)
    Buf_Quit(OK)
Return