Foros Club Delphi

Foros Club Delphi (https://www.clubdelphi.com/foros/index.php)
-   Varios (https://www.clubdelphi.com/foros/forumdisplay.php?f=11)
-   -   conversion de numeros romanos a letras (https://www.clubdelphi.com/foros/showthread.php?t=28935)

shishio_al 07-01-2006 03:51:35

conversion de numeros romanos a letras
 
Holas, q tal, soy nuevo y no manejo muy bien el delphi, lo veo interesante pero un poco mas complejo q otros programas

kisiera q me ayuden con el codigo de esta conversion, de romanos a letras (ejemplo, VI = seis, XXII = veintidos)

por favor, es muy importante este trabajo, x eso llegue hasta aka buskando ayuda :)

les agradezco de antemano, y en un futuro vere en q puedo ayudar a esta comunidad...

p.d.: por favor! :'(

mi msn es: rolovd@hotmail.com

dec 07-01-2006 05:30:26

Hola,

Aprovechando el estupendo componente ATexto, escrito por Antoni Aloy, solamente nos quedaría convertir un número romano al correspondiente entero. He buscado en Koders.com y he encontrado una clase escrita en Java que trata sobre números romanos.

En dicha clase hay un método, entre otros, cuyo nombre lo dice todo: "RomanToInt". He traducido dicho método a Delphi como una función y parece ir bien. La referida clase es muy curiosa y no estaría mal traducirla entera, pero, en todo caso, cierto método, además del mencionado, merecería atención: estoy hablando del método "validate".

El caso es que gracias a esas dos piezas: el componente ATexto y la siguiente función, por lo menos, podrías conseguir lo que te propones, me parece a mí, vamos. ;)

Código Delphi [-]
function RomanToInt(const roman: string) : integer;
var
  i: integer;
  lastChar: Char;
begin
  result := 0;
  lastChar := ' ';
  for i := Length(roman) downto 1 do
  begin
    case roman[i] of
      'I': if (lastChar = 'X') or (lastChar = 'V') then
             Dec(result) else Inc(result);
      'V': Inc(result, 5);
      'X': if (lastChar = 'C') or (lastChar = 'L') then
             Dec(result, 10) else Inc(result, 10);
      'L': Inc(result, 50);
      'C': if (lastChar = 'M') or (lastChar = 'D') then
             Dec(result, 100) else Inc(result, 100);
      'D': Inc(result, 500);
      'M': Inc(result, 1000);
    end;
    lastChar := roman[i];
  end;
end;

dec 07-01-2006 06:42:31

1 Archivos Adjunto(s)
Hola,

Pues nada, que no he podido evitarlo:

Código Delphi [-]
{******************************************************************************}
{                                                                              }
{   Clase estática TRomanNumbers                                               }
{                                                                              }
{   Traducida por David Esperalta (davidesperalta@gmail.com)                   }
{   de una clase escrita  en Java por  Marvin Herman Froeder                   }
{                                                                              }
{   7 de enero de 2006                                                         }
{                                                                              }
{******************************************************************************}

unit URomanNumbers;

interface

uses
  SysUtils;

type
  TRomanNumbers = class(TObject)
  public
    class function Validate(roman: string) : boolean;
    class function IntToRoman(value: integer) : string;
    class function RomanToInt(const roman: string) : integer;
  end;

implementation

resourcestring
  rsErrorEntradaInvalida1 = 'No es posible convertir un número romano menor que cero.';
  rsErrorEntradaInvalida2 = 'No es posible convertir un número romano mayor que 3999.';
  rsErrorEntradaInvalida3 = 'El número romano entregado parece no ser del todo bueno.';

{ TRomanNumbers }

{ Devuelve un número romano a partir de un número entero.
}
class function TRomanNumbers.IntToRoman(value: integer): string;
begin
  result := '';
  if(value = 0) then Exit;
  if(value < 0) then raise
    Exception.Create(rsErrorEntradaInvalida1);
  if (value <= 3999) then
  begin
    while(value / 1000 >= 1) do
    begin
      result := result + 'M';
      value := value - 1000;
    end;
    if (value / 900 >= 1) then
    begin
      result := result + 'CM';
      value := value - 900;
    end;
    if (value / 500 >= 1) then
    begin
      result := result + 'D';
      value := value - 500;
    end;
    if (value / 400 >= 1) then
    begin
      result := result + 'CD';
      value := value - 400;
    end;
    while(value / 100 >= 1) do
    begin
      result := result + 'C';
      value := value - 100;
    end;
    if (value / 90 >= 1) then
    begin
      result := result + 'XC';
      value := value - 90;
    end;
    if (value / 50 >= 1) then
    begin
      result := result + 'L';
      value := value - 50;
    end;
    if (value / 40 >= 1) then
    begin
      result := result + 'XL';
      value := value - 40;
    end;
    while(value / 10 >= 1) do
    begin
      result := result + 'X';
      value := value - 10;
    end;
    if (value / 9 >= 1) then
    begin
      result := result + 'IX';
      value := value - 9;
    end;
    if (value / 5 >= 1) then
    begin
      result := result + 'V';
      value := value - 5;
    end;
    if (value / 4 >= 1) then
    begin
      result := result + 'IV';
      value := value - 4;
    end;
    while(value >= 1 ) do
    begin
      result := result + 'I';
      value := value - 1;
    end;
  end
  else
    raise Exception.Create(rsErrorEntradaInvalida2);
end;

{ Devuelve un número entero a partir de un número romano.
}
class function TRomanNumbers.RomanToInt(const roman: string) : integer;
var
  i: integer;
  lastChar: Char;
begin
  result := 0;
  if Validate(roman) then
  begin
    lastChar := ' ';
    for i := Length(roman) downto 1 do
    begin
      case roman[i] of
        'I': if (lastChar = 'X') or (lastChar = 'V') then
               Dec(result) else Inc(result);
        'V': Inc(result, 5);
        'X': if (lastChar = 'C') or (lastChar = 'L') then
               Dec(result, 10) else Inc(result, 10);
        'L': Inc(result, 50);
        'C': if (lastChar = 'M') or (lastChar = 'D') then
               Dec(result, 100) else Inc(result, 100);
        'D': Inc(result, 500);
        'M': Inc(result, 1000);
      end;
      lastChar := roman[i];
    end;
  end
  else
    raise Exception.Create(rsErrorEntradaInvalida3);
end;

{ Valida (hasta cierto punto) que un número romano sea válido.
}
class function TRomanNumbers.Validate(roman: string) : boolean;
var
  i: integer;
begin
  result := false;
  for i := 1 to Length(roman) do
    result := (roman[i]    = 'I')
              or (roman[i] = 'V')
              or (roman[i] = 'X')
              or (roman[i] = 'L')
              or (roman[i] = 'C')
              or (roman[i] = 'D')
              or (roman[i] = 'M');
end;

end.

ContraVeneno 09-01-2006 18:26:13

Que bárbaro Maese Dec, ojala y yo te hubiera conocido en mis tiempos de estudiante, jeje :D

shishio_al 10-01-2006 05:07:00

Cita:

Empezado por dec
Hola,

Pues nada, que no he podido evitarlo:

Código Delphi [-]
{******************************************************************************}
{                                                                              }
{   Clase estática TRomanNumbers                                               }
{                                                                              }
{   Traducida por David Esperalta (davidesperalta@gmail.com)                   }
{   de una clase escrita  en Java por  Marvin Herman Froeder                   }
{                                                                              }
{   7 de enero de 2006                                                         }
{                                                                              }
{******************************************************************************}

unit URomanNumbers;

interface

uses
  SysUtils;

type
  TRomanNumbers = class(TObject)
  public
    class function Validate(roman: string) : boolean;
    class function IntToRoman(value: integer) : string;
    class function RomanToInt(const roman: string) : integer;
  end;

implementation

resourcestring
  rsErrorEntradaInvalida1 = 'No es posible convertir un número romano menor que cero.';
  rsErrorEntradaInvalida2 = 'No es posible convertir un número romano mayor que 3999.';
  rsErrorEntradaInvalida3 = 'El número romano entregado parece no ser del todo bueno.';

{ TRomanNumbers }

{ Devuelve un número romano a partir de un número entero.
}
class function TRomanNumbers.IntToRoman(value: integer): string;
begin
  result := '';
  if(value = 0) then Exit;
  if(value < 0) then raise
    Exception.Create(rsErrorEntradaInvalida1);
  if (value <= 3999) then
  begin
    while(value / 1000 >= 1) do
    begin
      result := result + 'M';
      value := value - 1000;
    end;
    if (value / 900 >= 1) then
    begin
      result := result + 'CM';
      value := value - 900;
    end;
    if (value / 500 >= 1) then
    begin
      result := result + 'D';
      value := value - 500;
    end;
    if (value / 400 >= 1) then
    begin
      result := result + 'CD';
      value := value - 400;
    end;
    while(value / 100 >= 1) do
    begin
      result := result + 'C';
      value := value - 100;
    end;
    if (value / 90 >= 1) then
    begin
      result := result + 'XC';
      value := value - 90;
    end;
    if (value / 50 >= 1) then
    begin
      result := result + 'L';
      value := value - 50;
    end;
    if (value / 40 >= 1) then
    begin
      result := result + 'XL';
      value := value - 40;
    end;
    while(value / 10 >= 1) do
    begin
      result := result + 'X';
      value := value - 10;
    end;
    if (value / 9 >= 1) then
    begin
      result := result + 'IX';
      value := value - 9;
    end;
    if (value / 5 >= 1) then
    begin
      result := result + 'V';
      value := value - 5;
    end;
    if (value / 4 >= 1) then
    begin
      result := result + 'IV';
      value := value - 4;
    end;
    while(value >= 1 ) do
    begin
      result := result + 'I';
      value := value - 1;
    end;
  end
  else
    raise Exception.Create(rsErrorEntradaInvalida2);
end;

{ Devuelve un número entero a partir de un número romano.
}
class function TRomanNumbers.RomanToInt(const roman: string) : integer;
var
  i: integer;
  lastChar: Char;
begin
  result := 0;
  if Validate(roman) then
  begin
    lastChar := ' ';
    for i := Length(roman) downto 1 do
    begin
      case roman[i] of
        'I': if (lastChar = 'X') or (lastChar = 'V') then
               Dec(result) else Inc(result);
        'V': Inc(result, 5);
        'X': if (lastChar = 'C') or (lastChar = 'L') then
               Dec(result, 10) else Inc(result, 10);
        'L': Inc(result, 50);
        'C': if (lastChar = 'M') or (lastChar = 'D') then
               Dec(result, 100) else Inc(result, 100);
        'D': Inc(result, 500);
        'M': Inc(result, 1000);
      end;
      lastChar := roman[i];
    end;
  end
  else
    raise Exception.Create(rsErrorEntradaInvalida3);
end;

{ Valida (hasta cierto punto) que un número romano sea válido.
}
class function TRomanNumbers.Validate(roman: string) : boolean;
var
  i: integer;
begin
  result := false;
  for i := 1 to Length(roman) do
    result := (roman[i]    = 'I')
              or (roman[i] = 'V')
              or (roman[i] = 'X')
              or (roman[i] = 'L')
              or (roman[i] = 'C')
              or (roman[i] = 'D')
              or (roman[i] = 'M');
end;

end.

pues en realidad es sorprendente tu trabajo!! muxas gracias, vere como le hago correr :)

