Author |
Topic: Fun Code (Read 970 times) |
|
Optimax
New Member
member is offline


Posts: 11
|
 |
Re: Fun Code
« Reply #8 on: Apr 24th, 2016, 1:42pm » |
|
Bonjour,
I wrote something about roman to decimal numbers a long time ago, and I adapted it for LBB 3.04. This code is not as smart as the code with word$, but seems to work with very elementary insructions, which are not fundamentally different. Here comes the most elementary code. Errors warnings are gratefully accepted.
Code:INPUT "Roman number ? "; R$
R$ = UPPER$(R$)
PRINT R$
D = 0
FOR i = 1 TO 13
READ Rom$, Dec
DO
IF LEFT$(R$, LEN(Rom$)) = Rom$ THEN
D = D + Dec
R$ = MID$(R$, LEN(Rom$)+1)
ELSE
EXIT DO
END IF
LOOP
NEXT i
PRINT D
END
DATA "M", 1000, "CM", 900, "CD", 400, "D", 500, "XC", 90, "C", 100, "XL", 40, "L", 50, "X", 10, "IX", 9, "IV", 4, "V", 5, "I", 1
I wrote the same thing with some GUI, roman to decimal, and back:
Code:
' ROMAN-DECIMAL.BAS LBB 3.04
' ===================
'conversion from roman numbers to decimal numbers, and back
'positive integers only, max 9999 or "MMMMMMMMMCMXCIX"
'seems enough for quite a while
NOMAINWIN
'--- setting the controls
RadioButton #w.radio1, "ROMAN => DECIMAL", [EnterRomanNumber], , 20, 50, 150, 20
StyleBits #w.radio1, _WS_BORDER, 0,0,0
TextBox #w.ROMAN, 180, 50, 160, 25
StyleBits #w.ROMAN, _ES_UPPERCASE, 0, 0, 0
RadioButton #w.radio2, "DECIMAL => ROMAN", [EnterDecimalNumber], , 20, 100, 150, 20
StyleBits #w.radio2, _WS_BORDER, 0,0,0
TextBox #w.DECIMAL, 180, 100, 160, 25
StyleBits #w.DECIMAL, _ES_NUMBER, 0, 0, 0
Button #w.RAZ, "RESET", [Reset], UL, 50, 180, 50, 50
StyleBits #w.RESET, _WS_DLGFRAME, 0, 0, 0
Button #w.Default, "CALC", [Toggle], UL, 155, 180, 50, 50
StyleBits #w.Default, _WS_DLGFRAME, 0, 0, 0
Button #w.Quit, "QUIT", [EndProgram], UL, 260, 180, 50, 50
StyleBits #w.Quit, _WS_DLGFRAME, 0, 0, 0
'--- opening the window
UpperLeftX = 100: UpperLeftY = 150: WindowHeight = 300: WindowWidth = 370
OPEN "ROMAN 2 DECIMAL & DECIMAL 2 ROMAN" FOR DIALOG AS #w
PRINT #w "Font Arial 10"
PRINT #w "TrapClose [EndProgram]"
PRINT #w.radio1, "set"
PRINT #w.ROMAN, "!enable"
PRINT #w.ROMAN, "!setfocus"
WAIT
'--- and now the subroutines
[EnterRomanNumber]
D = 0
R$ = ""
PRINT #w.DECIMAL, ""
PRINT #w.DECIMAL, "!disable" 'avoiding unwanted input
PRINT #w.radio1, "set"
PRINT #w.ROMAN, "!enable"
PRINT #w.ROMAN, ""
PRINT #w.ROMAN, "!setfocus"
WAIT
[EnterDecimalNumber]
D = 0
R$ = ""
PRINT #w.ROMAN, ""
PRINT #w.ROMAN, "!disable" 'avoiding unwanted input
PRINT #w.DECIMAL, "!enable"
PRINT #w.DECIMAL, "!setfocus"
PRINT #w.DECIMAL, ""
WAIT
[Toggle]
PRINT #w.ROMAN, "!contents? R$";
IF R$ <> "" THEN GOTO [Rom2Dec]
PRINT #w.DECIMAL, "!contents? D$";
IF D$ <> "" THEN GOTO [Dec2Rom]
WAIT
''' --------------------------- ROMAN TO DECIMAL -----------------
[Rom2Dec]
RESTORE [RomanData]
D = 0
FOR i = 1 TO 13
READ Rom$, Dec
DO
IF LEFT$(R$, LEN(Rom$)) = Rom$ THEN
D = D + Dec
R$ = MID$(R$, LEN(Rom$)+1)
ELSE
EXIT DO
END IF
LOOP
NEXT i
IF R$ <> "" THEN
NOTICE "Error" + CHR$(13) + "Wrong Roman Number"
ELSE
PRINT #w.DECIMAL, "!enable"
PRINT #w.DECIMAL, STR$(D)
END IF
WAIT
''' -------------------- DECIMAL TO ROMAN ------------
[Dec2Rom]
R$ = ""
D = VAL(D$)
RESTORE [DecimalData]
FOR i = 1 TO 13
IF D = 0 THEN EXIT FOR
READ Rom$, Dec
DO
IF D >= Dec THEN
R$ = R$ + Rom$
D = D - Dec
ELSE
EXIT DO
END IF
LOOP
NEXT i
PRINT #w.ROMAN, "!enable"
PRINT #w.ROMAN, R$
WAIT
[Reset]
PRINT #w.radio2, "reset"
PRINT #w.radio1, "set"
PRINT #w.DECIMAL, ""
PRINT #w.ROMAN, "!enable"
PRINT #w.ROMAN, ""
PRINT #w.ROMAN, "!setfocus"
R$ = ""
D = 0
WAIT
[EndProgram]
CLOSE #w
END
[RomanData]
DATA "M", 1000, "CM", 900, "CD", 400, "D", 500, "XC", 90, "C", 100, "XL", 40, "L", 50, "X", 10, "IX", 9, "IV", 4, "V", 5, "I", 1
[DecimalData]
DATA "M", 1000, "CM", 900, "D", 500, "CD", 400, "C", 100, "XC", 90, "L", 50, "XL", 40, "X", 10, "IX", 9, "V", 5, "IV", 4, "I", 1
|
|
Logged
|
|
|
|
Richard Russell
Administrator
member is offline


