⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 unit2.pas

📁 用delphi制作的一个简单的计算器
💻 PAS
字号:
unit Unit2;

interface

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

type
  TForm2 = class(TForm)
    BackSpaceBtn: TButton;
    CBtn: TButton;
    Btn_7: TButton;
    Btn_8: TButton;
    Btn_9: TButton;
    Btn_6: TButton;
    Btn_5: TButton;
    Btn_4: TButton;
    Btn_3: TButton;
    Btn_2: TButton;
    Btn_1: TButton;
    Btn_Dot: TButton;
    Btn_0: TButton;
    Btn_Divide: TButton;
    Btn_Multiply: TButton;
    Btn_SubStract: TButton;
    Btn_Add: TButton;
    Btn_Equal: TButton;
    CEBtn: TButton;
    Edit1: TEdit;
    procedure BackSpaceBtnClick(Sender: TObject);
    procedure Btn_0Click(Sender: TObject);
    procedure Btn_9Click(Sender: TObject);
    procedure Btn_8Click(Sender: TObject);
    procedure Btn_7Click(Sender: TObject);
    procedure Btn_6Click(Sender: TObject);
    procedure Btn_5Click(Sender: TObject);
    procedure Btn_4Click(Sender: TObject);
    procedure Btn_3Click(Sender: TObject);
    procedure Btn_DivideClick(Sender: TObject);
    procedure Btn_MultiplyClick(Sender: TObject);
    procedure Btn_SubStractClick(Sender: TObject);
    procedure CBtnClick(Sender: TObject);
    procedure Btn_DotClick(Sender: TObject);
    procedure Btn_2Click(Sender: TObject);
    procedure Btn_EqualClick(Sender: TObject);
    procedure Btn_AddClick(Sender: TObject);
    procedure Btn_1Click(Sender: TObject);
    procedure Calc(x,y:double;op:Char);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form2: TForm2;
  opnum1,opnum2:string;
  ca_operator:char;
  operated:boolean;

implementation

{$R *.dfm}
procedure TForm2.Calc(x,y:double;op:Char);
var
  temresult:double;
begin

   case op of
     '+': temresult := x + y;
     '-': temresult := x - y;
     '*': temresult := x * y;
     '/': temresult := x / y;
     //'%': Calc := Round(x) mod Round(y);
   end;
  opnum1:=floattostr(temresult);
  opnum2:='';
  ca_operator:=' ';
  edit1.Text:=opnum1;
end;
procedure TForm2.Btn_1Click(Sender: TObject);
begin
  if operated=false then
     begin
       opnum1:=opnum1+'1';
       edit1.Text:=opnum1;
     end;
  if (operated=true)and(opnum1<>'') then
     begin
       opnum2:=opnum2+'1';
       edit1.Text:=opnum2;
     end;
end;

procedure TForm2.Btn_AddClick(Sender: TObject);
begin
   operated:=true;
   if (opnum1<>'') and (opnum2<>'') then
      begin
         Calc(strtofloat(opnum1),strtofloat(opnum2),ca_operator);
      end;
   ca_operator:='+';
end;

procedure TForm2.Btn_EqualClick(Sender: TObject);
begin
  if (opnum1<>'') and (opnum2<>'') and (ca_operator<>' ') then
      begin
         Calc(strtofloat(opnum1),strtofloat(opnum2),ca_operator);
      end;
end;

procedure TForm2.Btn_2Click(Sender: TObject);
begin
  if operated=false then
     begin
       opnum1:=opnum1+'2';
       edit1.Text:=opnum1;
     end;
  if (operated=true)and(opnum1<>'') then
     begin
       opnum2:=opnum2+'2';
       edit1.Text:=opnum2;
     end;
end;

procedure TForm2.Btn_DotClick(Sender: TObject);
begin
  if operated=false then
     begin
       opnum1:=opnum1+'.';
       edit1.Text:=opnum1;
     end;
  if (operated=true)and(opnum1<>'') then
     begin
       opnum2:=opnum2+'.';
       edit1.Text:=opnum2;
     end;
end;

