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:
!------------------------------------------------
! CALLS THE romToDec FUNCTION WITH THE ROMAN
! NUMERALS AND RETURNS ITS DECIMAL EQUIVELENT.
!
 
   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:PRINT
   PRINT "Without error checking, this also is 2011, but is wrong"
   PRINT "MMIIIX  = "; romToDec("MMIIIX")  !INVAID, 2011
 
STOP
 
 
FUNCTION romToDec(roman AS STRING) AS INTEGER
!------------------------------------------------------
! 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
 