表达式计算器
unit Unit_Main;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Controls, Forms,
Dialogs, Menus, StdCtrls, ComObj, ShellAPI,Clipbrd;
type
TForm_Main = class(TForm)
MainMenu1: TMainMenu;
N1: TMenuItem;
N2: TMenuItem;
N3: TMenuItem;
N5: TMenuItem;
N6: TMenuItem;
Memo1: TMemo;
Memo2: TMemo;
Label1: TLabel;
procedure N3Click(Sender: TObject);
procedure N5Click(Sender: TObject);
procedure N1Click(Sender: TObject);
procedure Memo1KeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
procedure N6Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form_Main: TForm_Main;
implementation
{$R *.dfm}
function jisuan(S: string): string;
var
vScript: Variant;
begin
vScript := CreateOleObject('ScriptControl');
vScript.Language := 'VBScript';
Result := vScript.Eval(S);
end;
procedure TForm_Main.N3Click(Sender: TObject);
begin //前端显示
N3.Checked := not N3.Checked;
if N3.Checked then
begin
Form_Main.FormStyle := fsStayOnTop;
end
else
begin
Form_Main.FormStyle := fsNormal;
end;
end;
procedure TForm_Main.N5Click(Sender: TObject);
begin //自动复制
N5.Checked := not N5.Checked;
if N5.Checked then
begin
N5.Caption := '复制结果';
end
else
begin
N5.Caption := '复制表达式';
end;
end;
procedure TForm_Main.N1Click(Sender: TObject);
var
S ,S2: String;
begin //计算
if Memo1.Text = '' then
begin
ShowMessage('请输入相关计算式。');
Exit;
end;
S := Memo1.Text ;
S2 := jisuan(S);
Memo1.Text := S+'='+S2;
Memo2.Lines.Add('---------------');
Memo2.Lines.Add(Memo1.Text);
if N5.Checked then
begin //如果自动复制结果
Clipboard.AsText := S2;
Memo2.Lines.Add('结果:'+Clipboard.AsText+' 已复制。');
end
else
begin //自动复制表达式和结果
Clipboard.AsText := Memo1.Text;
Memo2.Lines.Add('结果:'+Clipboard.AsText+' 已复制。');
end;
Memo1.Clear;
Memo1.Text :='';
end;
procedure TForm_Main.Memo1KeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
begin
if Key= 13 then
begin
Key := 0;
N1.Click;
end;
end;
procedure TForm_Main.N6Click(Sender: TObject);
begin //帮助
Application.MessageBox('表达式计算器,作者:圣诞老人'+#13#10+
'1. 输入如:(1+2)*3 可正确计算结果为:9而非7。'+#13#10+
'2. 回车键可直接计算结果。'+#13#10+
'3. 在第一个文本框内输入要计算的表达式,如:(1+2+3+4)*2后,'+#13#10+
' 按回车键即可计算并复制:(1+2+3+4)*2=20到剪切板中。','帮助',64);
end;
end.