如题,如果是第三方支持的,请给一个下载地址

解决方案 »

  1.   

    http://www.torry.net 搜索这个 Regular Expressions for Delphi v.0.942
      

  2.   

    凑了一上午,终于凑出一个差不多万能的表达式求值:
    楼主一定要给满一百分哦.
    //说明一个:下面例子在窗体上加两个Edit,一个Button,Edit1中为输入的表达式,支持所有运算符,包括"()",Edit2为输出结果
    unit compute;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls, Math;type
      TForm1 = class(TForm)
        Edit1: TEdit;
        Button1: TButton;
        Edit2: TEdit;
        procedure Button1Click(Sender: TObject);  private
        { Private declarations }
        s: string;
        function GetNumber(): real;
        function comp(a, b: char): char;
        function cal(a, b: real; p: char): real;
        function expr(): real;
      public
        { Public declarations }
      end;var
      Form1: TForm1;implementation{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject);
    var r: real;
    begin
      s := edit1.Text; //edit1中为要求值的表达式
      r := expr(); //  调用求值函数
      edit2.Text := floattostr(r); //将求值结果写到eidt2中
    end;function TForm1.cal(a, b: real; p: char): real;
    begin
      case p of
        '+': result := a + b;
        '-': result := a - b;
        '*': result := a * b;
        '/':
          begin
            if b = 0 then
            begin
              showmessage('除数为0');
              result := 0;
            end
            else
              result := a / b;
          end;
      end;
    end;function TForm1.comp(a, b: char): char;
    begin
      if not (a in ['+', '-', '*', '/', '(', ')', '#']) or not (b in ['+', '-', '*', '/', '(', ')', '#']) then result := 'e'
      else
        case a of
          '+', '-': case b of
              '*', '/', '(': result := '<';
            else result := '>';
            end;
          '*', '/': if b = '(' then result := '<' else result := '>';
          '(': if b = ')' then result := '=' else if b = '#' then result := 'e' else result := '<';
          ')': if b = '(' then result := 'e' else result := '>';
          '#': if b = ')' then result := 'e' else if b = '#' then result := 'R' else result := '<';
        end;
    end;
    function TForm1.expr(): real;
    var
      res, ch: char;
      oprTop, numTop: integer;
      opr: array[1..100] of char;
      num: array[1..100] of real;begin  opr[1] := '#';
      s := s + '#';
      oprTop := 1;
      numTop := 0;
      res := ' ';
      repeat
        if s[1] in ['0'..'9'] then
        begin
          inc(numTop);
          num[numTop] := GetNumber();
        end
        else
        begin
          ch := s[1];
          delete(s, 1, 1);
          repeat
            res := comp(opr[oprTop], ch);
            case res of
              '>': begin
                  num[numTop - 1] := cal(num[numTop - 1], num[numtop], opr[oprTop]);
                  dec(numTop);
                  dec(oprTop);
                end;
              '<': begin
                  inc(oprTop);
                  opr[oprTop] := ch;
                end;
              '=': begin
                  dec(oprTop);
                end;
              'e': begin
                  showmessage('表达式出错!');
                  result := 0;
                  exit;
                end;
              'R': begin
                  result := num[numTop];
                end;
            end;
          until res <> '>';
        end;
      until res = 'R';
    end;
    function TForm1.GetNumber(): real;
    var
      i, code: integer;
    begin
      i := 1;
      while (i <= length(s)) and (s[i] in ['0'..'9', '.']) do inc(i);
      val(copy(s, 1, i - 1), result, code);
      delete(s, 1, i - 1);
    end;
    end.