Vamos iniciar incluindo em nossa classe do formulário uma function chamada ValorParaRomano.
Veja sua declaração como deveria ficar.
Listagem 1: Declaração da function ValorParaRomano
type
TForm1 = class(TForm)
private
{ Private declarations }
function ValorParaRomano(pValor: Integer): string;
public
{ Public declarations }
end;
Objetivo: Implementar a function que irá fazer a conversão do formato de número para Algarismo Romano.
Listagem 2: Implementação da function ValorParaRomano
function TForm1.ValorParaRomano(pValor: Integer): string;
const
Numeros: array[1..13] of Integer =
(1, 4, 5, 9, 10, 40, 50, 90, 100,
400, 500, 900, 1000);
Romanos: array[1..13] of string =
('I', 'IV', 'V', 'IX', 'X', 'XL',
'L', 'XC', 'C', 'CD', 'D', 'CM', 'M');
var
i: Integer;
begin
Result := '';
for i := 13 downto 1 do
while (pValor >= Numeros[i]) do
begin
pValor := pValor - Numeros[i];
Result := Result + Romanos[i];
end;
end;
Veja como a classe agora esta ficando.
Listagem 3: Inclusão do TMemo e do TButton
type
TForm1 = class(TForm)
Button1: TButton;
Memo1: TMemo;
procedure Button1Click(Sender: TObject);
private
function ValorParaRomano(pValor: Integer): string;
{ Private declarations }
public
{ Public declarations }
end;
Listagem 4: Implementação do evento onClick do TButton
procedure TForm1.Button1Click(Sender: TObject);
begin
with Memo1.Lines do
begin
Clear;
Add('15 = ' + ValorParaRomano(15));
Add('986 = ' + ValorParaRomano(986));
Add('1000 = ' + ValorParaRomano(1000));
end;
end;
Um abraço
Wesley Y
www.lithic.com.br