用一个按钮事件控制就行了:判断如果edit1(I1)...edit6(I6)不为空,则:
edit7.text ;= floattostr(strtofloat(edit1.text) + ....)

解决方案 »

  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 := 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 }
      

  2.   

    //demo
    procedure TForm1.Button1Click(Sender: TObject);
    begin
      Caption := Calc(Format('%s+%s*%s-(%s-%s/%s)',
        [Edit1.Text, Edit2.Text, Edit3.Text, Edit4.Text, Edit5.Text, Edit6.Text]));
    end;
      

  3.   

    TO zswang(伴水)(被黑中) 
    你的这段程序我试了,有些公式的计算结果不对呀
      

  4.   

    你把不能计算的公式Copy给我看看
      

  5.   

    那你试一下http://www.csdn.net/expert/topic/394/394903.shtm里面的这个公式?
      

  6.   

    (I1+I2)-I3*I4+I2输入I1=1,I2=2,I3=3,I4=4的计算结果是-11,应该是(1+2)-3*4+2=-7才对嘛
      

  7.   

    procedure TForm1.Button1Click(Sender: TObject);
    var
      S: string;
    begin
      S := Table1.FieldByName('计算公式').AsString;
      S := StringReplace(S, 'I1', Edit1.Text, [rfReplaceAll]);
      S := StringReplace(S, 'I2', Edit2.Text, [rfReplaceAll]);
      S := StringReplace(S, 'I3', Edit3.Text, [rfReplaceAll]);
      S := StringReplace(S, 'I4', Edit4.Text, [rfReplaceAll]);
      S := StringReplace(S, 'I5', Edit5.Text, [rfReplaceAll]);
      S := StringReplace(S, '(', '(', [rfReplaceAll]);
      S := StringReplace(S, ')', ')', [rfReplaceAll]);
      S := StringReplace(S, '+', '+', [rfReplaceAll]);
      S := StringReplace(S, '-', '-', [rfReplaceAll]);
      S := StringReplace(S, '*', '*', [rfReplaceAll]);
      S := StringReplace(S, '/', '/', [rfReplaceAll]);
      S := StringReplace(S, '×', '*', [rfReplaceAll]);
      S := StringReplace(S, '÷', '/', [rfReplaceAll]);
      Caption := Calc(S);
    end;
      

  8.   

    procedure TForm1.Button1Click(Sender: TObject);
    var
      S: string;
    begin
      S := Table1.FieldByName('计算公式').AsString;
      S := StringReplace(S, 'I1', Edit1.Text, [rfReplaceAll]);
      S := StringReplace(S, 'I2', Edit2.Text, [rfReplaceAll]);
      S := StringReplace(S, 'I3', Edit3.Text, [rfReplaceAll]);
      S := StringReplace(S, 'I4', Edit4.Text, [rfReplaceAll]);
      S := StringReplace(S, 'I5', Edit5.Text, [rfReplaceAll]);
      S := StringReplace(S, '(', '(', [rfReplaceAll]);
      S := StringReplace(S, ')', ')', [rfReplaceAll]);
      S := StringReplace(S, '+', '+', [rfReplaceAll]);
      S := StringReplace(S, '-', '-', [rfReplaceAll]);
      S := StringReplace(S, '*', '*', [rfReplaceAll]);
      S := StringReplace(S, '/', '/', [rfReplaceAll]);
      S := StringReplace(S, '×', '*', [rfReplaceAll]);
      S := StringReplace(S, '÷', '/', [rfReplaceAll]);
      Caption := Calc(S);
    end;
      

  9.   

    procedure TForm1.Button1Click(Sender: TObject);
    var
      S: string;
    begin
      S := Table1.FieldByName('计算公式').AsString;
      S := StringReplace(S, 'I1', Edit1.Text, [rfReplaceAll]);
      S := StringReplace(S, 'I2', Edit2.Text, [rfReplaceAll]);
      S := StringReplace(S, 'I3', Edit3.Text, [rfReplaceAll]);
      S := StringReplace(S, 'I4', Edit4.Text, [rfReplaceAll]);
      S := StringReplace(S, 'I5', Edit5.Text, [rfReplaceAll]);
      S := StringReplace(S, '(', '(', [rfReplaceAll]);
      S := StringReplace(S, ')', ')', [rfReplaceAll]);
      S := StringReplace(S, '+', '+', [rfReplaceAll]);
      S := StringReplace(S, '-', '-', [rfReplaceAll]);
      S := StringReplace(S, '*', '*', [rfReplaceAll]);
      S := StringReplace(S, '/', '/', [rfReplaceAll]);
      S := StringReplace(S, '×', '*', [rfReplaceAll]);
      S := StringReplace(S, '÷', '/', [rfReplaceAll]);
      Caption := Calc(S);
    end; 
      

  10.   

    const
      cCharCn: array[#32 .. #126] of string[2] =
    (
    { }' ',{!}'!',{"}'"',{#}'#',{ }'$',{%}'%',{&}'&',{'}''',{(}'(',
    {)}')',{*}'*',{+}'+',{,}',',{-}'-',{.}'。',{/}'/',{0}'0',{1}'1',
    {2}'2',{3}'3',{4}'4',{5}'5',{6}'6',{7}'7',{8}'8',{9}'9',{:}':',
    {;}';',{<}'<',{=}'=',{>}'>',{?}'?',{@}'@',{A}'A',{B}'B',{C}'C',
    {D}'D',{E}'E',{F}'F',{G}'G',{H}'H',{I}'I',{J}'J',{K}'K',{L}'L',
    {M}'M',{N}'N',{O}'O',{P}'P',{Q}'Q',{R}'R',{S}'S',{T}'T',{U}'U',
    {V}'V',{W}'W',{X}'X',{Y}'Y',{Z}'Z',{[}'[',{\}'\',{]}']',{^}'^',
    {_}'_',{`}'`',{a}'a',{b}'b',{c}'c',{d}'d',{e}'e',{f}'f',{g}'g',
    {h}'h',{i}'i',{j}'j',{k}'k',{l}'l',{m}'m',{n}'n',{o}'o',{p}'p',
    {q}'q',{r}'r',{s}'s',{t}'t',{u}'u',{v}'v',{w}'w',{x}'x',{y}'y',
    {z}'z',{{}'{',{&brvbar;}'|',{ }'}',{~}'~');  function CharToCharCn(mChar: Char): string;
    begin
      case mChar of
        #32 .. #126: Result := cCharCn[mChar];
      else Result := mChar;
      end;
    end; { CharToCharCn }function CharCnToChar(mCharCn: string): Char;
    var
      I: Char;
    begin
      Result := #0;
      for I := #32 to #126 do
        if cCharCn[I] = mCharCn then begin
          Result := I;
          Break;
        end;
    end; { CharCnToChar }function StrToStrCn(mStr: string): string;
    var
      I: Integer;
    begin
      Result := '';
      for I := 1 to Length(mStr) do
        Result := Result + CharToCharCn(mStr[I]);
    end; { StrToStrCn }function StrCnToStr(mText: string): string;
    var
      I: Integer;
      Temp: string;
      C: Char;
    begin
      Result := '';
      Temp := '';
      for I := 1 to Length(mText) do
        case ByteType(mText, I) of
          mbSingleByte: Result := Result + mText[I];
          mbLeadByte: Temp := Temp + mText[I];
          mbTrailByte: begin
            Temp := Temp + mText[I];
            C := CharCnToChar(Temp);
            if C <> #0 then
              Result := Result + C
            else Result := Result + Temp;
            Temp := '';
          end;
        end;
      Result := Result + Temp;
    end; { StrCnToStr }//..........
    begin
      Caption := Calc(StrCnToStr(S));
    end;
      

  11.   

    TO;zswang(伴水)(被黑中) 
    我试过了,计算结果错误不是全角半角的问题,好象是程序有问题,但我还没找到,你用我前面的数据和计算公式试一下,结果正确吗?
      

  12.   

    TO;zswang(伴水)(被黑中) 
    我试过了,计算结果错误不是全角半角的问题,好象是程序有问题,但我还没找到,你用我前面的数据和计算公式试一下,结果正确吗?
      

  13.   

    TO;zswang(伴水)(被黑中) 
    我试过了,计算结果错误不是全角半角的问题,好象是程序有问题,但我还没找到,你用我前面的数据和计算公式试一下,结果正确吗?
      

  14.   

    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 := 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 ['+', '-'])) 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 }
      

  15.   

    to : zswang(伴水)(被黑中) (
    能不能解释一下,为什么这么麻烦的判断?只要是数字,在象小新那样,把text里的值加起来不就可以了吗?可以给俺解释一下吗?谢谢
      

  16.   

    >你的第二段程序不行,我的计算公式是从数据库中提出的,是可变的,不能在程序中定死。 
    >你的第二段程序不行,我的计算公式是从数据库中提出的,是可变的,不能在程序中定死。 
    >你的第二段程序不行,我的计算公式是从数据库中提出的,是可变的,不能在程序中定死。 
    >你的第二段程序不行,我的计算公式是从数据库中提出的,是可变的,不能在程序中定死。 
    >你的第二段程序不行,我的计算公式是从数据库中提出的,是可变的,不能在程序中定死。 
    //                  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~