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

📄 unitmain.pas

📁 delphi的一个非常好的计算器程序。 绝对很好!
💻 PAS
字号:
unit UnitMain;

interface

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

type
    TFormMain = class(TForm)
    MainMenu: TMainMenu;
    N1: TMenuItem;
    C1: TMenuItem;
    VC1: TMenuItem;
    N2: TMenuItem;
    V1: TMenuItem;
    N3: TMenuItem;
    N4: TMenuItem;
    N5: TMenuItem;
    H1: TMenuItem;
    H2: TMenuItem;
    N6: TMenuItem;
    A1: TMenuItem;
    spdBtnEqul: TSpeedButton;
    SpeedButton2: TSpeedButton;
    spdBtn0: TSpeedButton;
    spdBtnSign: TSpeedButton;
    spdBtnDS: TSpeedButton;
    spdBtnPoint: TSpeedButton;
    spdBtnAdd: TSpeedButton;
    spdBtnSqrt: TSpeedButton;
    SpeedButton9: TSpeedButton;
    spdBtn1: TSpeedButton;
    spdBtn4: TSpeedButton;
    spdBtn7: TSpeedButton;
    spdBtn2: TSpeedButton;
    spdBtn5: TSpeedButton;
    spdBtn3: TSpeedButton;
    spdBtn9: TSpeedButton;
    spdBtnSub: TSpeedButton;
    spdBtn6: TSpeedButton;
    spdBtnMS: TSpeedButton;
    spdBtnM: TSpeedButton;
    spdBtnDiv: TSpeedButton;
    spdBtnMult: TSpeedButton;
    spdBtnClear: TSpeedButton;
    SpeedButton25: TSpeedButton;
    spdBtnMC: TSpeedButton;
    spdBtnMR: TSpeedButton;
    spdBtnCE: TSpeedButton;
    Panel1: TPanel;
    spdBtn8: TSpeedButton;
    edtInput: TEdit;
    procedure ProcessMessage(Sender: TObject);
    procedure Initcalculator();
    procedure InitStatus();
    procedure FirstNum();
    procedure Sign();
    procedure SecondNum();
    procedure FormCreate(Sender: TObject);
    procedure  calculate();

  private
    { Private declarations }
    dFstNum : Double;
    dSndNum : Double;
    dResult : Double;
    iSign   : Integer;
    iBitNum : Integer;
    strInput: String;
    iInput  : Integer;
    iIsInputSnd:Integer;
    iStatusMachine: Integer;

  public
    { Public declarations }

  end;

var
FormMain: TFormMain;

implementation

{$R *.dfm}

procedure TFormMain.ProcessMessage(Sender: TObject);
begin

   strInput:=(Sender as TspeedButton).Caption;
   //输入字符串转为整型常量
   if(strInput='0') then iInput:=INPUT_ZERO
   else if(strInput='1')then iInput:=INPUT_ONE
   else if(strInput='2')then iInput:=INPUT_TWO
   else if(strInput='3')then iInput:=INPUT_THREE
   else if(strInput='4')then iInput:=INPUT_FOUR
   else if(strInput='5')then iInput:=INPUT_FIVE
   else if(strInput='6')then iInput:=INPUT_SIX
   else if(strInput='7')then iInput:=INPUT_SEVEN
   else if(strInput='8')then iInput:=INPUT_EIGHT
   else if(strInput='9')then iInput:=INPUT_NINE
   else if(strInput='+')then iInput:=INPUT_ADD
   else if(strInput='-')then iInput:=INPUT_SUB
   else if(strInput='*')then iInput:=INPUT_MUL
   else if(strInput='/')then iInput:=INPUT_DIV
   else if(strInput='=')then iInput:=INPUT_EQUAL
   else if(strInput='.')then iInput:=INPUT_POINT
   else if(strInput='C')then iInput:=INPUT_C;

  case iStatusMachine of                        //状态判断并调用函数处理
  INIT_STATUS:InitStatus() ;
  INPUT_FIRSTNUM_STATUS:FirstNum();
  INPUT_SIGN_STATUS:  Sign();
  INPUT_SECONDNUM_STATUS: SecondNum();
  end;
end;

procedure TFormMain.FormCreate(Sender: TObject);
begin
 initcalculator();
end;

procedure TFormMain.Initcalculator();
begin
  dFstNum:=0;
  dSndNum:=0;
  dResult:=0;
  iSign:=0;
  iBitNum:=-1;
  strInput:='';
  iInput:=-1;
  iIsInputSnd:=0;
  iStatusMachine:=INIT_STATUS;
end;

procedure TFormMain.InitStatus();
begin
case iInput of//
0..9:                    //输入数字时
begin
  dFstNum:=iInput;
  iStatusMachine:=INPUT_FIRSTNUM_STATUS;
  edtInput.Text:=IntToStr(iInput);
