evmain.pas

来自「表达式计算DEMO 利用词法分析器原理 以及后缀表达式计算 暂不支持函」· PAS 代码 · 共 86 行

PAS
86
字号
unit EvMain;

interface

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

type
  TForm1 = class(TForm)
    Label1: TLabel;
    Edit1: TEdit;
    Button1: TButton;
    Label2: TLabel;
    ListBox1: TListBox;
    Label3: TLabel;
    Edit2: TEdit;
    Label4: TLabel;
    Edit3: TEdit;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
    procedure ListToken(InList: TList);
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

{$R WindowsXP.res}

uses
  EvFunction;

procedure TForm1.ListToken(InList: TList);
var
   I: Integer;
begin
     ListBox1.Clear;
     For I := 0 to InList.Count - 1 do
         Case PToken(InList.Items[I])^.Token of
           tkNumber: ListBox1.Items.Add('tkNumber');
           tkAdd: ListBox1.Items.Add('tkAdd');
           tkSub: ListBox1.Items.Add('tkSub');
           tkMul: ListBox1.Items.Add('tkMul');
           tkDiv: ListBox1.Items.Add('tkDiv');
           tkLBracket: ListBox1.Items.Add('tkLBracket');
           tkRBracket: ListBox1.Items.Add('tkRBracket');
         end;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
   InList: TList;
   sSuffix: String;
   ErrCode: Integer;
begin
     if Trim(Edit1.Text) = '' then Exit;
     InList := TList.Create;
     ErrCode := ParseExpression(Edit1.Text,
                                InList);
     if ErrCode > 0 then
        begin
             MessageBox(Handle,
                        'Input Error!',
                        'Error',
                        MB_ICONERROR);
             Edit1.SetFocus;
             Edit1.SelStart := ErrCode - 1;
             Edit1.SelLength := 1;
             Exit;
        end;
     ListToken(InList);
     sSuffix := InsideToSuffix(InList);
     Edit2.Text := sSuffix;
     Edit3.Text := FloatToStr(Evaluate(sSuffix));
     InList.Free;
end;

end.

⌨️ 快捷键说明

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