delphi第五堂课 做了个计算器部分功能无法实现
本帖最后由 月平 于 2014-4-6 15:08 编辑andorxor不知道如何实现高手指点。
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, jpeg, ExtCtrls;
type
TForm1 = class(TForm)
edt1: TEdit;
btn1: TButton;
btn2: TButton;
btn3: TButton;
btn4: TButton;
btn5: TButton;
btn6: TButton;
btn7: TButton;
btn8: TButton;
btn9: TButton;
btn10: TButton;
btn11: TButton;
btn12: TButton;
btn13: TButton;
btn14: TButton;
btn15: TButton;
btn16: TButton;
btn17: TButton;
btn18: TButton;
btn19: TButton;
btn20: TButton;
btn21: TButton;
btn22: TButton;
btn23: TButton;
img1: TImage;
img2: TImage;
procedure btn3Click(Sender: TObject);
procedure btn2Click(Sender: TObject);
procedure btn1Click(Sender: TObject);
procedure btn6Click(Sender: TObject);
procedure btn5Click(Sender: TObject);
procedure btn4Click(Sender: TObject);
procedure btn9Click(Sender: TObject);
procedure btn8Click(Sender: TObject);
procedure btn7Click(Sender: TObject);
procedure btn10Click(Sender: TObject);
procedure btn11Click(Sender: TObject);
procedure btn12Click(Sender: TObject);
procedure btn23Click(Sender: TObject);
procedure btn14Click(Sender: TObject);
procedure btn15Click(Sender: TObject);
procedure btn16Click(Sender: TObject);
procedure btn21Click(Sender: TObject);
procedure btn17Click(Sender: TObject);
procedure btn22Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
i,j,k:real;
jia,jian,cheng,chu,dengyu:Boolean;
a,zf:Boolean;
implementation
{$R *.dfm}
procedure TForm1.btn3Click(Sender: TObject);
begin
if a=True
then
begin
edt1.Clear;
a:=False;
end;
if edt1.Text='0'
then edt1.Text:='9'
else edt1.Text:=edt1.Text+'9';
j:=StrToFloat(edt1.Text);
end;
procedure TForm1.btn2Click(Sender: TObject);
begin
if a=True
then
begin
edt1.Clear;
a:=False;
end;
if edt1.Text='0'
then edt1.Text:='8'
else edt1.Text:=edt1.Text+'8';
j:=StrToFloat(edt1.Text);
end;
procedure TForm1.btn1Click(Sender: TObject);
begin
if a=True
then
begin
edt1.Clear;
a:=False;
end;
if edt1.Text='0'
then edt1.Text:='7'
else edt1.Text:=edt1.Text+'7';
j:=StrToFloat(edt1.Text);
end;
procedure TForm1.btn6Click(Sender: TObject);
begin
if a=True
then
begin
edt1.Clear;
a:=False;
end;
if edt1.Text='0'
then edt1.Text:='6'
else edt1.Text:=edt1.Text+'6';
j:=StrToFloat(edt1.Text);
end;
procedure TForm1.btn5Click(Sender: TObject);
begin
if a=True
then
begin
edt1.Clear;
a:=False;
end;
if edt1.Text='0'
then edt1.Text:='5'
else edt1.Text:=edt1.Text+'5';
j:=StrToFloat(edt1.Text);
end;
procedure TForm1.btn4Click(Sender: TObject);
begin
if a=True
then
begin
edt1.Clear;
a:=False;
end;
if edt1.Text='0'
then edt1.Text:='4'
else edt1.Text:=edt1.Text+'4';
j:=StrToFloat(edt1.Text);
end;
procedure TForm1.btn9Click(Sender: TObject);
begin
if a=True
then
begin
edt1.Clear;
a:=False;
end;
if edt1.Text='0'
then edt1.Text:='3'
else edt1.Text:=edt1.Text+'3';
j:=StrToFloat(edt1.Text);
end;
procedure TForm1.btn8Click(Sender: TObject);
begin
if a=True
then
begin
edt1.Clear;
a:=False;
end;
if edt1.Text='0'
then edt1.Text:='2'
else edt1.Text:=edt1.Text+'2';
j:=StrToFloat(edt1.Text);
end;
procedure TForm1.btn7Click(Sender: TObject);
begin
if a=True
then
begin
edt1.Clear;
a:=False;
end;
if edt1.Text='0'
then edt1.Text:='1'
else edt1.Text:=edt1.Text+'1';
j:=StrToFloat(edt1.Text);
end;
procedure TForm1.btn10Click(Sender: TObject);
begin
if a=True
then
begin
edt1.Clear;
a:=False;
end;
if edt1.Text='0'
then edt1.Text:='0'
else edt1.Text:=edt1.Text+'0';
j:=StrToFloat(edt1.Text);
end;
procedure TForm1.btn11Click(Sender: TObject);
begin
if a=True
then
begin
edt1.Clear;
a:=False;
end;
if edt1.Text='0'
then edt1.Text:='00'
else edt1.Text:=edt1.Text+'00';
j:=StrToFloat(edt1.Text);
end;
procedure TForm1.btn12Click(Sender: TObject);
begin
edt1.Text:=edt1.Text+'.';
end;
procedure TForm1.btn23Click(Sender: TObject);
begin
dengyu:=True;
a:=True;
edt1.Clear;
if jia=True
then edt1.Text:=FloatToStr(i+j)
else if jian=True
then edt1.Text:=FloatToStr(i-j)
else if cheng=True
then edt1.Text:=FloatToStr(i*j)
else if chu=True
then
if j<>0
then edt1.Text:=FloatToStr(i/j)
else ShowMessage('0不能做除数');
end;
procedure TForm1.btn14Click(Sender: TObject);
begin
a:=True;
jia:=True;
jian:=False;
cheng:=False;
chu:=False;
i:=StrToFloat(edt1.text);
end;
procedure TForm1.btn15Click(Sender: TObject);
begin
a:=True;
jia:=False;
jian:=True;
cheng:=False;
chu:=False;
i:=StrToFloat(edt1.text);
end;
procedure TForm1.btn16Click(Sender: TObject);
begin
a:=True;
jia:=False;
jian:=False;
cheng:=True;
chu:=False;
i:=StrToFloat(edt1.text);
end;
procedure TForm1.btn21Click(Sender: TObject);
begin
a:=True;
jia:=False;
jian:=False;
cheng:=False;
chu:=True;
i:=StrToFloat(edt1.text);
end;
procedure TForm1.btn17Click(Sender: TObject);
begin
edt1.Text:='';
end;
procedure TForm1.btn22Click(Sender: TObject);
begin
edt1.Text:=FloatToStr(StrToFloat(edt1.Text)/100)
end;
end.
好给力,好牛逼。你的逻辑颠覆了人类。 cxj98 发表于 2014-4-6 16:58 static/image/common/back.gif
好给力,好牛逼。你的逻辑颠覆了人类。
:sweat:甲鱼老师才讲到第五堂课的 本帖最后由 rmb788520 于 2014-4-8 12:37 编辑
以前做了个简易计算器。 你说的And Or Xor都有。程序还有一点BUG懒改 你参考下
代码:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Menus,ClipBrd,ShellAPI, ActnMan, ActnColorMaps;
type
TForm1 = class(TForm)
Edit1: TEdit;
Button_sqr: TButton;
Button_Back: TButton;
Button_NotData: TButton;
Button_DivData: TButton;
Button_MulData: TButton;
Button_SubData: TButton;
Button_AddData: TButton;
Button_CountData: TButton;
Button_Nothing: TButton;
MainMenu1: TMainMenu;
N1: TMenuItem;
CopyData: TMenuItem;
PasteData: TMenuItem;
N4: TMenuItem;
About: TMenuItem;
GroupBox1: TGroupBox;
Button_Or: TButton;
Button_Xor: TButton;
Button_Shl: TButton;
Button_Shr: TButton;
Button_And: TButton;
Button1: TButton;
Button2: TButton;
procedure Button_sqrClick(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure Button_BackClick(Sender: TObject);
procedure Edit1KeyPress(Sender: TObject; var Key: Char);
procedure Button_NotDataClick(Sender: TObject);
procedure Button_NothingClick(Sender: TObject);
procedure CopyDataClick(Sender: TObject);
procedure AboutClick(Sender: TObject);
procedure PasteDataClick(Sender: TObject);
procedure Button_AddDataClick(Sender: TObject);
procedure Button_SubDataClick(Sender: TObject);
procedure Button_DivDataClick(Sender: TObject);
procedure Button_MulDataClick(Sender: TObject);
procedure Button_CountDataClick(Sender: TObject);
procedure Button_AndClick(Sender: TObject);
procedure Button_OrClick(Sender: TObject);
procedure Button_XorClick(Sender: TObject);
procedure Button_ShlClick(Sender: TObject);
procedure Button_ShrClick(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
RealDataX:Real=0;
RealDataY:Real=0;
ExDataX:Extended=0;
ExDataY:Extended=0;
DataType:Integer=0;
implementation
{$R *.dfm}
procedure TForm1.FormCreate(Sender: TObject); //把编辑框光标放到右边。窗口置顶
begin
SetWindowLong(Edit1.Handle,GWL_STYLE,GetWindowLong(Edit1.Handle,GWL_STYLE)Or ES_RIGHT);
Edit1.Invalidate;
SetWindowPos(Form1.Handle,HWND_TOPMOST,0,0,0,0,SWP_NOSIZE+SWP_NOMOVE);
end;
procedure TForm1.Button_BackClick(Sender: TObject); //退格
begin
Edit1.Text:=Copy(Edit1.Text,1,Length(Edit1.Text)-1);
end;
procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char); //过滤 除了数字以外的其他数
begin
if Not ((Key in ['0'..'9']) Or (Key = #8) Or (Key = '.') Or (Key = '-')) then
Key:=#0
end;
procedure TForm1.CopyDataClick(Sender: TObject); //菜单中的 “复制”
begin
if Edit1.Text <>'' then
ClipBoard.SetTextBuf(Pchar(Edit1.Text));
end;
procedure TForm1.AboutClick(Sender: TObject);//菜单中的"关于本程序"
begin
ShellAbout(Form1.Handle,Pchar('黄志平精简计算器'),Pchar('由志平制作滴..'),HICON(Nil));
end;
procedure TForm1.PasteDataClick(Sender: TObject);//菜单中的 “粘贴”
var
TempStr,TempKey:ShortString;
i:Integer;
begin
if ClipBoard.AsText <>'' then
begin
Edit1.Text :='';
TempStr :=ClipBoard.AsText;
for i:=0 to Length(TempStr)-1 do
begin
TempKey:=Copy(TempStr,i,1);
if (TempKey='0') or (TempKey='1') or (TempKey='2') or (TempKey='3') or (TempKey='4') or (TempKey='5') or
(TempKey='6') or (TempKey='7') or (TempKey='8') or (TempKey='9') or (TempKey='.')then
Edit1.Text :=Edit1.Text +TempKey;
end;
end;
end;
procedure TForm1.Button_sqrClick(Sender: TObject); //求平方
var
a:Extended; //占10个字节的实数型
begin
if Edit1.Text <>'' then
begin
a:=StrToFloat(Edit1.Text);
Edit1.Text:=FloatToStr(Sqr(a)); //Sqr(a) 是平方函数,返回实数a的平方值
end;
end;
procedure TForm1.Button_NotDataClick(Sender: TObject); //取反
var
TempNotData:Extended;
begin
if Edit1.Text<>'' then
begin
TempNotData:=StrToFloat(Edit1.Text);
Edit1.Text:=IntToStr(Not(Trunc(TempNotData))+1);
end;
end;
procedure TForm1.Button_NothingClick(Sender: TObject); //清空
begin
Edit1.Text :='';
end;
procedure TForm1.Button_AddDataClick(Sender: TObject); //加法
begin
if Edit1.Text <> '' then
begin
if RealDataX <> 0 then
begin
RealDataY:= StrToFloat(Edit1.Text);
Edit1.Text :=FloatToStr(RealDataY + RealDataX);
RealDataY:=0;
RealDataX:=0;
end
else
begin
RealDataX:=StrToFloat(Edit1.Text);
Edit1.Text:='';
Edit1.SetFocus;
end;
DataType:=1;
end;
end;
procedure TForm1.Button_SubDataClick(Sender: TObject);//减法
begin
if Edit1.Text <> '' then
begin
if RealDataX <> 0 then
begin
RealDataY:= StrToFloat(Edit1.Text);
Edit1.Text :=FloatToStr(RealDataX - RealDataY);
RealDataY:=0;
RealDataX:=0;
end
else
begin
RealDataX:=StrToFloat(Edit1.Text);
Edit1.Text:='';
Edit1.SetFocus;
end;
DataType:=2;
end;
end;
procedure TForm1.Button_DivDataClick(Sender: TObject);//除法
begin
if Edit1.Text <> '' then
begin
if RealDataX <> 0 then
begin
RealDataY:= StrToFloat(Edit1.Text);
Edit1.Text :=FloatToStr(RealDataX / RealDataY);
RealDataY:=0;
RealDataX:=0;
end
else
begin
RealDataX:=StrToFloat(Edit1.Text);
Edit1.Text:='';
Edit1.SetFocus;
end;
DataType:=3;
end;
end;
procedure TForm1.Button_MulDataClick(Sender: TObject); //乘法
begin
if Edit1.Text <> '' then
begin
if RealDataX <> 0 then
begin
RealDataY:= StrToFloat(Edit1.Text);
Edit1.Text :=FloatToStr(RealDataX * RealDataY);
RealDataY:=0;
RealDataX:=0;
end
else
begin
RealDataX:=StrToFloat(Edit1.Text);
Edit1.Text:='';
Edit1.SetFocus;
end;
DataType:=4;
end;
end;
procedure TForm1.Button_CountDataClick(Sender: TObject);//计算
begin
if RealDataX <> 0 then
begin
RealDataY:= StrToFloat(Edit1.Text);
ifDataType=1 then
Edit1.Text :=FloatToStr(RealDataX + RealDataY);
ifDataType=2 then
Edit1.Text :=FloatToStr(RealDataX - RealDataY);
ifDataType=3 then
Edit1.Text :=FloatToStr(RealDataX / RealDataY);
ifDataType=4 then
Edit1.Text :=FloatToStr(RealDataX * RealDataY);
RealDataY:=0;
RealDataX:=0;
end
else
if ExDataX <> 0 then
begin
ExDataY:=StrToFloat(Edit1.text);
if DataType=5 then
Edit1.Text :=IntToStr(Trunc(ExDataX) and Trunc(ExDataY));
if DataType=6 then
Edit1.Text :=IntToStr(Trunc(ExDataX) Or Trunc(ExDataY));
if DataType=7 then
Edit1.Text :=IntToStr(Trunc(ExDataX) Xor Trunc(ExDataY));
if DataType=8 then
Edit1.Text :=IntToStr(Trunc(ExDataX) Shl Trunc(ExDataY));
if DataType=9 then
Edit1.Text :=IntToStr(Trunc(ExDataX) Shr Trunc(ExDataY));
ExDataX:=0;
ExDataY:=0;
end;
end;
procedure TForm1.Button_AndClick(Sender: TObject); //与
begin
if Edit1.Text <> '' then
begin
if ExDataX <> 0 then
begin
ExDataY:= StrToFloat(Edit1.Text);
Edit1.Text :=IntToStr(Trunc(ExDataX) and Trunc(ExDataY));
ExDataX:=0; {Trunc():返回浮点数据整数部分}
ExDataY:=0;
end
else
begin
ExDataX:=StrToFloat(Edit1.Text);
Edit1.Text:='';
Edit1.SetFocus;
end;
DataType:=5;
end;
end;
procedure TForm1.Button_OrClick(Sender: TObject); //或
begin
if Edit1.Text <> '' then
begin
if ExDataX <> 0 then
begin
ExDataY:= StrToFloat(Edit1.Text);
Edit1.Text :=IntToStr(Trunc(ExDataX) Or Trunc(ExDataY));
ExDataX:=0;
ExDataY:=0;
end
else
begin
ExDataX:=StrToFloat(Edit1.Text);
Edit1.Text:='';
Edit1.SetFocus;
end;
DataType:=6;
end;
end;
procedure TForm1.Button_XorClick(Sender: TObject); //异或
begin
if Edit1.Text <> '' then
begin
if ExDataX <> 0 then
begin
ExDataY:= StrToFloat(Edit1.Text);
Edit1.Text :=IntToStr(Trunc(ExDataX) Xor Trunc(ExDataY));
ExDataX:=0;
ExDataY:=0;
end
else
begin
ExDataX:=StrToFloat(Edit1.Text);
Edit1.Text:='';
Edit1.SetFocus;
end;
DataType:=7;
end;
end;
procedure TForm1.Button_ShlClick(Sender: TObject); //向左移位
begin
if Edit1.Text <> '' then
begin
if ExDataX <> 0 then
begin
ExDataY:= StrToFloat(Edit1.Text);
Edit1.Text :=IntToStr(Trunc(ExDataX) Shl Trunc(ExDataY));
ExDataX:=0;
ExDataY:=0;
end
else
begin
ExDataX:=StrToFloat(Edit1.Text);
Edit1.Text:='';
Edit1.SetFocus;
end;
DataType:=8;
end;
end;
procedure TForm1.Button_ShrClick(Sender: TObject); //向右移位
begin
if Edit1.Text <> '' then
begin
if ExDataX <> 0 then
begin
ExDataY:= StrToFloat(Edit1.Text);
Edit1.Text :=IntToStr(Trunc(ExDataX) Shr Trunc(ExDataY));
ExDataX:=0;
ExDataY:=0;
end
else
begin
ExDataX:=StrToFloat(Edit1.Text);
Edit1.Text:='';
Edit1.SetFocus;
end;
DataType:=9;
end;
end;
procedure TForm1.Button1Click(Sender: TObject); //百分号
var
ExDa:Extended;
begin
if Edit1.Text <>'' then
begin
ExDa:=StrToFloat(Edit1.Text);
Edit1.Text:=FloatToStr(ExDa / 100);
end;
end;
procedure TForm1.Button2Click(Sender: TObject); //指数函数
var
ExDa:Real;
begin
if Edit1.Text <>'' then
begin
ExDa:=StrToFloat(Edit1.Text);
Edit1.Text:=FloatToStr(Exp(ExDa));
end;
end;
end. 你不觉得你的求%那个有点小问题?出结果后再点数字的话,以前的输出会留着一起算结果。 写a+a a-a a*a a/a两元的都可以,一但写a+b*c完了,写不来,,,,还没有认真去看。
页:
[1]