salu2

Fernando 24-01-2006 06:50:31

Hola Foronautas:

En www.q3.nu existen un truco para la conversión.

Saludos.

isarmiento 28-09-2012 13:51:57

duda
 
Cita:

Empezado por dec (Mensaje 124846)
Hola,

Pues nada, que no he podido evitarlo:

Código Delphi [-]
{******************************************************************************}
{                                                                              }
{   Clase estática TRomanNumbers                                               }
{                                                                              }
{   Traducida por David Esperalta (davidesperalta@gmail.com)                   }
{   de una clase escrita  en Java por  Marvin Herman Froeder                   }
{                                                                              }
{   7 de enero de 2006                                                         }
{                                                                              }
{******************************************************************************}

unit URomanNumbers;

interface

uses
  SysUtils;

type
  TRomanNumbers = class(TObject)
  public
    class function Validate(roman: string) : boolean;
    class function IntToRoman(value: integer) : string;
    class function RomanToInt(const roman: string) : integer;
  end;

implementation

resourcestring
  rsErrorEntradaInvalida1 = 'No es posible convertir un número romano menor que cero.';
  rsErrorEntradaInvalida2 = 'No es posible convertir un número romano mayor que 3999.';
  rsErrorEntradaInvalida3 = 'El número romano entregado parece no ser del todo bueno.';

