//from
http://www.csdn.net/Expert/topic/418/418277.shtm

解决方案 »

  1.   

    //完整代码
    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 > 1) and (mText[Pred(I)] = ')') and (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 }
      

  2.   

    to zzllabc(龙): 计算表达式
      

  3.   

    to zswang(伴水)(被黑中) 
    为什么你回答刚才那个人的计算要这么 复杂?直接把edit1到edit6里的数值加起来不就可以了吗?可以说说为什么???
      

  4.   

    to urchinjj(懒刺猬):公式是动态的,没有固定
      

  5.   

    to cobi(我是小新):你如果再养成良好开发风格就完美了
      

  6.   

    回复人: zswang(伴水)(被黑中) (2001-12-13 11:32:06)  得0分 
    to urchinjj(懒刺猬):公式是动态的,没有固定  
    什么公式是动态的?能不能具体解释一下?详细一点可以?俺不明白,什么里有公式?公式在哪里?不是把edit里的数值加起来吗??请解释??
      

  7.   

    to Sprikg(网络游魂):非常谢谢
      

  8.   

    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 }//请各位有空再测测
      

  9.   

    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 }//请各位有空再测测
      

  10.   

    喂,为什么不详细解释一下?什么公式是动态的?能不能具体解释一下?详细一点可以?俺不明白,什么里有公式?公式在哪里?不是把edit里的数值加起来吗??请解释?? 
      

  11.   

    (I1+I2)-I3*I4+I5
    (I1-I2)*I3/I4+I5
    (I1+I2)+I3/I4+I5
    (I1+I2)-I3*(I4+I5)
    (I1-I2)-I3*(I4-I5)
      

  12.   

    procedure TForm1.Button1Click(Sender: TObject);
    var
      I: Integer;
    begin
      for I := 0 to Memo1.Lines.Count - 1 do begin
        ADOQuery1.SQL.Text := Format('SELECT %s AS 计算', [Memo1.Lines[I]]);
        ADOQuery1.Open;
        Memo1.Lines[I] := Memo1.Lines[I] + '=' + Calc(Memo1.Lines[I]) + '//' +
          ADOQuery1.FieldByName('计算').AsString;
        ADOQuery1.Close;
      end;
    end;
      

  13.   

    不是故意帮你up的,只是告诉你,以后不在麻烦问你,,,大侠,,,bless
      

  14.   

    to urchinjj(懒刺猬):怎么了,我没有说清楚吗?
    他的公式还可能是
    (I1+I2)*I4+I5
    (I1-I2)*I3/I4+I5
    (I1+I2)-I3*(I4+I5)+I3*(I4+I5)
    I1-I2
      

  15.   

    to : zswang(伴水)(被黑中) 他的公式还可能是
    (I1+I2)*I4+I5
    (I1-I2)*I3/I4+I5
    (I1+I2)-I3*(I4+I5)+I3*(I4+I5)
    I1-I2 我知道公式变化可以不同,我是想问,这些公式在哪里?是在edit1---edit6的text里吗?因为我认为text里的是数值,,我想问,什么叫计算公式?闲俺问的问题低级啊?那俺问问什么叫 词法分析可以吗?哼
      

  16.   

    动态从数据库提取公式//这些公式在哪里?
    应用在人事工资等领域
    编辑框里保存的是变量的数值//是在edit1---edit6的text里吗?
    I1对应Edit1.Text
    I2对应Edit2.Text
    I3对应Edit3.Text
    I4对应Edit5.Text
    I5对应Edit6.Text
    没有具体数值只有变量的表达式//什么叫计算公式?
    不是低级,只是要说清楚不太好表达//闲俺问的问题低级啊?
    可以,但没有词法分析这么复杂//那俺问问什么叫 词法分析可以吗?嘻嘻嘻嘻嘻嘻嘻嘻嘻嘻
    嘻嘻嘻笑一笑嘻嘻嘻嘻
    嘻嘻嘻嘻嘻嘻嘻嘻嘻嘻哼 √
      

  17.   

    俺俺俺俺俺俺俺俺俺俺俺俺俺俺俺俺
    俺俺俺俺俺俺明白了俺俺俺俺俺俺俺
    俺俺俺俺俺俺俺俺俺俺俺俺俺俺俺俺//shy
    //blush
    俺在写作业那,,嘿嘿