Microsoft Small Basic

Program Listing: RPC590
' Challenge of the Month - October 2014 // convert between Roman Numerals and numbers by NaochanON
'Roman Numerals is 1 to 3999. Over 4000 numbers are not available.
'1 2 3 4 5 6 7 8 9 =I II III IV V VI VII VIII IX
'10 20 30 40 50 60 70 80 90= X XX XXX XL L LX LXX LXXX XC
'100 200 300 400 500 600 700 800 900= C CC CCC CD D DC DCC DCCC CM
'1000 2000 3000= M MM MMM
'11 = XI 12 = XII 14 = XIV 18 = XVIII 24 = XXIV 43 = XLIII 99 = XCIX 495 = CDXCV
'1888 = MDCCCLXXXVIII 1945 = MCMXLV 3999 = MMMCMXCIX
'---------------------------------------------------------------------------------------
Label[1]="1=I;2=II;3=III;4=IV;5=V;6=VI;7=VII;8=VIII;9=IX"
Label[10]="1=X;2=XX;3=XXX;4=XL;5=L;6=LX;7=LXX;8=LXXX;9=XC"
Label[100]="1=C;2=CC;3=CCC;4=CD;5=D;6=DC;7=DCC;8=DCCC;9=CM"
Label[1000]="1=M;2=MM;3=MMM"
div="1=1000;2=100;3=10;4=1"

Controls.ButtonClicked=onclicked
init()

Sub onclicked
If Controls.LastClickedButton=Btn1 Then
RNMB=""
DecNMB= Controls.GetTextBoxText(Decimal) '
If 0< DecNMB And DecNMB<4000 Then
For i=1 To 4
D= Math.Floor(DecNMB/div[i])
RNMB=text.Append(RNMB, Label[div[i]][D])
DecNMB=Math.Remainder(DecNMB,div[i])
endfor
Controls.SetTextBoxText(Roman,RNMB)
Else
Controls.SetTextBoxText(Roman,"Error. Input 1-3999 ")
EndIf
elseIf Controls.LastClickedButton=Btn2 Then
RNMB= Controls.GetTextBoxText(Roman)
decNMB=0
For i=1 To 4
For j= array.GetItemCount(Label[div[i]]) To 1 step -1
If Text.StartsWith(RNMB,Label[div[i]][j])="True" Then
decNMB=decNMB+div[i]*j
RNMB=text.GetSubTextToEnd(RNMB, Text.GetLength(Label[div[i]][j]) +1)
j=0
EndIf
EndFor
EndFor
Controls.SetTextBoxText(Decimal,decNMB)
EndIf
EndSub

Sub init
GraphicsWindow.BackgroundColor="Darkgreen"
GraphicsWindow.Width=350
GraphicsWindow.Height=200
GraphicsWindow.FontSize=18
Decimal= Controls.AddTextBox(110,10)
Controls.SetSize(Decimal,200,35)
Roman= Controls.AddTextBox(110,50)
Controls.SetSize(Roman,200,35)
GraphicsWindow.FontSize=13
Btn1= Controls.AddButton("Decimal-->Roman",50,100)
Btn2= Controls.AddButton("Roman-->Decimal",200,100)
GraphicsWindow.FontSize=18
GraphicsWindow.BrushColor="Yellow"
GraphicsWindow.DrawText(10,10,"Decimal")
GraphicsWindow.DrawText(10,50,"Roman")
GraphicsWindow.DrawText(5,150,"Input decimal number 1--3999,"+Text.GetCharacter(10)+" Roman number I--MMMCMXCIX ")
EndSub