如题:高分求 字符串转计算表达式,比如:
   ((123+112)*12+21*(4562-60))*3/7 + 50

解决方案 »

  1.   

    uses
        ComObj.....
    function Eval(expStr: string): string;
    var
        js: OleVariant;
    begin
        js := CreateOleObject('ScriptControl');
        js.Language := 'JavaScript';
        Result := js.Eval(expStr);
        js := Unassigned;
    end;Eval('((123+112)*12+21*(4562-60))*3/7 + 50')=41776.5714285714给分吧.哈哈.
      

  2.   

    用数据库可以实现
    比如Edit1.text是'((123+112)*12+21*(4562-60))*3/7 + 50',then
    adoquery.sql.clear;
    adoquery.sql.add('select '+edit1.text);
    adoquery.sql.open;
    edit2.text:=inttostr(adoquery.filelds[0].asinteger);
    大体思路是这样,代码可能有不规范的地方,楼主试试吧,肯定行
      

  3.   

    楼上的方法不行,
    adoquery.sql.open;编译都不能通过
      

  4.   

    问题已经解决在csdn上找的,贴出来大家共享
    uses
      Math,Sysutils;  procedure Bracket(mText: string; var nLStr, nCStr, nRStr: string);
      function Calc(mText: string): string;implementationprocedure 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 := Calc(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 ['+', '-']) then
            vText := vText + '+_'
          else vText := vText + mText[I]
        else vText := vText + mText[I];
      Result := fCalc(vText);
    end; { Calc }
    end.
      

  5.   

    sorry,多了个sql,应该是adoquery1.open,我自己写的,也没测
      

  6.   

    hellolongbin(一个人)一直想写的是下面的句子把
    ADOQuery1.ExecSQL
    不知道行不行啊,不过hlb1111(蜀狼)的例子现收藏了
      

  7.   

    >>如题:高分求 字符串转计算表达式,比如:
    >>   ((123+112)*12+21*(4562-60))*3/7 + 50無非是求表達式的結果, 其實 netcrawller(放弃绑定-不用第三方控件)和
     hellolongbin(一个人) 兩位就實現了
      

  8.   

    人家贴出来的是底层解决的算法,在pascal里那样位的就不成咯。
      

  9.   

    正规军用逆波兰式解决问题,向我这样的杂牌军就是喜欢netcrawller(放弃绑定-不用第三方控件) 和hellolongbin(一个人) 的办法,严重的崇拜!希望二位加我的QQ 12416204 !!
      

  10.   

    netcrawller(放弃绑定-不用第三方控件) 和hellolongbin(一个人) 的方法确实能实现,但第一次气动较慢,感谢各位的参与。
      揭帖
      

  11.   

    to: heluqing 
    不是ExecSQL,就是Open,因为是select操作