⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 unit2.pas

📁 设计模式delphi版给想学delphi的朋友一个很有价值的参考
💻 PAS
字号:
unit Unit2;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls;

type
  TForm2 = class(TForm)
    Button1: TButton;
    Label1: TLabel;
    Button2: TButton;
    Label2: TLabel;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form2: TForm2;

implementation

uses InterpreterUnit2;

{$R *.DFM}

procedure TForm2.Button1Click(Sender: TObject);
var
  expression: TBooleanExp;
  NowContext: TContext;
  x, y: TVariableExp;
  trueConst: TConstantExp;
//  orExp1: TOrExp;
  andExp1, andExp2: TAndExp;
  notExp1: TNotExp;
begin
  NowContext := TContext.Create;

  x := TVariableExp.Create('X');
  y := TVariableExp.Create('Y');
  trueConst := TConstantExp.Create(true);
  notExp1 := TNotExp.Create(x);
  andExp1 := TAndExp.Create(trueConst, x);
  andExp2 := TAndExp.Create(y, notExp1);
  expression := TOrExp.Create(andExp1, andExp2);
//  orExp1:=TOrExp.Create(andExp1,andExp2);

  try
    NowContext.Assign(x, false);
    NowContext.Assign(y, true);

    if expression.Evaluate(NowContext) then
//  if TOrExp(expression).Evaluate(NowContext) then
      Label1.Caption := 'The Result is true.'
    else
      Label1.Caption := 'The Result is false.';

  finally
    NowContext.Free;
    x.Free;
    y.Free;
    trueConst.Free;
    notExp1.Free;
    andExp1.Free;
    andExp2.Free;
    expression.Free;
  end;
end;

procedure TForm2.Button2Click(Sender: TObject);
var
  expression: TBooleanExp;
  NowContext: TContext;
  x, y: TVariableExp;
  trueConst: TConstantExp;
  andExp1, andExp2: TAndExp;
  notExp1: TNotExp;

  z: TVariableExp;
  not_z: TNotExp;
  replacement: TBooleanExp;
begin
  NowContext := TContext.Create;

  x := TVariableExp.Create('X');
  y := TVariableExp.Create('Y');
  trueConst := TConstantExp.Create(true);
  notExp1 := TNotExp.Create(x);
  andExp1 := TAndExp.Create(trueConst, x);
  andExp2 := TAndExp.Create(y, notExp1);
  expression := TOrExp.Create(andExp1, andExp2);

  z := TVariableExp.Create('Z');
  not_z := TNotExp.Create(z);
  replacement := TBooleanExp.Create;
  replacement := expression.Replace('Y', not_z);

  try
    NowContext.Assign(x, false);
    NowContext.Assign(y, true);
    NowContext.Assign(z, true);

    if replacement.Evaluate(NowContext) then
      Label2.Caption := 'The Result is true.'
    else
      Label2.Caption := 'The Result is false.';

  finally
    NowContext.Free;
    x.Free;
    y.Free;
    trueConst.Free;
    notExp1.Free;
    andExp1.Free;
    andExp2.Free;
    expression.Free;

    z.Free;
    not_z.Free;
    replacement.Free;
  end;

end;

end.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -