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

📄 unit1.pas

📁 演示表达式处理过程的源码
💻 PAS
字号:
unit Unit1;

interface

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

type
  TForm1 = class(TForm)
    Edit1: TEdit;
    ListBox1: TListBox;
    Button2: TButton;
    Edit2: TEdit;
    Button3: TButton;
    Edit3: TEdit;
    ListBox2: TListBox;
    ListBox3: TListBox;
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    Label4: TLabel;
    Label5: TLabel;
    Label6: TLabel;
    Edit4: TEdit;
    Label7: TLabel;
    Edit5: TEdit;
    Label8: TLabel;
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

uses Unit3;

{$R *.DFM}
var
  e1: expression;
  visual: array[1..100]of string;


procedure TForm1.Button1Click(Sender: TObject);
var
   i, a: integer;
   ss, ss2: string;
begin
     a:=length(edit1.text);
     ss2:='';
     for i:=1 to a do begin
//         ss:=copy(edit1.text, i, 1);
         ss:=edit1.text[i];
         ss2:=ss2+ss;
         listbox1.Items.Add(ss);
     end;
     listbox1.Items.Add(ss2);
end;

procedure TForm1.Button2Click(Sender: TObject);
var
   result: string;
begin
   e1:=expression.create;
   e1.Init_machine;
   e1.Set_string(edit1.text);
   e1.count_expression(result);
   edit2.text:=result;
   if result[1]<>'#' then begin
      button3.Enabled:=true;
      e1:=expression.create;
      e1.Init_machine;
      e1.Set_string(edit1.text);
      e1.show_prepare;
   end;
end;

procedure TForm1.Button3Click(Sender: TObject);
var
   result, a_word: string;
   i: integer;
begin
     e1.show_count(result);
     edit4.Text:=visual[e1.show_a];
     edit5.Text:=visual[strtoint(result)];
     edit3.Text:=copy(e1.e_string, 1, e1.e_ptr);
     if result='99' then begin
        button3.Enabled:=false;
     end;
     listbox1.Clear;
     listbox2.Clear;
     listbox3.Clear;
     for i:=e1.word_index downto 1 do begin
         a_word:=inttostr(i)+': '+visual[e1.opnds.get_type(i)]+' '+e1.opnds.get_value(i);
         if i<10 then a_word:='0'+a_word;
         listbox1.Items.Add(a_word);
     end;
     for i:=e1.opnd_s.s_top downto 1 do begin
         a_word:=inttostr(e1.opnd_s.data[i])+', '+e1.opnds.get_value(e1.opnd_s.data[i]);
         if e1.opnd_s.data[i]<10 then a_word:='0'+a_word;
         if i=e1.opnd_s.s_top then begin
            a_word:='→'+a_word;
         end else begin
            a_word:='  '+a_word;
         end;
         listbox2.Items.Add(a_word);
     end;
//     listbox2.Items.Add('******************');
     for i:=e1.optr_s.s_top downto 1 do begin
         a_word:=visual[e1.optr_s.data[i]];
         if i=e1.optr_s.s_top then begin
            a_word:='→'+a_word;
         end else begin
            a_word:='  '+a_word;
         end;
         listbox3.Items.Add(a_word);
     end;
//     listbox3.Items.Add('******************');
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
   visual[1]:='开始/结束';
   visual[11]:='整数';
   visual[12]:='小数';
   visual[21]:='加号';
   visual[22]:='减号';
   visual[23]:='乘号';
   visual[24]:='除号';
   visual[26]:='负号';
   visual[27]:='左括号';
   visual[28]:='右括号';
   visual[87]:='开始符入栈';
   visual[88]:='开始准备';
   visual[89]:='开始碰结束';
   visual[90]:='操作符比较';
   visual[91]:='开始读入';
   visual[92]:='读入下一个';
   visual[93]:='操作数入栈';
   visual[94]:='大于,入栈';
   visual[95]:='小于,语义';
   visual[96]:='等于,出栈';
   visual[97]:='继续';
   visual[98]:='判断类型';
   visual[99]:='结束';

end;

end.

⌨️ 快捷键说明

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