Calculadora Simples








unit uMenu;

{
  Autor: Paulo Henrique Tadeu Alves de Olivera
  E-Mail : paulo@liveteenslinux.com
           dr_insanebr@hotmail.com
}

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, XPMan, ActnList, Menus;

type
  TFMenu = class(TForm)
    MainMenu1: TMainMenu;
    ActionList1: TActionList;
    Editar1: TMenuItem;
    btn0: TAction;
    btn1: TAction;
    btn2: TAction;
    btn3: TAction;
    btn4: TAction;
    btn5: TAction;
    btn6: TAction;
    btn7: TAction;
    btn8: TAction;
    btn9: TAction;
    XPManifest1: TXPManifest;
    Button1: TButton;
    Button2: TButton;
    Button3: TButton;
    Button4: TButton;
    Button5: TButton;
    Button6: TButton;
    Button7: TButton;
    Button8: TButton;
    Button9: TButton;
    Button10: TButton;
    Button11: TButton;
    Button12: TButton;
    Button13: TButton;
    Button14: TButton;
    Button15: TButton;
    Button16: TButton;
    Button17: TButton;
    Button18: TButton;
    Button19: TButton;
    btnAdic: TAction;
    btnSubt: TAction;
    btnMult: TAction;
    btnDivi: TAction;
    btnABS: TAction;
    btnIgual: TAction;
    btnC: TAction;
    edtResult: TEdit;
    btnVing: TAction;
    Button23: TButton;
    btnCE: TAction;
    procedure FormKeyPress(Sender: TObject; var Key: Char);
    procedure FormCreate(Sender: TObject);
    procedure btn0Execute(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
    //Procedure Responsavel pelo funcionamento da calculadora
    procedure Click(obj :TObject);
  end;

var
  FMenu: TFMenu;
  ValorDif: Extended;
  //Teclas
  Adic, Subt, Mult, Divi, Igual, Virg: Boolean;

implementation

{$R *.dfm}

{ TFMenu }

procedure TFMenu.btn0Execute(Sender: TObject);
begin
  {
  ----------========= Executa Ações ========-----------------
  Esse ação deve ser adionanda em todos os botoes
  }
  Click(Sender);
end;

procedure TFMenu.Click(obj: TObject);
var
  ConverNumero: String;
begin
  //Retira os zeros na frente dos numero
  ConverNumero:= FloatToStr(StrToFloat(edtResult.Text));
  //Incrementa numeros no edit
  if (TAction(obj).Caption = '0') or (TAction(obj).Caption = '1') or
     (TAction(obj).Caption = '2') or (TAction(obj).Caption = '3') or
     (TAction(obj).Caption = '4') or (TAction(obj).Caption = '5') or
     (TAction(obj).Caption = '6') or (TAction(obj).Caption = '7') or
     (TAction(obj).Caption = '8') or (TAction(obj).Caption = '9')
   then
     begin
       //Checa se pode adicionar virgula
       if Virg then
         begin
           //checa se ja tem virgula se nao tem ele adiciona
           if pos(',',ConverNumero) = 0 then
             begin
               ConverNumero:= ConverNumero + ',';//Adiciona virgula
               Virg:= False;
             end;
         end;
       ConverNumero  := ConverNumero + TAction(obj).Caption;
       edtResult.Text:= FloatToStr(StrToFloat(ConverNumero));
     end
  //Ativa ação de Adição de virgula
  else if (TAction(obj).Caption = ',') then
     begin
       Virg          := True; Divi:= False; Mult:= False; Adic:= False; Subt:= False;
     end
  //Ativa ação de Divisao
  else if (TAction(obj).Caption = '/') then
     begin
       Divi          := True; Virg:= False; Mult:= False; Adic:= False; Subt:= False;
       ValorDif      := StrToFloat(edtResult.Text);
       edtResult.Text:= '0';
     end
  //Ativa ação de Multiplicação
  else if (TAction(obj).Caption = '*') then
     begin
       Mult          := True; Virg:= False; Divi:= False; Adic:= False; Subt:= False;
       ValorDif      := StrToFloat(edtResult.Text);
       edtResult.Text:= '0';
     end
  //Ativa ação de Soma
  else if (TAction(obj).Caption = '+') then
     begin
       Adic          := True; Virg:= False; Divi:= False; Mult:= False; Subt:= False;
       ValorDif      := StrToFloat(edtResult.Text);
       edtResult.Text:= '0';
     end
  //Ativa ação de Subtrair
  else if (TAction(obj).Caption = '-') then
     begin
       Subt          := True; Virg:= False; Divi:= False; Mult:= False; Adic:= False;
       ValorDif      := StrToFloat(edtResult.Text);
       edtResult.Text:= '0';
     end
   //Faz o Resultado
   else if (TAction(obj).Caption = '=') then
     begin
       if (Adic = True) or (Subt = True) or (Mult = True) or
          (Divi = True) or (Igual = True) then
          begin
            {executa os calculos}
            if adic then //Executa soma
               edtResult.Text := FloatToStr(ValorDif + StrToFloat(edtResult.text));
            if Subt then //Executa Subtração
               edtResult.Text := FloatToStr(ValorDif - StrToFloat(edtResult.text));
            if Divi then //Executa Divisão
               edtResult.Text := FloatToStr(ValorDif / StrToFloat(edtResult.text));
            if Mult then //Executa Multiplicação
               edtResult.Text := FloatToStr(ValorDif * StrToFloat(edtResult.text));
          end;
     end
   //Zera o Edit
   else if (TAction(obj).Caption = 'C') then
     begin
       edtResult.Text:= '0';
     end
   //Reseta tudo
   else if (TAction(obj).Caption = 'CE') then
     begin
       edtResult.Text:= '0';
       ValorDif      := 0;
       Adic          := False; Virg:= False; Divi:= False; Mult:= False; Subt:= False;
     end
   //Troca Sinais do Valores
   else if (TAction(obj).Caption = '+/-') then
     begin
       if StrToFloat(edtResult.text) > 0 then
          edtResult.Text:=  FloatToStr(((0-1)*(abs(StrToFloat(edtResult.text)))))
       else if StrToFloat(edtResult.text) < 0 then
          edtResult.Text:=  FloatToStr(((0+1)*(abs(StrToFloat(edtResult.text)))));
     end;
end;

procedure TFMenu.FormCreate(Sender: TObject);
begin
  //Reseta as variaveis
  Adic := False; Virg:= False; Divi:= False; Mult:= False; Subt:= False;
  ValorDif:= 0;
end;

procedure TFMenu.FormKeyPress(Sender: TObject; var Key: Char);
begin
  //Executa click manuais sem necessidade usar o mouse
  if key = '0' then btn0.Execute; if key = '1' then btn1.Execute;
  if key = '2' then btn2.Execute; if key = '3' then btn3.Execute;
  if key = '4' then btn4.Execute; if key = '5' then btn5.Execute;
  if key = '6' then btn6.Execute; if key = '7' then btn7.Execute;
  if key = '8' then btn8.Execute; if key = '9' then btn9.Execute;

  if key = ',' then btnVing.Execute; if key = '+' then btnAdic.Execute;
  if key = '-' then btnSubt.Execute; if key = '*' then btnMult.Execute;
  if key = '/' then btnDivi.Execute; if key = 'c' then btnC.Execute;

  if key in ['=',#13] then btnIgual.Execute;
end;

end.