有一个字符串 '200000000000000000000000000001'  为30位我想使它加1 得到 '200000000000000000000000000002' .使它减1 得到 '200000000000000000000000000002' .
用strtoint 位数数不够,请问该如何实现!!!

解决方案 »

  1.   

    试试看,仅针对1位数相加减var
      v, k :Integer;k := 1;  //初始要加的数
    for i:= length(s) downto 1 
    begin
      v := StrToInt(s[i]) + k;
      if v > 10 then
      begin
        k = v div 10;
        s[i] := IntToStr(v mod 10);
      end;
    end;减法可以参考写吧
      

  2.   

    试试看,仅针对1位数相加减var
      v, k :Integer;k := 1;  //初始要加的数
    for i:= length(s) downto 1 
    begin
      v := StrToInt(s[i]) + k;
      if v > 10 then
      begin
        k = v div 10;
        s[i] := IntToStr(v mod 10);
      end else
      begin
        s[i] := IntToStr(v);
        break;  //忘了退出循环了
      end;
    end;减法可以参考写吧
      

  3.   

    索性减法也写了var
      v, k :Integer;k := 1;  //初始要加的数
    for i:= length(s) downto 1 
    begin
      v := StrToInt(s[i]) - k;
      if v < 0 then
      begin
        k = 1;
        s[i] := IntToStr(v + 10);
      end else
      begin
        s[i] := IntToStr(v);
        break;  //忘了退出循环了
      end;
    end;
      

  4.   

    晕,刚才试了一下,
    int64也放不下这么大的数自己写函数吧
      

  5.   

    字符串运算的函数
    function StringAdd(const Value, Minus: string): string;
    var
      Digit: string;
      LenV, LenM, MaxDigits, I, DigitV, DigitM: Integer;
      R: Byte;           // Remainder
      CV, CM: Char;      // char from string A, string B
      V, M: string;
    begin
      V := Value;
      M := Minus;
      if (V = '') or (V = '0') then
      begin
        Result := M;
        Exit;
      end;
      if (M = '') or (M = '0') then
      begin
        Result := V;
        Exit;
      end;
      if V[1]='-' then
      begin
        Delete(V, 1, 1);
        result := StringMultiply(StringSubtract(V, M), '-1');
        exit;
      end;
      if M[1]='-' then
      begin
        Delete(M, 1, 1);
        result := StringSubtract(V, M);
        exit;
      end;
      R := 0;
      LenV := Length(V);
      LenM := Length(M);
      MaxDigits := Max(LenV, LenM);
      Result := '';
      for I := 0 to MaxDigits - 1 do
      begin
        DigitV := LenV - I;
        DigitM := LenM - I;
        if I < LenV then CV := V[DigitV] else CV := '0';
        if I < LenM then CM := M[DigitM] else CM := '0';
        Digit := AddChars(CV, CM, R);
        Result := Digit + Result;
      end;
      if R > 0 then
        Result := '1' + Result;
    end;
      

  6.   

    如果只是加减法的,自己抓string 写个很快的哦
      

  7.   

    LocustWei(LocustWei) 大哥,你给的还缺少以下几个函数啊!!StringMultiply
    StringSubtract
    AddChars本人刚学习 delphi ,不是太熟悉,请高手帮忙给个程序好吗??
      

  8.   

    谁有 delphi 大整数运算库程序啊(用字符串实现加减乘除),发个给我好吗? 先谢谢了!!
      

  9.   

    function StringMultiply(const Left, Right: string): string;  function InternalMultiply(const A, B: string): string;
      begin
        if BlankArgument(A) or BlankArgument(B) then
          Result := '0'
        else if CanUseShort(A, B, 9) then
          Result := InternalShortMultiply(A,B)
        else
          Result := InternalLongMultiply(A,B);
      end;
    var
      LLeft, LRight: string;
      NegCount: Integer;
    begin
      if (Left='') or (Left='0') or (Right='') or (Right='0') then
      begin
        result := '0';
        exit;
      end;
      NegCount := 0;  if Left[1] = '-' then
      begin
        Inc(NegCount);
        LLeft := Copy(Left, 2, Length(Left) - 1);
      end
      else
        LLeft := Left;  if Right[1] = '-' then
      begin
        Inc(NegCount);
        LRight := Copy(Right, 2, Length(Right) - 1);
      end
      else
        LRight := Right;  Result := InternalMultiply(LLeft, LRight);  if (NegCount mod 2) <> 0 then
        if (result <>'0') and (result<>'') then
          result := '-'+result;end;
      

  10.   

    function StringSubtract(const Value, Minus: string): string;
      function SubtractChars(V, M: Char; var R: Byte): string;
      var
        Value: Byte;
        //I: Integer;
      begin
        Value := Byte((Ord(V)-48) - ((Ord(M)-48) + R));
        if Value > 9 then  // Byte is unsigned: values will be between 246-255
        begin
          Result := Char(Byte(10+Value) + 48);
          R := Byte(1);
        end else
        begin
          Result :=  Char(Byte(Value + 48));
          R := Byte(0);
        end;
      end;
    var
      Digit, V, M: string;
      LenV, LenM, MaxDigits, I, DigitV, DigitM: Integer;
      R: Byte;           // Remainder
      CV, CM: Char;      // char from string A, string B
      rs: string;
    begin
      V := Value;
      M := Minus;  if (V = '') or (M = '') then
      begin
        if V = '' then
        begin
          Result := StringMultiply(M, '-1');
        end
        else
        begin
          Result := V;
        end;
        exit;
      end;
      if (V = '0') or (M = '0') then
      begin
        if M = '0' then
          Result := V
        else
          Result := StringMultiply(M, '-1');
        Exit;
      end;  if V[1]='-' then
      begin
        Delete(V, 1, 1);
        result := StringMultiply(StringAdd(V, M), '-1');
        exit;
      end;
      if M[1]='-' then
      begin
        Delete(M, 1, 1);
        result := StringAdd(V, M);
        exit;
      end;  if CompareDigits(Value, Minus) >= 0 then
      begin
        V := Value;
        M := Minus;
        rs := '';
      end else
      begin
        M := Value;
        V := Minus;
        rs := '-';
      end;  R := 0;
      LenV := Length(V);
      LenM := Length(M);
      MaxDigits := Max(LenV, LenM);
      Result := '';
      for I := 0 to MaxDigits - 1 do
      begin
        DigitV := LenV - I;
        DigitM := LenM - I;
        if I < LenV then
          CV := V[DigitV]
        else
          CV := '0';
        if I < LenM then
          CM := M[DigitM]
        else
          CM := '0';
        Digit := SubtractChars(CV, CM, R);
        Result := Digit + Result;
      end;
      result := LeftTrim(result);
      result := rs + result;
      if Result = '' then
        Result := '0';
    end;
      

  11.   

    function AddChars(V, M: Char; var R: Byte): string;
    var
      Value: Byte;
    begin
      Value := Byte((Ord(V)-48) + (Ord(M)-48) + R);
      if Value > 9 then
      begin
        Result := Char(Byte(Value - 10) + 48);
        R := Byte(1);
      end else
      begin
        Result :=  Char(Byte(Value + 48));
        R := Byte(0);
      end;
      if Result = '' then Result := '0';
    end;
      

  12.   

    To: qybao(阿宝) ( ) 信誉:100  2005-08-25 12:38:00  得分: 0  
     
     
       试试看,仅针对1位数相加减var
      v, k :Integer;k := 1;  //初始要加的数
    for i:= length(s) downto 1 
    begin
      v := StrToInt(s[i]) + k;
      if v > 10 then
      begin
        k = v div 10;
        s[i] := IntToStr(v mod 10);
      end else
      begin
        s[i] := IntToStr(v);
        break;  //忘了退出循环了
      end;
    end;有问题哈,    s[i] := IntToStr(v mod 10);
    语法错误,    s[i] := IntToStr(v mod 10)[1];
    还有如果是999+1,你看看你的程序运行结果是什么。
      

  13.   

    to kiboisme(蓝色光芒)我只是随手写写,大概写明一下思路而已
    如果真要我写函数,我会返回一个新的字符串的,那999+1就会考虑在里面了
    其实,加,减,乘,我都有思路(包括多位数加减乘多位数),但是除法一直找不到好的算法