Club Delphi  
    FTP   CCD     Buscar   Trucos   Trabajo   Foros

Retroceder   Foros Club Delphi > Principal > Varios
Registrarse FAQ Miembros Calendario Guía de estilo Temas de Hoy

Grupo de Teaming del ClubDelphi

Respuesta
 
Herramientas Buscar en Tema Desplegado
  #1  
Antiguo 07-01-2006
shishio_al shishio_al is offline
Registrado
 
Registrado: ene 2006
Posts: 2
Poder: 0
shishio_al Va por buen camino
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
Responder Con Cita
  #2  
Antiguo 07-01-2006
Avatar de dec
dec dec is offline
Moderador
 
Registrado: dic 2004
Ubicación: Alcobendas, Madrid, España
Posts: 13.107
Poder: 34
dec Tiene un aura espectaculardec Tiene un aura espectacular
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;
__________________
David Esperalta
www.decsoftutils.com
Responder Con Cita
  #3  
Antiguo 07-01-2006
Avatar de dec
dec dec is offline
Moderador
 
Registrado: dic 2004
Ubicación: Alcobendas, Madrid, España
Posts: 13.107
Poder: 34
dec Tiene un aura espectaculardec Tiene un aura espectacular
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.
Archivos Adjuntos
Tipo de Archivo: zip URomanNumbers.zip (1,1 KB, 43 visitas)
__________________
David Esperalta
www.decsoftutils.com
Responder Con Cita
  #4  
Antiguo 09-01-2006
Avatar de ContraVeneno
ContraVeneno ContraVeneno is offline
Miembro
 
Registrado: may 2005
Ubicación: Torreón, México
Posts: 4.738
Poder: 23
ContraVeneno Va por buen camino
Que bárbaro Maese Dec, ojala y yo te hubiera conocido en mis tiempos de estudiante, jeje
__________________

Responder Con Cita
  #5  
Antiguo 10-01-2006
shishio_al shishio_al is offline
Registrado
 
Registrado: ene 2006
Posts: 2
Poder: 0
shishio_al Va por buen camino
Smile

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
Responder Con Cita
  #6  
Antiguo 24-01-2006
Fernando Fernando is offline
Miembro
 
Registrado: ago 2003
Posts: 77
Poder: 21
Fernando Va por buen camino
Hola Foronautas:

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

Saludos.
Responder Con Cita
  #7  
Antiguo 28-09-2012
Avatar de isarmiento
isarmiento isarmiento is offline
Registrado
NULL
 
Registrado: sep 2012
Posts: 5
Poder: 0
isarmiento Va por buen camino
duda

Cita:
Empezado por dec Ver Mensaje
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
Responder Con Cita
  #8  
Antiguo 28-09-2012
Avatar de escafandra
[escafandra] escafandra is offline
Miembro Premium
 
Registrado: nov 2007
Posts: 2.197
Poder: 20
escafandra Tiene un aura espectacularescafandra Tiene un aura espectacular
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.
Responder Con Cita
Respuesta



Normas de Publicación
no Puedes crear nuevos temas
no Puedes responder a temas
no Puedes adjuntar archivos
no Puedes editar tus mensajes

El código vB está habilitado
Las caritas están habilitado
Código [IMG] está habilitado
Código HTML está deshabilitado
Saltar a Foro


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


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
Copyright 1996-2007 Club Delphi