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 sistema de numeracion + delphi 7 (https://www.clubdelphi.com/foros/showthread.php?t=75667)

Nicolas_2011 11-09-2011 01:44:27

conversion de sistema de numeracion + delphi 7
 
gente necesito ayuda, tengo todo el codigo, pero no puedo lograr unirlo en un solo radiogroup aca le va el codigo. lo que esta en azul supuestamente esta bien pero lo de rojo es el problema que tengo..espero q entiendan mi pequeño inconveniente de no ser asi explico mejor, espero su ayuda!!!
Código Delphi [-]
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ExtCtrls,math;

type
  TForm1 = class(TForm)
    e: TEdit;
    rgbase: TRadioGroup;
    procedure eKeyPress(Sender: TObject; var Key: Char);
    procedure rgbaseClick(Sender: TObject);
  private
    Function dr (s:string; bl:integer):string; //base llegada
    Function sp (s:string ;bp:integer):string; // base partida
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation
 Function TForm1.sp (s:string ;bp:integer):string; // suma ponderada
 var
  i:integer;
  m:Extended;
  Begin
  m:=0;
    For i:=1 to Length(s) do
      Case s[i] Of
        'A' : m:= m + 10* Power(bp,Length(s) - i);
        'B' : m:= m + 11* Power(bp,Length(s) - i);
        'C' : m:= m + 12* Power(bp,Length(s) - i);
        'D' : m:= m + 13* Power(bp,Length(s) - i);
        'E' : m:= m + 14* Power(bp,Length(s) - i);
        'F' : m:= m + 15* Power(bp,Length(s) - i);
          Else
            m:= m + StrToInt(s[i])*Power(bp,Length(s)-i);
            end;
    result:=FloatToStr(m);
end;
 Function TForm1.dr (s:string; bl:integer):string; // division reiterada
  Var
  num: integer;
  d:integer;
  resultado:string;
        Begin
        num:=StrToInt(s);
        resultado:=  ' ';
        while (num > 0) do
          begin
            d:= num mod bl;
            num:= num div bl;
                     case d of
              10: resultado:= 'A' + resultado;
              11: resultado:= 'B' + resultado;
              12: resultado:= 'C' + resultado;
              13: resultado:= 'D' + resultado;
              14: resultado:= 'E' + resultado;
              15: resultado:= 'F' + resultado;
                else
                  resultado:= IntToStr(d) + resultado;
              end;
              end;
              Result:=resultado;
            end;
{$R *.dfm}

procedure TForm1.eKeyPress(Sender: TObject; var Key: Char);   // restricciones
begin
  Case RgBase.ItemIndex Of
    0:Begin
    If not (Key in ['1','0', #8]) then
      key:= #0
end;
    1:Begin
    If not (Key in ['0'..'7', #8]) then
      key:= #0
end;
    2:Begin
    If not (Key in ['0'..'9', #8]) then
      key:= #0
end;
    3:Begin
    If not (Key in ['A'..'F', '0'..'9', #8]) then
      key:= #0
end;
end;

end;

procedure TForm1.rgbaseClick(Sender: TObject);  // conversion
begin
e.Enabled := true;
e.SetFocus;
 { ¡¡ AQUI EL PROBLEMA !! }
 Begin
  If rgbase.ItemIndex <> 2 then
    case rgbase.ItemIndex of
       0: e.Text := dr(e.Text,2);
       1: e.Text := dr(e.Text,8);
       2: e.Text := dr(e.Text,10);
       3: e.Text := dr(e.Text,16)
       Else
begin
If rgbase.ItemIndex = 2 then
  case rgbase.ItemIndex of
  0 : e.Text := sp(e.Text,2);
  1 : e.Text:= sp(e.Text,8);
  2 : e.Text:= sp(e.Text,10);
  3 : e.text:= sp(e.Text,16);
end;

end;


end;
end;
end;
end.
la idea es que seleccione un item y recien se habilite el edit cuando ingresa un numero hace click en otro item y cambia automaticamente el valor y asi sucesivamente.

ecfisa 11-09-2011 01:52:49

Hola Nicolas_2011 y bienvenido a los foros de Club Delphi :)

Por favor, cuando pongas código Delphi, sólamente encerralo entre las etiquetas [delphi] [/delphi] , de ese modo es mucho más legible.

Si querés destacar una parte del código ( la que te dá problemas ), sólamente comentala como en el editor de Delphi { comentario }



Un saludo.

Nicolas_2011 11-09-2011 01:56:00

mil discuplas, no sabia

ecfisa 11-09-2011 02:22:01

Hola Nicolas.

No hay problema, sos nuevo y es lógico pero si nadie te lo dice, no vas a usar las etiquetas... :)

Estuve corrigiendo un poco tu código y así parece funcionar bién:
Código Delphi [-]
...
implementation

uses Math;

function TForm1.sp (s:string ;bp:integer):string; // suma ponderada
var
  i:integer;
  m:Extended;
begin
  m:=0;
  For i:=1 to Length(s) do
    case s[i] Of
      'A' : m:= m + 10* Power(bp,Length(s) - i);
      'B' : m:= m + 11* Power(bp,Length(s) - i);
      'C' : m:= m + 12* Power(bp,Length(s) - i);
      'D' : m:= m + 13* Power(bp,Length(s) - i);
      'E' : m:= m + 14* Power(bp,Length(s) - i);
      'F' : m:= m + 15* Power(bp,Length(s) - i);
      else
       m:= m + StrToInt(s[i])*Power(bp,Length(s)-i);
    end;
  result:=FloatToStr(m);
end;

function TForm1.dr (s:string; bl:integer):string; // division reiterada
var
  num: integer;
  d:integer;
  resultado:string;
begin
  num:= StrToInt(s);
  resultado:=  '';
  while (num > 0) do
  begin
    d:= num mod bl;
    num:= num div bl;
    case d of
      10: resultado:= 'A' + resultado;
      11: resultado:= 'B' + resultado;
      12: resultado:= 'C' + resultado;
      13: resultado:= 'D' + resultado;
      14: resultado:= 'E' + resultado;
      15: resultado:= 'F' + resultado;
      else
        resultado:= IntToStr(d) + resultado;
    end;
  end;
  Result:= resultado;
end;

procedure TForm1.eKeyPress(Sender: TObject; var Key: Char);
begin
  case RgBase.ItemIndex Of
    0: If not (Key in ['1','0', #8]) then key:= #0;
    1: If not (Key in ['0'..'7', #8]) then   key:= #0;
    2: If not (Key in ['0'..'9', #8]) then   key:= #0;
    3: If not (Key in ['A'..'F', '0'..'9', #8]) then key:= #0;
  end;
end;

procedure TForm1.rgBaseClickClick(Sender: TObject);
begin
  e.Enabled := true;
  e.SetFocus;
  if rgbase.ItemIndex <> 2 then   // Si es cualquier número excepto el 2
  begin
    case rgbase.ItemIndex of
       0: e.Text := dr(e.Text,2);
       1: e.Text := dr(e.Text,8);
       2: e.Text := dr(e.Text,10);
       3: e.Text := dr(e.Text,16)
    end;
  end
  else                             // Si no, es el 2
  begin
    case rgbase.ItemIndex of
      0 : e.Text := sp(e.Text,2);
      1 : e.Text:= sp(e.Text,8);
      2 : e.Text:= sp(e.Text,10);
      3 : e.text:= sp(e.Text,16);
    end;
  end;
end;
end.
Cualquier duda o problema avisame y te adjunto los fuentes de mi prueba.

Un saludo.

Nicolas_2011 11-09-2011 02:46:03

Amigo estoy tiendiendo 2 problemas.
problema 1. Solo puedo seleccionar x primera vez el item 2 osea base 10. si selecciono por primera vez cualquier otro tira error.
problema 2. al seleccionar base 10(obligadamente) ingrso el numero, selecciono otra base (cualquiera) y convierte bien, pero luego no puedo seleccionar otra base para que convierta nuevamente da error.

ecfisa 11-09-2011 05:55:19

Hola Nicolas.

Hoy andaba con nostalgia de épocas de estudiante... :)

Código Delphi [-]
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ExtCtrls;

type
  TForm1 = class(TForm)
    Edit: TEdit;
    RGroupBases: TRadioGroup;
    procedure RGroupBasesClick(Sender: TObject);
    procedure EditKeyPress(Sender: TObject; var Key: Char);
    procedure FormShow(Sender: TObject);
  private
    FBasePrevia: Integer;
    procedure DivisionReiterada(var Numero:string; Base:integer);
    procedure SumaPonderada(var Numero:string; Base:integer);
  public
  end;

var
  Form1: TForm1;

implementation {$R *.dfm}

uses Math;

procedure TForm1.FormShow(Sender: TObject);
begin
  // Cargar Items + Valor de las bases
  with RGroupBases do
  begin
    Items.AddObject('Binario', TObject(2));
    Items.AddObject('Octal', TObject(8));
    Items.AddObject('Decimal', TObject(10));
    Items.AddObject('Hexadecimal', TObject(16));
  end;
  FBasePrevia:= 2;           // Binario
  RGroupBases.ItemIndex:= 0; // Primer Item
  // Previamente quitar evento RadioGroup.OnClick del Object Inspector
  RGroupBases.OnClick:= RGroupBasesClick;
end;

{ Funcion que pasa de base N a decimal }
function BaseNToDec(Numero : string; Base : byte): Integer;
const
  BHex = '0123456789ABCDEF';
var
  i : Byte;
begin
  Result:=0;
  for i:=1 to length(Numero) do
    Result:= Result * Base + Pos(Upcase(Numero[i]), BHex) - 1;
end;

{ Suma ponderada }
procedure TForm1.SumaPonderada (var Numero:string; Base:integer);
var
  i:integer;
  m:Extended;
begin
  m:=0;
  for i:= 1 to Length(Numero) do
  begin
    case Numero[i] Of
      'A': m:= m + 10 * Power(FBasePrevia, Length(Numero) - i);
      'B': m:= m + 11 * Power(FBasePrevia, Length(Numero) - i);
      'C': m:= m + 12 * Power(FBasePrevia, Length(Numero) - i);
      'D': m:= m + 13 * Power(FBasePrevia, Length(Numero) - i);
      'E': m:= m + 14 * Power(FBasePrevia, Length(Numero) - i);
      'F': m:= m + 15 * Power(FBasePrevia, Length(Numero) - i);
      else
       m:= m + StrToInt(Numero[i]) * Power(FBasePrevia, Length(Numero) - i);
    end;
  end;
  Numero:= FloatToStr(m);
end;

{ División reiterada }
procedure TForm1.DivisionReiterada(var Numero:string; Base:integer);
var
  num: integer;
  d:integer;
begin
  // Calcular en base a la base previa al click
  num:= BaseNToDec(Numero, FBasePrevia);  // Pasar a base 10
  Numero:=  '';
  while (num > 0) do
  begin
    d:= num mod Base;
    num:= num div Base;
    case d of
      10: Numero:= 'A' + Numero;
      11: Numero:= 'B' + Numero;
      12: Numero:= 'C' + Numero;
      13: Numero:= 'D' + Numero;
      14: Numero:= 'E' + Numero;
      15: Numero:= Numero + 'F';
      else
        Numero:= IntToStr(d) + Numero;
    end;
  end;
  Numero:= Numero;
end;

procedure TForm1.EditKeyPress(Sender: TObject; var Key: Char);
begin
  Key:= UpCase(Key);         // A mayúsculas
  case RGroupBases.ItemIndex Of
    0: If not (Key in ['1','0', #8]) then key:= #0;
    1: If not (Key in ['0'..'7', #8]) then key:= #0;
    2: If not (Key in ['0'..'9', #8]) then key:= #0;
    3: If not (Key in ['0'..'9', 'A'..'F', #8]) then key:= #0;
  end;
end;

{ RadioGroup OnClick }
procedure TForm1.RGroupBasesClick(Sender: TObject);
var
  Valor: string;
begin
  Valor:= Edit.Text;
  Edit.SetFocus;
  if RGroupBases.ItemIndex <> 2 then   // Binario, Octal, Hexadecimal
  begin
    case RGroupBases.ItemIndex of
       0: DivisionReiterada(Valor,2);
       1: DivisionReiterada(Valor,8);
       2: DivisionReiterada(Valor,10);
       3: DivisionReiterada(Valor,16)
    end;
  end
  else                                 // Decimal
  begin
    case RGroupBases.ItemIndex of
      0: SumaPonderada(Valor,2);
      1: SumaPonderada(Valor,8);
      2: SumaPonderada(Valor,10);
      3: SumaPonderada(Valor,16);
    end;
  end;
  // Actualizar valor de la base
  FBasePrevia:= Integer(RGroupBases.Items.Objects[RGroupBases.ItemIndex]);
  Edit.Text:= Valor;
end;

end.

Saludos.

Nicolas_2011 11-09-2011 18:53:16

muchas gracias amigo ahora me pongo a interpretarlo, seguramente me sera de gran ayuda :D

ecfisa 11-09-2011 19:33:54

Hola Nicolas.

Echando una segunda mirada al código que te puse, veo que se puede simplificar aún más... RGroupBasesClick puede quedar así:
Código Delphi [-]
{ RadioGroup OnClick }
procedure TForm1.RGroupBasesClick(Sender: TObject);
var
  Valor: string;
begin
  Valor:= Edit.Text;
  Edit.SetFocus;
  if RGroupBases.ItemIndex <> 2 then   // Binario, Octal, Hexadecimal
    DivisionReiterada(Valor, Integer(RGroupBases.Items.Objects[RGroupBases.ItemIndex]))
  else                                // Decimal
    SumaPonderada(Valor, Integer(RGroupBases.Items.Objects[RGroupBases.ItemIndex])); 
  FBasePrevia:= Integer(RGroupBases.Items.Objects[RGroupBases.ItemIndex]);  // Actualizar valor de la base
  Edit.Text:= Valor;
end;
Seguramente encuentres algunas partes más para optimizar.

Saludos. :)


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

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