delphi中如何实现表达式计算?
即我想在一个Edit1中输入:1+(2*3-4/2)*8之类表达式,
我怎么计算其结果?
如果表达式中带变量呢,如:1+(2*A-4/B)*C 怎么做?谢谢!

解决方案 »

  1.   

    用Pascal Script™ 3.0 控件
    http://www.remobjects.com/page.asp?id={9A30A672-62C8-4131-BA89-EEBBE7E302E6}
      

  2.   

    如果自已写一个,也是很繁的(以前在C/C++版有人写过,用栈实现)
    如果不想研究编译原理,还是建议用控件(D版我也见有人问过,不过好像没有人完整写出来过)
      

  3.   

    另外,有国人写过一个,你也可试试:
    http://www.delphiun.com/down_view.asp?id=3059
      

  4.   

    不好意思,上面的要注册,这里可以下的
    http://www.tomore.com/1/35356.html
      

  5.   

    谢谢你了,在PB中,可以用数据窗口的一个函数实现,我想在delphi中是不是有这样的方法,可以自己用不太麻烦的代码实现。
    如果没有,我就用这个了。
    外国那个不用注册吧。我下了,安装了,还没研究明白怎么用。
      

  6.   

    你也可以用微软的一个scriptcontrol的activex,不过用控件也没关系,控件复杂用起来并不复杂
      

  7.   

    看来最简单的是用用Pascal Script™ 3.0 控件
    但谁能告诉我,这样的控件怎么用?
    才能计算出1+(2*3-4/2)*8之类表达式或带变量的表达式?
      

  8.   

    简单的表达式构件(不含变量.可用+-*/^())非常实用
    如果要支持变量,就一定是script了,用我上面的,里面有demo(不过很复杂)
    unit cmpEvaluator;// Copyright (c) Colin Wilson 1997.  All rights reserved.
    // E-mail to [email protected]
      Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs;type
      TExpressionEvaluator = class(TComponent)
      private
        fExpression : string;    pos : Integer;
        ch : char;    function GetChar : char;
        function GetNonWhitespace : char;
        procedure SkipWhitespace;    function CalcAddition : extended;
        function CalcMultiplication : extended;
        function CalcPower : extended;
        function CalcSignedTerm : extended;
        function CalcTerm : extended;
        function CalcNumber : extended;
        function CalcExpressionInBrackets : extended;  protected
      public
        function Calculate : extended;
        property Position : Integer read pos;
      published
        property Expression : string read fExpression write fExpression;
      end;procedure Register;implementationfunction TExpressionEvaluator.GetChar : char;
    begin
      if pos < Length (Expression) then
      begin
        ch := Expression [pos + 1];
        Inc (pos)
      end
      else ch := #0;
      result := ch;
    end;function TExpressionEvaluator.GetNonWhitespace : char;
    begin
      GetChar;
      SkipWhitespace;
      result := ch;
    end;procedure TExpressionEvaluator.SkipWhitespace;
    begin
      if ch in [' ', #9] then
      repeat
        GetChar
      until not (ch in [' ', #9])
    end;function TExpressionEvaluator.CalcAddition : extended;
    begin
      result := CalcMultiplication;
      while True do
        case ch of
          '+' : result := result + CalcMultiplication;
          '-' : result := result - CalcMultiplication;
          else break
        end
    end;function TExpressionEvaluator.CalcMultiplication : extended;
    begin
      result := CalcPower;
      while True do
        case ch of
          '*' : result := result * CalcPower;
          '/' : result := result / CalcPower;
          else break
        end
    end;function TExpressionEvaluator.CalcPower : extended;
    begin
      result := CalcSignedTerm;
      while True do
        case ch of
          '^' : result := exp (ln (result) * CalcSignedTerm);
          else break
        end
    end;function TExpressionEvaluator.CalcSignedTerm : extended;
    begin
      case GetNonWhitespace of
        '+' : result := CalcSignedTerm;
        '-' : result := -CalcSignedTerm;
        else result := CalcTerm
      end
    end;function TExpressionEvaluator.CalcTerm : extended;
    begin
      case ch of
        '0'..'9' : result := CalcNumber;
        '(' : result := CalcExpressionInBrackets;
        else
          raise Exception.CreateFmt ('Syntax error in expression at position %d', [pos + 1])
      end
    end;function TExpressionEvaluator.CalcNumber : extended;
    var
       s : string;
    begin
      result := 0;
      s := '';
      while ch in ['0'..'9'] do
      begin
        s := s + ch;
        GetChar;
        if ch = '.' then
        begin
          repeat
            s := s + ch;
            GetChar
          until not (ch in ['0'..'9']);
          break
        end
      end;
      result := StrToFloat (s);
      SkipWhitespace
    end;function TExpressionEvaluator.CalcExpressionInBrackets : extended;
    begin
      result := CalcAddition;
      if ch = ')' then
        GetNonWhitespace
      else
        raise Exception.CreateFmt ('Mismatched parentheses at position %d', [pos + 1])
    end;function TExpressionEvaluator.Calculate : extended;
    begin
      pos := 0;
      result := CalcAddition;
      if ch <> #0 then
        raise Exception.CreateFmt ('Unexpected end of expression at position %d', [pos + 1]);
    end;procedure Register;
    begin
      RegisterComponents('Samples', [TExpressionEvaluator]);
    end;end.
      

  9.   

    我的是要有变量的,如果再在程序中查找替换,也很仍很麻烦,看来只能用Scripe的了,但是还没明白怎么用。
    谢谢