Foros Club Delphi

Foros Club Delphi (https://www.clubdelphi.com/foros/index.php)
-   Varios (https://www.clubdelphi.com/foros/forumdisplay.php?f=11)
-   -   Carculadora ayuda (https://www.clubdelphi.com/foros/showthread.php?t=85985)

cristian22 03-06-2014 09:00:52

Carculadora ayuda
 
hola buenas tardes .. soy nuevo en este foro... eh estado trabajando con delphi en crear una carculadora hasta ahora voy bien, las operaciones basicas como suma, resta, division y multiplicacion me compilan bien
por ejemplo si yo hago 2+2= 4 me funciona pero quiero realizar una operacion convinada como "2+3-4*2"y cuando aprete la tecla "=" me tire el resultado de dicha operación, es decir que yo cuando realice "2+3" en el visor me muestre "5" al apretar la tecla - y al ingresar "4" y de nuevo al apretar la tecla * me muestre el resultado "1" y de nuevo ingrese "2" y al apretar esta vez la tecla "=" me salga el resultado que seria "2"
hay viene mi problema que no logro realizar esta tarea ; si alguien tiene alguna solución, o pueda guiarme para realizar este trabajo se los agradecería mucho aquí les dejo mi programa


Código Delphi [-]
type
  TForm2 = class(TForm)
    visor: TLabel;
    Button1: TButton;
    Button2: TButton;
    Button3: TButton;
    Button5: TButton;
    Button4: TButton;
    Button6: TButton;
    Button7: TButton;
    Button8: TButton;
    Button9: TButton;
    ButtonSuma: TButton;
    ButtonResta: TButton;
    ButtonIgual: TButton;
    ButtonProducto: TButton;
    ButtonDivision: TButton;
    button0: TButton;
    procedure button0Click(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure Button4Click(Sender: TObject);
    procedure Button5Click(Sender: TObject);
    procedure Button6Click(Sender: TObject);
    procedure Button7Click(Sender: TObject);
    procedure Button8Click(Sender: TObject);
    procedure Button9Click(Sender: TObject);
    procedure ButtonSumaClick(Sender: TObject);
    procedure ButtonRestaClick(Sender: TObject);
    procedure ButtonProductoClick(Sender: TObject);
    procedure ButtonDivisionClick(Sender: TObject);
    procedure ButtonIgualClick(Sender: TObject);
  private
  op: string;
  op2: real;
    op1:real;
  result:string;

    { variables}
  public
    { Public declarations }
  end;
 const
  Pi = 3.14;
var
  Form2: TForm2;


implementation

{$R *.dfm}

procedure TForm2.button0Click(Sender: TObject);
begin

   if (sender is tbutton) and (visor.Caption ='0') then
visor.Caption:= visor.Caption+(sender as tbutton).Caption

  else
visor.Caption:= visor.Caption+(sender as tbutton).Caption
end;

procedure TForm2.Button1Click(Sender: TObject);
begin

if (sender is tbutton) and (visor.Caption ='1') then
visor.Caption:= visor.Caption+(sender as tbutton).Caption

else
visor.Caption:= visor.Caption+(sender as tbutton).Caption
end;

procedure TForm2.Button2Click(Sender: TObject);
begin;
if (sender is tbutton) and (visor.Caption ='2') then
visor.Caption:= visor.Caption+(sender as tbutton).Caption

else
visor.Caption:= visor.Caption+(sender as tbutton).Caption
end;

procedure TForm2.Button3Click(Sender: TObject);
begin
   if (sender is tbutton) and (visor.Caption ='3') then
visor.Caption:= visor.Caption+(sender as tbutton).Caption

else
visor.Caption:= visor.Caption+(sender as tbutton).Caption
end;

procedure TForm2.Button4Click(Sender: TObject);
begin
if (sender is tbutton) and (visor.Caption ='4') then
visor.Caption:= visor.Caption+(sender as tbutton).Caption

else
visor.Caption:= visor.Caption+(sender as tbutton).Caption
end;

procedure TForm2.Button5Click(Sender: TObject);
begin
if (sender is tbutton) and (visor.Caption ='5') then
visor.Caption:= visor.Caption+(sender as tbutton).Caption

else
visor.Caption:= visor.Caption+(sender as tbutton).Caption
end;

procedure TForm2.Button6Click(Sender: TObject);
begin
if (sender is tbutton) and (visor.Caption ='6') then
visor.Caption:= visor.Caption+(sender as tbutton).Caption

else
visor.Caption:= visor.Caption+(sender as tbutton).Caption
end;

procedure TForm2.Button7Click(Sender: TObject);
begin
if (sender is tbutton) and (visor.Caption ='7') then
visor.Caption:= visor.Caption+(sender as tbutton).Caption

else
visor.Caption:= visor.Caption+(sender as tbutton).Caption
end;

procedure TForm2.Button8Click(Sender: TObject);
begin
if (sender is tbutton) and (visor.Caption ='8') then
visor.Caption:= visor.Caption+(sender as tbutton).Caption

else
visor.Caption:= visor.Caption+(sender as tbutton).Caption
end;

procedure TForm2.Button9Click(Sender: TObject);
begin
if (sender is tbutton) and (visor.Caption ='9') then
visor.Caption:= visor.Caption+(sender as tbutton).Caption

else
visor.Caption:= visor.Caption+(sender as tbutton).Caption
end;

procedure TForm2.ButtonDivisionClick(Sender: TObject);
begin
op2:= StrToFloat ( visor.Caption);
op:= '4';
visor.Caption:=''


end;

procedure TForm2.ButtonProductoClick(Sender: TObject);
begin

op2:= StrToFloat ( visor.Caption);
op:= '3';
visor.Caption:=''

end;

procedure TForm2.ButtonRestaClick(Sender: TObject);
begin

op2:= StrToFloat ( visor.Caption);
op:= '2';
visor.Caption:='';

end;

procedure TForm2.ButtonSumaClick(Sender: TObject);
begin

op2:= StrToFloat ( visor.Caption);
op:= '1';
visor.Caption:=''

end;

procedure TForm2.ButtonIgualClick(Sender: TObject);
 begin
 op1:= strtofloat(visor.Caption);
 if (op='1') then
 begin
 result:= floattostr(op2+op1);
  end
  else
  if (op='2') then
  begin
 result:= floattostr(op2-op1);
  end
  else
  if (op='3') then
  begin
result:= floattostr(op2*op1);
  end
    else
  if (op='4') then
  begin
    if (op1<>0) then
     begin
   result:= floattostr(op2/op1);
       end
       else
       showmessage('Divisor 0')

     end;
  visor.Caption:= result;
  end;
end.

engranaje 03-06-2014 13:46:43

Si te he entendido bien bastaria con llamar al onclick del igual justo al principio del onclick de cualquier otro boton de operación.

De todos modos a la hora de hacer una calculadora igual te valdría la pena componer una string utilizando la notación polaca inversa
http:////es.wikipedia.org/wiki/Notac...polaca_inversa
y resolverla cuando le des al igual, depende mucho de cual quieres que sea el comportamiento.


La franja horaria es GMT +2. Ahora son las 05:53:33.

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