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

 
 
Herramientas Buscar en Tema Desplegado
  #7  
Antiguo 26-05-2017
Avatar de olbeup
olbeup olbeup is offline
Miembro
 
Registrado: jul 2005
Ubicación: Santiago de la Ribera (España)
Posts: 685
Poder: 19
olbeup Va camino a la fama
Hola Juampi,

Con esto puedes avanzar un poco.

Esto al principio
Código Delphi [-]
const
  WVK_BACKDELETE  = #8;
  WVK_SUM         = #43;
  WVK_REST        = #45;
  WVK_EQUAL       = #61;
  WVK_NULL        = #0;
  WVK_EMPTY       = '';

type
  TCalculatorReg = record
    NumberStr: String;
    KeyPred: Char;
    KeyNext: Char;
    SubTotal: Integer;
  end;
Y esto en los eventos correspondientes.
Código Delphi [-]
procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char);
begin
  if not (Key in ['0'..'9', WVK_SUM, WVK_REST, WVK_EQUAL, WVK_BACKDELETE]) then
    Key := WVK_NULL;
end;

procedure TForm1.Button1Click(Sender: TObject);

  function IsNumeric(S: Char): Boolean;
  begin
    Result := (S in ['0'..'9']);
  end;

  function IsSum(S: Char): Boolean;
  begin
    Result := (S = '+');
  end;

  function IsRest(S: Char): Boolean;
  begin
    Result := (S = '-');
  end;

var
  Calc: TCalculatorReg;
  nI: Integer;
  S: String;
  Key: Char;
begin
  FillChar(Calc, SizeOf(Calc), #0);
  Key := WVK_NULL;
  S := Edit1.Text;

  for nI := 1 to Length(S) do
  begin
    if IsNumeric(S[nI]) then
      Calc.NumberStr := Calc.NumberStr + S[nI]
    else
      if IsSum(S[nI]) then
        Key := WVK_SUM
      else
        if IsRest(S[nI]) then
          Key := WVK_REST
        else
          Key := WVK_EQUAL;

    if (Key <> WVK_EMPTY) and (Calc.KeyPred <> WVK_EMPTY) and (Calc.KeyNext = WVK_EMPTY) then
      Calc.KeyNext := Key;

    if (Key <> WVK_EMPTY) and (Calc.KeyPred = WVK_EMPTY) and (Calc.KeyNext = WVK_EMPTY) then
      Calc.KeyPred := Key;

    Key := WVK_NULL;

    if (Calc.KeyPred <> WVK_EMPTY) and (Calc.KeyNext = WVK_EMPTY) then
      if (Calc.SubTotal = 0) then
      begin
        Calc.SubTotal := StrToInt(Calc.NumberStr);
        Calc.NumberStr := WVK_EMPTY;
      end;

    if (Calc.KeyPred <> WVK_EMPTY) and (Calc.KeyNext <> WVK_EMPTY) then
    begin
      if (Calc.KeyPred = WVK_SUM) then
        Calc.SubTotal := Calc.SubTotal + StrToInt(Calc.NumberStr);
      if (Calc.KeyPred = WVK_REST) then
        Calc.SubTotal := Calc.SubTotal - StrToInt(Calc.NumberStr);
      Calc.NumberStr := WVK_EMPTY;
      Calc.KeyPred := Calc.KeyNext;
      Calc.KeyNext := WVK_NULL;
    end;
  end;

  Label2.Caption := IntToStr(Calc.SubTotal);
end;

Un Saludo.

p.d.: Sólo funciona con enteros y al final tienes que poner un = para que lo sume todo, ya lo modificas como quieras.
__________________
Al hacer una consulta SQL, haz que los demás te entiendan y disfruten de ella, será tú reflejo de tú saber.
Responder Con Cita
 



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

Temas Similares
Tema Autor Foro Respuestas Último mensaje
Opinión sobre la estabilidad actual de Lazarus/Free Pascal Toni Lazarus, FreePascal, Kylix, etc. 34 06-02-2016 01:04:45
Soy desarrollador de Delphi ¿Que necesito saber para usar Lazarus? Godzuki Linux 7 23-02-2012 23:54:34
Lazarus, Nemesis Pascal rmendoza83 Varios 1 08-01-2009 17:41:08
Indy soporta Free Pascal/Lazarus Delfino Noticias 6 15-01-2006 10:55:55
Free Pascal y Lazarus Magician^ Noticias 7 31-03-2004 19:12:04


La franja horaria es GMT +2. Ahora son las 17:54:20.


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