.
.
.{Arabic and Roman are TEdit components (objects),
Arabic.text and Roman.text are the actual text values }
const
romans:array[1..7] of char =('I','V','X','L','C','D','M'); {All the characters we'll use}
procedure Tform1.ShowRoman;
{Routine to convert and display a number}
var
s,s2:string;
i, baseindex:integer;
n1,n2,n3:char;
begin
s2:='';
{make sure number is OK}
if (length(Arabic.text)=0)
or (strtoint(Arabic.text)<=0)
or (strtoint(Arabic.text)>3999)
then begin beep; showmessage('Numbers must be between 1 and 3999'); end
else
begin
s:=Arabic.text;
for i:=1 to length(s) do
begin
{Baseindex picks the right starting spot in the base index array}
{expression was found by trial and error after making a table like this
Length of # Position in # Index value wanted
------------ -------------- -----------------
length(s) i BaseIndex
------------ --------------- ------------------
1 1 1
2 2 1
2 1 3
3 3 1
3 2 3
3 1 5
4 4 1
4 3 3
4 2 5
4 1 7
}
Baseindex:=2*(length(s)-i)+1;
{n1, n2, and n3 are the 3 characters we might need}
n1:=romans[baseindex];
n2:=romans[baseindex+1];
n3:=romans[baseindex+2];
case s[i] of
'1': s2:=s2+n1; {I,X,C,M}
'2': s2:=s2+n1+n1; {II,XX,CC,MM}
'3': s2:=s2+n1+n1+n1; {III,XXX,CCC,MMM}
'4': s2:=s2+n1+n2; {IV,XL,CD}
'5': s2:=s2+n2; {V,L,D}
'6': s2:=s2+n2+n1; {VI,LX,DC}
'7': s2:=s2+n2+n1+n1; {VII,LXX,DCC}
'8': s2:=s2+n2+n1+n1+n1; {VIII,LXXX,DCCC}
'9': s2:=s2+n1+n3; {IX,XC,CM}
end; {case}
{Note - no output for 0}
end;
end;
Roman.text:=s2; {Put the string back into display field}
end;
procedure TForm1.ArabicKeyPress(Sender: TObject; var Key: Char);
{We come here everytime the user presses a character key}
begin {keep out invalid data}
If not (key in ['0'..'9']) then begin key:=#00; beep; end;
end;
procedure TForm1.ArabicChange(Sender: TObject);
{We come here everytime the Arabic text box changes}
begin
showroman; {display new roman # for every change}
end;
end.