请教表达式计算的问题:已知一个表达式,表达式中出现的运算符只有+,-,*,/,
(,)六种,现在急需一个计算表达式的程序。
比如输入表达式:(a+b)*c+d,要求计算出结果。其中a,b,c,d值均已知。我知道这要用到数据结构中关于栈的应用(表达式求值)技术,但是苦于对此不是太了解,恳请高手能够给予我帮助,最好能够有调试好的源程序,小弟将不胜感激!!

解决方案 »

  1.   

    在Delphi中安装Microsoft Script Control 1.0的ActiveX控件,添加到窗体上,用以下的代码就行了:
    ScriptControl1.Language:='vbscript';
    Edit1.Text:=ScriptControl1.Eval('10+20*(3-5)/6+(4+(3+5)*6)');
      

  2.   

    标题:计算表达式
    说明:加、减、乘、除及括号;请大家多多测试
    设计:Zswang
    日期:2002-01-26
    支持:[email protected] 
    ///////Begin Source
    uses
      Math;
     
    procedure Bracket(mText: string; var nLStr, nCStr, nRStr: string);
    var
      L, R: Integer;
      I: Integer;
      B: Boolean;
    begin
      nLStr := '';
      nCStr := '';
      nRStr := '';
      B := True;
      L := 0;
      R := 0;
      for I := 1 to Length(mText) do
        if B then begin
          if mText[I] = '(' then
            Inc(L)
          else if mText[I] = ')' then
            Inc(R);
          if L = 0 then
            nLStr := nLStr + mText[I]
          else if L > R then
            nCStr := nCStr + mText[I]
          else B := False;
        end else nRStr := nRStr + mText[I];
      Delete(nCStr, 1, 1);
    end; { Bracket }
     
    function Calc(mText: string): string;
    var
      vText: string;
     
      function fCalc(mText: string): string;
      var
        vLStr, vCStr, vRStr: string;
        I, J, K, L: Integer;
      begin
        L := Length(mText);
        if Pos('(', mText) > 0 then begin
          Bracket(mText, vLStr, vCStr, vRStr);
          Result := fCalc(vLStr + fCalc(vCStr) + vRStr);
        end else if (Pos('+', mText) > 0) or (Pos('-', mText) > 0) then begin
          I := Pos('+', mText);
          J := Pos('-', mText);
          if I = 0 then I := L;
          if J = 0 then J := L;
          K := Min(I, J);
          vLStr := Copy(mText, 1, Pred(K));
          vRStr := Copy(mText, Succ(K), L);
          if vLStr = '' then vLStr := '0';
          if vRStr = '' then vRStr := '0';
          if I = K then
            Result := FloatToStr(StrToFloat(fCalc(vLStr)) + StrToFloat(fCalc(vRStr)))
          else Result := FloatToStr(StrToFloat(fCalc(vLStr)) - StrToFloat(fCalc(vRStr)))
        end else if (Pos('*', mText) > 0) or (Pos('/', mText) > 0) then begin
          I := Pos('*', mText);
          J := Pos('/', mText);
          if I = 0 then I := L;
          if J = 0 then J := L;
          K := Min(I, J);
          vLStr := Copy(mText, 1, Pred(K));
          vRStr := Copy(mText, Succ(K), L);
          if vLStr = '' then vLStr := '0';
          if vRStr = '' then vRStr := '0';
          if I = K then
            Result := FloatToStr(StrToFloat(fCalc(vLStr)) * StrToFloat(fCalc(vRStr)))
          else Result := FloatToStr(StrToFloat(fCalc(vLStr)) / StrToFloat(fCalc(vRStr)))
        end else if Pos('_', mText) = 1 then
          Result := FloatToStr(-StrToFloat(fCalc(Copy(mText, 2, L))))
        else Result := FloatToStr(StrToFloat(mText));
      end;
    var
      I, L: Integer;
    begin
      vText := '';
      L := Length(mText);
      for I := 1 to L do
        if (mText[I] = '-') and (I < L) and (not (mText[Succ(I)] in ['+', '-', '(', ')'])) then
          if (I = 1) or ((I > 1) and (mText[Pred(I)] in ['*', '/'])) then
            vText := vText + '_'
          else if ((I > 1) and (mText[Pred(I)] in ['+', '-'])) or
            ((I < L) and (not (mText[Succ(I)] in ['+', '-', '(', ')']))) then
            vText := vText + '+_'
          else vText := vText + mText[I]
        else vText := vText + mText[I];
      Result := fCalc(vText);
    end; { Calc }
    ///////End Source
     
    ///////Begin Demo
    procedure TForm1.Button1Click(Sender: TObject);
    begin
      Edit1.Text := Calc(Edit2.Text);
    end;
    ///////End Demo
      

  3.   

    如果你的表达式中只有+,-,*,/,(,)的话你可以采用sql语句得到结果:
    比如你的表达式为:(1+2)*3,那么你如下写:
    select distinct (1+2)*3 from aa3
    得到的就是你想要的结果
      

  4.   

    T0 supercdz(烟鬼,一天两包半)十分感谢,你的代码我调试过了,是可以的.
    不知道还有没有比较精练算法,如果有那就太好了