博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
delphi实现计算器
阅读量:6382 次
发布时间:2019-06-23

本文共 4663 字,大约阅读时间需要 15 分钟。

这篇文章是用delphi做了一个简单的计算器,代码如下:
 
unit Unit1; 


interface 


uses 

    Windows, 

    Messages, 

    SysUtils, 

    Variants, 

    Classes, 

    Graphics, 

    Controls, 

    Forms, 

    Dialogs, 

    StdCtrls; 


type 

    TForm1 = class(TForm) 

        Edit1: TEdit; 

        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; 

        procedure Button4Click(Sender: TObject); 

        procedure Button1Click(Sender: TObject); 

        procedure Button2Click(Sender: TObject); 

        procedure Button3Click(Sender: TObject); 

        procedure Button5Click(Sender: TObject); 

        procedure Button6Click(Sender: TObject); 

        procedure Button7Click(Sender: TObject); 

        procedure Button9Click(Sender: TObject); 

        procedure Button10Click(Sender: TObject); 

        procedure Button11Click(Sender: TObject); 

        procedure Button13Click(Sender: TObject); 

        procedure Button8Click(Sender: TObject); 

        procedure Button12Click(Sender: TObject); 

        procedure Button15Click(Sender: TObject); 

        procedure Button16Click(Sender: TObject); 

        procedure Button17Click(Sender: TObject); 

        procedure Button14Click(Sender: TObject); 

    private 

        { Private declarations } 

    public 

        { Public declarations } 

    end; 

type //自定义一个Tcompute的类 


    Tcompute = object 

    private //保护类中成员数据的安全 


        temp1: double; 

        temp2: double; 

        temp3: double; 

        sign: Char; 


    public //把类中的成员函数进行共享 



        function Add(x: double; y: double): Double; 

        function Sub(x: double; y: double): Double; 

        function Mult(x: double; y: double): Double; 

        function Dived(x: double; y: double): Double; 


    end; 

var 

    Form1: TForm1; 

    Info: Tcompute; //声明Info是类Tcompute的对象 



implementation 


function Tcompute.Add(x: double; y: double): Double; //进行类的定义与实现 


begin 

    Result := x + y; 

end; 


function Tcompute.Sub(x: double; y: double): Double; 

begin 

    Result := x - y; 

end; 


function Tcompute.Mult(x: double; y: double): Double; 

begin 

    Result := x * y; 

end; 


function Tcompute.Dived(x: double; y: double): Double; 

begin 

if (y = 0) then 

    begin 

         Form1.Edit1.text:='1111111111'; 

        //ShowMessage('除数不能为0!'); //这里判断除数不能为0 


        Result := x; 

    end 

    else 

        Result := x / y; 

end; 


{$R *.dfm} 


procedure TForm1.Button4Click(Sender: TObject); 


begin 

    Edit1.Clear; 

    Info.temp1 := 0; 

    Info.temp2 := 0; 

end; 


procedure TForm1.Button1Click(Sender: TObject); 

begin 

    Edit1.Text := Edit1.text + '7'; 

    Info.temp1 := StrTofloat(Edit1.text); 

end; 


procedure TForm1.Button2Click(Sender: TObject); 


begin 

    Edit1.Text := Edit1.text + '8'; 

    Info.temp1 := StrTofloat(Edit1.text); 

end; 


procedure TForm1.Button3Click(Sender: TObject); 


begin 

    Edit1.Text := Edit1.text + '9'; 

    Info.temp1 := StrTofloat(Edit1.text); 

end; 


procedure TForm1.Button5Click(Sender: TObject); 


begin 

    Edit1.Text := Edit1.text + '4'; 

    Info.temp1 := StrTofloat(Edit1.text); 

end; 


procedure TForm1.Button6Click(Sender: TObject); 


begin 

    Edit1.Text := Edit1.text + '5'; 

    Info.temp1 := StrTofloat(Edit1.text); 

end; 