{ TRomanNumbers }

{ Devuelve un número romano a partir de un número entero.
}
class function TRomanNumbers.IntToRoman(value: integer): string;
begin
  result := '';
  if(value = 0) then Exit;
  if(value < 0) then raise
    Exception.Create(rsErrorEntradaInvalida1);
  if (value <= 3999) then
  begin
    while(value / 1000 >= 1) do
    begin
      result := result + 'M';
      value := value - 1000;
    end;
    if (value / 900 >= 1) then
    begin
      result := result + 'CM';
      value := value - 900;
    end;
    if (value / 500 >= 1) then
    begin
      result := result + 'D';
      value := value - 500;
    end;
    if (value / 400 >= 1) then
    begin
      result := result + 'CD';
      value := value - 400;
    end;
    while(value / 100 >= 1) do
    begin
      result := result + 'C';
      value := value - 100;
    end;
    if (value / 90 >= 1) then
    begin
      result := result + 'XC';
      value := value - 90;
    end;
    if (value / 50 >= 1) then
    begin
      result := result + 'L';
      value := value - 50;
    end;
    if (value / 40 >= 1) then
    begin
      result := result + 'XL';
      value := value - 40;
    end;
    while(value / 10 >= 1) do
    begin
      result := result + 'X';
      value := value - 10;
    end;
    if (value / 9 >= 1) then
    begin
      result := result + 'IX';
      value := value - 9;
    end;
    if (value / 5 >= 1) then
    begin
      result := result + 'V';
      value := value - 5;
    end;
    if (value / 4 >= 1) then
    begin
      result := result + 'IV';
      value := value - 4;
    end;
    while(value >= 1 ) do
    begin
      result := result + 'I';
      value := value - 1;
    end;
  end
  else
    raise Exception.Create(rsErrorEntradaInvalida2);
