Converte um número decimal em um algarismo romano

 

Em uma nova aplicação, adicione duas caixas de texto e um botão. Copie a função abaixo, ela será responsável pela conversão dos números decimais em algarismos romanos.

 

function TForm1.DecToRoman(Decimal: Integer): string;

const

  Romans: array[1..13] of string = ( 'I', 'IV', 'V', 'IX', 'X', 'XL', 'L', 'XC', 'C', 'CD', 'D', 'CM', 'M' );

  Arabics: array[1..13] of Integer = ( 1, 4, 5, 9, 10, 40, 50, 90, 100, 400, 500, 900, 1000);

var

  i: Integer;

  scratch: string;

begin

  scratch := '';

  for i := 13 downto 1 do

    while ( Decimal >= Arabics[i] ) do

    begin

      Decimal := Decimal - Arabics[i];

      scratch := scratch + Romans[i];

    end;

  Result := scratch;

end;

 

Agora faça a chamada à funcão DecToRoman no evento OnClick do botão.

 

procedure TForm1.Button1Click(Sender: TObject);

begin

  edit2.Text := DecToRoman(strtoint(edit1.Text));

end;

 

Excute a aplicação.

 

por Erick Rhamnusia

uploader@clubedelphi.net