用delphi写一个计算器
时间: 2023-11-23 16:05:56 浏览: 38
以下是一个简单的 Delphi 计算器程序,可以实现基本的加、减、乘、除操作:
```
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Edit1: TEdit;
Edit2: TEdit;
Button1: TButton;
Button2: TButton;
Button3: TButton;
Button4: TButton;
Label1: TLabel;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure Button4Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
var
a, b, c: Double;
begin
a := StrToFloat(Edit1.Text);
b := StrToFloat(Edit2.Text);
c := a + b;
Label1.Caption := FloatToStr(c);
end;
procedure TForm1.Button2Click(Sender: TObject);
var
a, b, c: Double;
begin
a := StrToFloat(Edit1.Text);
b := StrToFloat(Edit2.Text);
c := a - b;
Label1.Caption := FloatToStr(c);
end;
procedure TForm1.Button3Click(Sender: TObject);
var
a, b, c: Double;
begin
a := StrToFloat(Edit1.Text);
b := StrToFloat(Edit2.Text);
c := a * b;
Label1.Caption := FloatToStr(c);
end;
procedure TForm1.Button4Click(Sender: TObject);
var
a, b, c: Double;
begin
a := StrToFloat(Edit1.Text);
b := StrToFloat(Edit2.Text);
if b = 0 then
ShowMessage('除数不能为0')
else
begin
c := a / b;
Label1.Caption := FloatToStr(c);
end;
end;
end.
```
该程序包含两个文本框(Edit1 和 Edit2)和四个按钮(Button1、Button2、Button3 和 Button4),分别对应加、减、乘、除操作。当用户输入两个数字后,按下相应的按钮,程序将计算结果并在标签(Label1)中显示。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)