' Numerus3.ibas

BEGIN
   IF E=21 GOSUB _MClear
   IF E=22 GOSUB _MRelease
   IF E=23 GOSUB _MAdd
   IF E=24 GOSUB _CEReset
   IF E=25 GOSUB _CReset
   IF E=34 THEN
      E=NOTICEBOX(7)
      IF E=1 CLIPBOARDPUT N$
      IF E=2 GOSUB _MClear : GOSUB _CReset
      IF E=3 LET F=10+F : F=NOTICEBOX(F)
      IF E=4 LET H=20+H : H=NOTICEBOX(H)
      IF E=5 LET E=34 : CHAIN "Numerus.ibas"
   ENDIF
   GOSUB _DrawNum
   CHAIN "Numerus2.ibas"
END

_Convert1Num:
   A=P MOD 10
   IF A=1 LET N$="I"+N$
   IF A=2 LET N$="II"+N$
   IF A=3 LET N$="III"+N$
   IF A=4 LET N$="IV"+N$
   IF A=5 LET N$="V"+N$
   IF A=6 LET N$="VI"+N$
   IF A=7 LET N$="VII"+N$
   IF A=8 LET N$="VIII"+N$
   IF A=9 LET N$="IX"+N$
   B=P MOD 100 -A
   IF B=10 LET N$="X"+N$
   IF B=20 LET N$="XX"+N$
   IF B=30 LET N$="XXX"+N$
   IF B=40 LET N$="XL"+N$
   IF B=50 LET N$="L"+N$
   IF B=60 LET N$="LX"+N$
   IF B=70 LET N$="LXX"+N$
   IF B=80 LET N$="LXXX"+N$
   IF B=90 LET N$="XC"+N$
   C=P MOD 1000 -B -A
   IF C=100 LET N$="C"+N$
   IF C=200 LET N$="CC"+N$
   IF C=300 LET N$="CCC"+N$
   IF C=400 LET N$="CD"+N$
   IF C=500 LET N$="D"+N$
   IF C=600 LET N$="DC"+N$
   IF C=700 LET N$="DCC"+N$
   IF C=800 LET N$="DCCC"+N$
   IF C=900 LET N$="CM"+N$
   D=P MOD 10000 -C -B -A
   IF D=1000 LET N$="M"+N$
   IF D=2000 LET N$="MM"+N$
   IF D=3000 LET N$="MMM"+N$
   IF D=4000 LET N$="MMMM"+N$
RETURN

_Convert2Nums:
   P=N MOD 1000
   GOSUB _Convert1Num
   N$=".M "+N$
   P=N\1000
   GOSUB _Convert1Num
RETURN

_ConvertToRoman:
   IF N<=4999 THEN
      P=N
      GOSUB _Convert1Num
   ELSE GOSUB _Convert2Nums
   ENDIF
RETURN

_DrawNum:
   IF K=1 LET N=L
   IF K=2 LET N=M
   IF K=3 LET N=M : M=-1
   N$=""
   IF N>0 GOSUB _ConvertToRoman
   O$="" : M$=STR$(N,0)
   IF Q>0 LET O$="m"
   IF N<0 LET M$="error"
   COLOR Z
   BOXFILLED 3,19 TO 157,31
   BOXFILLED 3,34 TO 157,49
   UPDATELABEL #41,O$
   UPDATELABEL #42,M$
   UPDATELABEL #43,N$
RETURN

_CEReset:
   M=0
   K=3
RETURN

_CReset:
   L=0 : M=-1 : O=15
   K=1
RETURN

_MAdd:
   IF M<=0 THEN Q=Q+L
   ELSE Q=Q+M : L=M : M=-1
   ENDIF
   Q=MIN(Q,99999)
   K=1
RETURN

_MClear:
   Q=0
   IF M<=0 THEN K=1
   ELSE K=2
   ENDIF
RETURN

_MRelease:
   IF O=15 THEN L=Q : M=-1 : K=1
   ELSE M=Q : K=2
   ENDIF
RETURN