REM ============================================
REM Author: Phaelax
REM Challenge: Roman Numeral Conversion
REM ============================================
 
dim romanNumeralValues(13)
dim romanNumeralSymbols$(13)
romanNumeralValues(1) = 1000 : romanNumeralValues(2) = 900 : romanNumeralValues(3) = 500
romanNumeralValues(4) = 400  : romanNumeralValues(5) = 100 : romanNumeralValues(6) = 90
romanNumeralValues(7) = 50   : romanNumeralValues(8) = 40  : romanNumeralValues(9) = 10 
romanNumeralValues(10) = 9   : romanNumeralValues(11) = 5  : romanNumeralValues(12) = 4
romanNumeralValues(13) = 1
romanNumeralSymbols$(1) = "M" : romanNumeralSymbols$(2) = "CM" : romanNumeralSymbols$(3) = "D"
romanNumeralSymbols$(4) = "CD" : romanNumeralSymbols$(5) = "C" : romanNumeralSymbols$(6) = "XC"
romanNumeralSymbols$(7) = "L" : romanNumeralSymbols$(8) = "XL" : romanNumeralSymbols$(9) = "X"
romanNumeralSymbols$(10) = "IX" : romanNumeralSymbols$(11) = "V" : romanNumeralSymbols$(12) = "IV"
romanNumeralSymbols$(13) = "I"
 
 
print "Convert to Roman:   ",arabicToRoman$(2369)
print "Convert to Arabic:  ",romanToArabic("MMCCCLXIX")
 
wait key
end
 
 
REM ============================================
REM Converts arabic digits to a roman numeral string
REM ============================================
function arabicToRoman$(arabicVal)
	roman$ = ""
	for i = 1 to 13
		while arabicVal >= romanNumeralValues(i)
			arabicVal = arabicVal - romanNumeralValues(i)
			roman$ = roman$ + romanNumeralSymbols$(i)
		endwhile
	next i
endfunction roman$
 
 
REM ============================================
REM Converts a roman numeral string to arabic digits
REM ============================================
function romanToArabic(roman$)
	roman$ = upper$(roman$)
	I = 1
	V = 5
	X = 10
	L = 50
	C = 100
	D = 500
	M = 1000	
 
	arabic = 0
	oldVal = 0
	for k = 1 to len(roman$)
		c$ = mid$(roman$, k)
		select c$
			case "M":
				if oldVal < M : inc arabic, (M-oldVal*2) : else : inc arabic, M : endif
				oldVal = M
			endcase
			case "D":
				if oldVal < D : inc arabic, (D-oldVal*2) : else : inc arabic, D : endif
				oldVal = D
			endcase
			case "C":
				if oldVal < C : inc arabic, (C-oldVal*2) : else : inc arabic, C : endif
				oldVal = C
			endcase
			case "L":
				if oldVal < L : inc arabic, (L-oldVal*2) : else : inc arabic, L : endif
				oldVal = L
			endcase
			case "X":
				if oldVal < X : inc arabic, (X-oldVal*2) : else : inc arabic, X : endif
				oldVal = X
			endcase
			case "V":
				if oldVal < V : inc arabic, (V-oldVal*2) : else : inc arabic, C : endif
				oldVal = V
			endcase
			case "I":
				if oldVal < I : inc arabic, (I-oldVal*2) : else : inc arabic, I : endif
				oldVal = I
			endcase
		endselect
	next k
endfunction arabic