end;

{ Devuelve un número entero a partir de un número romano.
}
class function TRomanNumbers.RomanToInt(const roman: string) : integer;
var
  i: integer;
  lastChar: Char;
begin
  result := 0;
  if Validate(roman) then
  begin
    lastChar := ' ';
    for i := Length(roman) downto 1 do
    begin
      case roman[i] of
        'I': if (lastChar = 'X') or (lastChar = 'V') then
               Dec(result) else Inc(result);
        'V': Inc(result, 5);
        'X': if (lastChar = 'C') or (lastChar = 'L') then
               Dec(result, 10) else Inc(result, 10);
        'L': Inc(result, 50);
        'C': if (lastChar = 'M') or (lastChar = 'D') then
               Dec(result, 100) else Inc(result, 100);
        'D': Inc(result, 500);
        'M': Inc(result, 1000);
      end;
      lastChar := roman[i];
    end;
  end
  else
    raise Exception.Create(rsErrorEntradaInvalida3);
end;

{ Valida (hasta cierto punto) que un número romano sea válido.
}
class function TRomanNumbers.Validate(roman: string) : boolean;
var
  i: integer;
begin
  result := false;
  for i := 1 to Length(roman) do
    result := (roman[i]    = 'I')
              or (roman[i] = 'V')
              or (roman[i] = 'X')
              or (roman[i] = 'L')
              or (roman[i] = 'C')
              or (roman[i] = 'D')
              or (roman[i] = 'M');
end;

end.






Que maravilla :) quisiera saber como implementar esta clase a un proyecto de consola... para hacerlo correr tengo que llamar a la clase o como? te agradezco la ayuda, soy nueva programando y ando un poco perdida :)

escafandra 28-09-2012 18:07:45

Ejemplo sencillo:

Código Delphi [-]
program Project1;

{$APPTYPE CONSOLE}

uses
  SysUtils, URomanNumbers;

var
   S : string;
begin
  Writeln('Introduzca un número romano:');
  Readln(S);
  with TRomanNumbers.Create do
  begin
    Writeln(RomanToInt(UpperCase(S)));
    Free;
  end;
  Readln(S);
end.


Saludos.


La franja horaria es GMT +2. Ahora son las 21:34:18.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Traducción al castellano por el equipo de moderadores del Club Delphi