end;

INPUT_POINT:             //输入小数点时
begin
iStatusMachine:=INPUT_FIRSTNUM_STATUS;
iBitNum:=0;
end;
end;//case
end;

procedure TFormMain.FirstNum();
var
i:integer;
dInput: Double;    //当处理小数是用到
begin

case iInput of//
0..9:
begin
if(iBitNum<0)then        //当前数为整数时
begin
dfstNum:=dfstNum*10+iInput;
edtInput.Text:= FloattoStr(dfstNum);
end
else                   //当前数为小数时 即iBitMun>=0
begin
iBItNum:=iBitNum+1;    //小数位增1
dInput:=iInput;
if(iInput=0)then    //当输入为0时,因为小数点最后面0无意义,
                    //要在原来数后面显示0
begin
if(iBItNum=1)then edtInput.Text:=FloattoStr(dfstNum)+'.0'//当小数点后面第一位是0
else edtInput.Text:=FloattoStr(dfstNum)+'0';
end
else
begin
for i:=0 to iBItNum-1 do
begin
dInput:=dInput/10;
end;
dfstNum :=dfstNum+dInput;
edtInput.Text:= FloattoStr(dfstNum);
end;
end;//else
end;//0..9

INPUT_POINT:
begin
if(iBitNum<0)then iBitNum:=0;
end;  // INPUT_POINT

INPUT_ADD..INPUT_DIV:
begin
iSign :=iInput;
iBItNum:=-1;   //还原
iStatusMachine :=INPUT_SIGN_STATUS;
iBitNum:=0;
end;
INPUT_EQUAL:
InitCalculator();
INPUT_C:
begin
InitCalculator();
edtInput.Text:='0';
end;
end;//case
end;

procedure TFormMain.Sign();
begin
case iInput of//
0..9:
begin
iIsInputSnd:=1;
dsndNum:=iInput;
iStatusMachine:=INPUT_SECONDNUM_STATUS;
edtInput.Text:=IntToStr(iInput);
 end;
INPUT_ADD..INPUT_DIV:
 begin
iSign:=iInput;
iIsInputSnd:=0;
end;
INPUT_EQUAL:
begin
if(iIsInputSnd=0)then
begin
dsndNum:=dfstNum;
iIsInputSnd:=1;
end;
calculate();
edtInput.Text:=FloattoStr(dResult);
dfstNum:=dResult;
iStatusMachine:=INPUT_SECONDNUM_STATUS ;
end;

INPUT_POINT :
begin
iBitNum:=0;
iStatusMachine:=INPUT_SECONDNUM_STATUS;
end;

INPUT_C:
begin
InitCalculator();
edtInput.Text:='0';
end;
end;//case
end;

procedure TFormMain.SecondNum();
var
i:integer;
dInput: Double;    //当处理小数时用到
begin

case iInput of//
0..9:
begin
if(iBitNum<0)then        //当前数为整数时
begin
dsndNum:=dsndNum*10+iInput;
edtInput.Text:= FloattoStr(dsndNum);
end
else                   //当前数为小数时 即iBitMun>=0
begin
iBItNum:=iBitNum+1;    //小数位增1
dInput:=iInput;
if(iInput=0)then    //当输入为0时,因为小数点最后面0无意义,
                    //要在原来数后面显示0
begin
if(iBItNum=1)then edtInput.Text:=FloattoStr(dsndNum)+'.0'//当小数点后面第一位是0
else edtInput.Text:=FloattoStr(dsndNum)+'0';
end
else
begin
for i:=0 to iBItNum-1 do
begin
dInput:=dInput/10;
end;
dsndNum :=dsndNum+dInput;
edtInput.Text:= FloattoStr(dsndNum);
end;
end;//else
end;//0..9

INPUT_POINT:
begin
if(iBitNum<0)then iBitNum:=0;
end;  // INPUT_POINT

INPUT_ADD..INPUT_DIV:
begin
calculate();
edtInput.Text:=FloattoStr(dResult);
dfstNum:=dResult;
iSign :=iInput;
iBitNum:=0;
iStatusMachine :=INPUT_SIGN_STATUS;
end;

INPUT_EQUAL:
begin
calculate();
edtInput.Text:=FloattoStr(dResult);
dfstNum:=dResult;
iBitNum:=0;
iStatusMachine :=INPUT_SIGN_STATUS;
end;

INPUT_C:
begin
InitCalculator();
edtInput.Text:='0';
end;
end;//case
end;

procedure TFormMain.calculate();
begin
case iSign of
INPUT_ADD:dResult:=dfstNum+dSndNum;
INPUT_SUB:dResult:=dfstNum-dSndNum;
INPUT_MUL:dResult:=dfstNum*dsndNum;
INPUT_DIV:dResult:=dfstNum/dsndNum;
end;
end;
end.


⌨️ 快捷键说明

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