procedure TForm1.Button7Click(Sender: TObject); 


begin 

    Edit1.Text := Edit1.text + '6'; 

    Info.temp1 := StrTofloat(Edit1.text); 

end; 


procedure TForm1.Button9Click(Sender: TObject); 

begin 

    Edit1.Text := Edit1.text + '1'; 

    Info.temp1 := StrTofloat(Edit1.text); 

end; 


procedure TForm1.Button10Click(Sender: TObject); 

begin 

    Edit1.Text := Edit1.text + '2'; 

    Info.temp1 := StrTofloat(Edit1.text); 

end; 


procedure TForm1.Button11Click(Sender: TObject); 

begin 

    Edit1.Text := Edit1.text + '3'; 

    Info.temp1 := StrTofloat(Edit1.text); 

end; 


procedure TForm1.Button13Click(Sender: TObject); 

begin 

    Edit1.Text := Edit1.text + '0'; 

    Info.temp1 := StrTofloat(Edit1.text); 

end; 


procedure TForm1.Button8Click(Sender: TObject); 


begin 

    Info.temp2 := Info.temp1; 

    Info.sign := '+'; 

    Edit1.Clear; 

end; 


procedure TForm1.Button12Click(Sender: TObject); 

begin 


    Info.temp2 := Info.temp1; 

    Info.sign := '-'; 

    Edit1.Clear; 

end; 


procedure TForm1.Button15Click(Sender: TObject); 

begin 


    Info.temp2 := Info.temp1; 

    Info.sign := '*'; 

    Edit1.Clear; 

end; 


procedure TForm1.Button16Click(Sender: TObject); 

begin 

    Info.temp2 := Info.temp1; 

    Info.sign := '/'; 

    Edit1.Clear; 

end; 


procedure TForm1.Button17Click(Sender: TObject); 

begin 


    if (Info.sign = '+') then 

        Edit1.text := FloatToStr(Info.Add(Info.temp2, Info.temp1)) 

    else 

        if (Info.sign = '-') then 

            Edit1.text := FloatToStr(Info.Sub(Info.temp2, Info.temp1)) 

        else 

            if (Info.sign = '*') then 

                Edit1.text := FloatToStr(Info.Mult(Info.temp2, Info.temp1)) 

            else 

                if (Info.sign = '/') then 

                    Edit1.text := FloatToStr(Info.Dived(Info.temp2, Info.temp1)); 

    Info.temp1 := StrToFloat(Edit1.Text); 


end; 


procedure TForm1.Button14Click(Sender: TObject); 

begin 


    Edit1.Text := Edit1.Text + '.'; 

    Info.temp1 := StrToFloat(Edit1.Text); 


end; 


end.
 
 
本文转自wiliiwin 51CTO博客,原文链接:http://blog.51cto.com/wiliiwin/202984

转载地址:http://yckha.baihongyu.com/

你可能感兴趣的文章
如何更好的使用module vuex?
查看>>
java B2B2C Springcloud电子商城系统-断路器(Hystrix)
查看>>
MySQL忘记密码的正确解决方法
查看>>
2012年报刊杂志订阅目录【全面 1900条记录】
查看>>
apache和tomcat区别_三
查看>>
mac brew安装的mysql 无法远程访问的问题
查看>>
位、字节和字
查看>>
软件测试基础(我以前的一些笔记,希望对大家有帮助,有错漏的地方希望大家指出)...
查看>>
node.js 的错误提示
查看>>
helloword
查看>>
防重复请求处理的实践与总结
查看>>
聊聊hikari连接池的isAllowPoolSuspension
查看>>
从PHP语法糖剖析Zend VM引擎
查看>>
git命令
查看>>
Linux—寻找到的起源!
查看>>
arm-linux gdb调试工具的安装
查看>>
Scala正则表达式问题
查看>>
Oracle自动内存管理的几个小问题
查看>>
dhcp协议交互报文
查看>>
运维学习之openssh-server命令运用及控制
查看>>