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.




 
SCREEN _NEWIMAGE(400, 600, 32)
 
 
CLS
 
 
Main:
'------------------------------------------------
' CALLS THE romToDec FUNCTION WITH THE ROMAN
' NUMERALS AND RETURNS ITS DECIMAL EQUIVELENT.
'
   PRINT "ROMAN NUMERAL TO DECIMAL CONVERSION"
   PRINT: PRINT
 
   PRINT "MDCCIV  = "; romToDec("MDCCIV") '1704
   PRINT "MCMXC   = "; romToDec("MCMXC") '1990
   PRINT "MMVIII  = "; romToDec("MMVIII") '2008
   PRINT "MDCLXVI = "; romToDec("MDCLXVI") '1666
   PRINT: PRINT
   PRINT "Here are other solutions not from the TASK:"
   PRINT "MCMXCIX = "; romToDec("MCMXCIX") '1999
   PRINT "XXV     = "; romToDec("XXV") '25
   PRINT "CMLIV   = "; romToDec("CMLIV") '954
   PRINT "MMXI    = "; romToDec("MMXI") '2011
   PRINT "MMIIIX  = "; romToDec("MMIIIX") '2011
   PRINT: PRINT
   PRINT "2011 can be written either as MMXI or MMIIIX"
   PRINT "With the IX = 9, MMIIIX is also 2011."
   PRINT "2011 IS CORRECT (MM=2000 + II = 2 + IX = 9)"
 
   END
 
 
 
FUNCTION romToDec (roman AS STRING)
'------------------------------------------------------
' FUNCTION THAT CONVERTS ANY ROMAN NUMERAL TO A DECIMAL
'
    prenum = 0: num = 0
    LN = LEN(roman)
    FOR i = LN TO 1 STEP -1
        x$ = MID$(roman, i, 1)
        n = 1000
        SELECT CASE x$
               CASE "M": n = n / 1
               CASE "D": n = n / 2
               CASE "C": n = n / 10
               CASE "L": n = n / 20
               CASE "X": n = n / 100
               CASE "V": n = n / 200
               CASE "I": n = n / n
               CASE ELSE: n = 0
        END SELECT
        IF n < prenum THEN num = num - n ELSE num = num + n
        prenum = n
   NEXT i
 
   romToDec = num
 
END FUNCTION
 