procedure TForm2.CBtnClick(Sender: TObject);
begin
  edit1.Clear;
  opnum2:='';
  opnum1:='';
  ca_operator:=' ';
  operated:=false;
end;

procedure TForm2.Btn_SubStractClick(Sender: TObject);
begin
   operated:=true;
   if (opnum1<>'') and (opnum2<>'') then
      begin
         Calc(strtofloat(opnum1),strtofloat(opnum2),ca_operator);
      end;
   ca_operator:='-';
end;

procedure TForm2.Btn_MultiplyClick(Sender: TObject);
begin
   operated:=true;
   if (opnum1<>'') and (opnum2<>'') then
      begin
         Calc(strtofloat(opnum1),strtofloat(opnum2),ca_operator);
      end;
   ca_operator:='*';
end;

procedure TForm2.Btn_DivideClick(Sender: TObject);
begin
   operated:=true;
   if (opnum1<>'') and (opnum2<>'') then
      begin
         Calc(strtofloat(opnum1),strtofloat(opnum2),ca_operator);
      end;
   ca_operator:='/';
end;

procedure TForm2.Btn_3Click(Sender: TObject);
begin
  if operated=false then
     begin
       opnum1:=opnum1+'3';
       edit1.Text:=opnum1;
     end;
  if (operated=true)and(opnum1<>'') then
     begin
       opnum2:=opnum2+'3';
       edit1.Text:=opnum2;
     end;
end;

procedure TForm2.Btn_4Click(Sender: TObject);
begin
  if operated=false then
     begin
       opnum1:=opnum1+'4';
       edit1.Text:=opnum1;
     end;
  if (operated=true)and(opnum1<>'') then
     begin
       opnum2:=opnum2+'4';
       edit1.Text:=opnum2;
     end;
end;

procedure TForm2.Btn_5Click(Sender: TObject);
begin
  if operated=false then
     begin
       opnum1:=opnum1+'5';
       edit1.Text:=opnum1;
     end;
  if (operated=true)and(opnum1<>'') then
     begin
       opnum2:=opnum2+'5';
       edit1.Text:=opnum2;
     end;
end;

procedure TForm2.Btn_6Click(Sender: TObject);
begin
  if operated=false then
     begin
       opnum1:=opnum1+'6';
       edit1.Text:=opnum1;
     end;
  if (operated=true)and(opnum1<>'') then
     begin
       opnum2:=opnum2+'6';
       edit1.Text:=opnum2;
     end;
end;

procedure TForm2.Btn_7Click(Sender: TObject);
begin
  if operated=false then
     begin
       opnum1:=opnum1+'7';
       edit1.Text:=opnum1;
     end;
  if (operated=true)and(opnum1<>'') then
     begin
       opnum2:=opnum2+'7';
       edit1.Text:=opnum2;
     end;
end;

procedure TForm2.Btn_8Click(Sender: TObject);
begin
  if operated=false then
     begin
       opnum1:=opnum1+'8';
       edit1.Text:=opnum1;
     end;
  if (operated=true)and(opnum1<>'') then
     begin
       opnum2:=opnum2+'8';
       edit1.Text:=opnum2;
     end;
end;

procedure TForm2.Btn_9Click(Sender: TObject);
begin
  if operated=false then
     begin
       opnum1:=opnum1+'9';
       edit1.Text:=opnum1;
     end;
  if (operated=true)and(opnum1<>'') then
     begin
       opnum2:=opnum2+'9';
       edit1.Text:=opnum2;
     end;
end;

procedure TForm2.Btn_0Click(Sender: TObject);
begin
  if operated=false then
     begin
       opnum1:=opnum1+'0';
       edit1.Text:=opnum1;
     end;
  if (operated=true)and(opnum1<>'') then
     begin
       opnum2:=opnum2+'0';
       edit1.Text:=opnum2;
     end;
end;

procedure TForm2.BackSpaceBtnClick(Sender: TObject);
var
  tmpstr,resultstr:string;
  i:integer;
begin
  tmpstr:=edit1.Text;
  resultstr:='';
  for i:=1 to length(tmpstr)-1 do
      resultstr:= resultstr+tmpstr[i];
  edit1.Text:=resultstr;
end;

end.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -