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

📄 unit1.pas

📁 该程序把Excel公式分解为Token序列。
💻 PAS
字号:
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls,  Grids, ExtCtrls, ExcelParser, ComCtrls,contnrs;

type
  TForm1 = class(TForm)
    StringGrid1: TStringGrid;
    Panel1: TPanel;
    btnParse: TButton;
    ComboBox1: TComboBox;
    TreeView1: TTreeView;
    procedure btnParseClick(Sender: TObject);
  private
    procedure parseFormula(formula: string);
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.btnParseClick(Sender: TObject);
begin
  parseFormula(ComboBox1.Text);
end;


procedure TForm1.parseFormula(formula: string);
  function indent(indentCount: integer): string;
  var
    s: string;
    i: integer;
  begin
    s := '|';
    for i := 0 to indentCount - 1 do begin
      s := s + ' |';
    end;
    Result := s;
  end;
var
  indentcount: integer;
  tokens: TTokens;
  token: TToken;
  ARow: integer;
  MyStack:TStack;
  PNode,CNode: TTreeNode;
begin
  tokens := getTokens(formula);


  tokens.Reset;
  TreeView1.Items.Clear;
  MyStack:= TStack.Create;
  indentcount := 0;
  
  StringGrid1.Cells[1, 0] := 'index';
  StringGrid1.Cells[2, 0] := 'Type';
  StringGrid1.Cells[3, 0] := 'subtype';
  StringGrid1.Cells[4, 0] := 'value';
  StringGrid1.Cells[5, 0] := 'Token tree';
  for Arow := 1 to StringGrid1.RowCount-1 do
  begin
    StringGrid1.Cells[1, Arow] := '';
    StringGrid1.Cells[2, Arow] := '';
    StringGrid1.Cells[3, Arow] := '';
    StringGrid1.Cells[4, Arow] := '';
    StringGrid1.Cells[5, Arow] := '';
  end;

  StringGrid1.RowCount:= tokens.Count+1;
  ARow := 0;

  PNode:=Nil;
  MyStack.Push(PNode);


  while (tokens.moveNext) do begin

    token := tokens.current;

    if (token.subtype = TOK_SUBTYPE_STOP) then
      if indentCount > 0 then begin
        indentCount := indentCount - 1;
        PNode:= MyStack.pop;
        PNode:= MyStack.Peek;
      end;
      
    Arow := ARow + 1;
    StringGrid1.Cells[1, ARow] := inttostr(tokens.index + 1);
    StringGrid1.Cells[2, ARow] := token.TokenType;
    StringGrid1.Cells[3, ARow] := token.subtype;
    StringGrid1.Cells[4, ARow] := token.value;
    StringGrid1.Cells[5, ARow] := indent(indentCount) + token.value;
    if (token.subtype <> TOK_SUBTYPE_STOP) then
    CNode:= TreeView1.Items.AddChildObject(PNode,token.value,token);

    if (token.subtype = TOK_SUBTYPE_START) then begin
      indentCount := indentCount + 1;
      MyStack.Push(CNode);
      PNode:= CNode;
    end;

  end;

  tokens.Free;
  token.Free;
end;


end.

⌨️ 快捷键说明

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