Posts: 1348
|
 |
Re: Fun Code
« Reply #9 on: Apr 24th, 2016, 3:53pm » |
|
on Apr 24th, 2016, 1:42pm, Optimax wrote:| I wrote the same thing with some GUI, roman to decimal, and back |
|
I'm pleased to see that it accepts the alternative forms of 4 (IV or IIII) and 9 (IX or VIIII). You will commonly see IIII used on clock faces, although Big Ben is an exception.
Some of the less common variations (e.g. MDCDIII for 1903) are, unsurprisingly, not accepted. The Wikipedia article discusses these.
It would be an interesting challenge to write a Roman-to-Decimal converter which would accept all reasonable alternatives.
Richard.
|
|
Logged
|
|
|
|
Optimax
New Member
member is offline


Posts: 11
|
 |
Re: Fun Code
« Reply #10 on: Apr 25th, 2016, 06:37am » |
|
I've read that article with great interest, thanks for the reference.
Well, I could add in the DATA (at the right place), MDCD = 1900, and IIX = 8 ("the subtractive").
MDCCCC = 1900 (for editors), XXXXXX = 60, IIIII = 5, ..., are already accepted.
Works now also with MDCDIII =1903, or CXIIX =118, with the "subtractive" IIX = 8.
Code:INPUT "Roman number ? "; R$
R$ = UPPER$(R$)
PRINT R$
D = 0
FOR i = 1 TO 15
READ Rom$, Dec
DO
IF LEFT$(R$, LEN(Rom$)) = Rom$ THEN
D = D + Dec
R$ = MID$(R$, LEN(Rom$)+1)
ELSE
EXIT DO
END IF
LOOP
NEXT i
PRINT D
END
DATA "MDCD", 1900,"M", 1000, "CM", 900, "CD", 400, "D", 500, "XC", 90, "C", 100, "XL", 40, "L", 50, "X", 10, "IIX", 8, "IX", 9, "IV", 4, "V", 5, "I", 1
|
|
Logged
|
|
|
|
|