|
发表于 2014-4-8 12:29:51
|
显示全部楼层
本帖最后由 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);
if DataType=1 then
Edit1.Text :=FloatToStr(RealDataX + RealDataY);
if DataType=2 then
Edit1.Text :=FloatToStr(RealDataX - RealDataY);
if DataType=3 then
Edit1.Text :=FloatToStr(RealDataX / RealDataY);
if DataType=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. |
-
|