//好呀
算法命题:无限位数加法(不考虑错误输入和负数情况)输入范例1:
123456789012345678901234567890
111111111111111111111111111111输出范例1:
234567900123456790012345679001输入范例2:
1190
111111111111111111111111111111输出范例2:
111111111111111111111111112301测试代码:
function Calc(mNumberA: string; mNumberB: string): string;
begin
  { 你的代码 }
end;procedure TForm1.Button1Click(Sender: TObject);
begin
  Edit1.Text := Calc(Edit2.Text, Edit3.Text);
end;

解决方案 »

  1.   

    很简单了:
    function Calc(mNumberA: string; mNumberB: string): string;
    var Sum:array of byte;
        aLen,bLen,MaxLen,i,ii:integer;
    begin
        aLen:=Length(mNumberA);bLen:=Length(mNumberB);
        if aLen>bLen then MaxLen:=aLen else MaxLen:=bLen;
        if MaxLen<=0 then exit; SetLength(Sum,MaxLen+2);
        for i:=0 to MaxLen+1 do Sum[i]:=0; Sum[0]:=MaxLen;
        for i:=1 to aLen do Sum[i]:=Sum[i]+ord(MNumberA[aLen+1-i])-48;
        for i:=1 to bLen do Sum[i]:=Sum[i]+ord(MNumberB[bLen+1-i])-48;
        for i:=Sum[0]+1 downto 1  do
           if Sum[i]<>0 then break;
        i:=1;
        while(i<Sum[0])or(Sum[i]>=10) do
         begin
           Sum[i+1]:=Sum[i+1]+Sum[i] div 10;
           Sum[i]:=Sum[i] mod 10;
           inc(i);
         end;
        Result:='';
        for ii:=i downto 1 do Result:=result+chr(sum[ii]+48);
        SetLength(Sum,0);
    end;
    如Calc('123456789012345678901234567890',
        '111111111111111111111111111111')
     =234567900123456790012345679001
    但是必须保证2个参数(10进制)都是只由10个数字组成的字符串(不含非法字符
    ,不含+,-)
      

  2.   

    !!!!!!!!!!!!!!!!
    对不起!上面的那个程序有问题!!
    应该改为!
    -----------------------------------------------------------------------------
    function Calc(mNumberA: string; mNumberB: string): string;
    var Sum:array of byte;
        aLen,bLen,MaxLen,i,ii:integer;
    begin
        aLen:=Length(mNumberA);bLen:=Length(mNumberB);
        if aLen>bLen then MaxLen:=aLen else MaxLen:=bLen;
        if MaxLen<=0 then exit; SetLength(Sum,MaxLen+2);
        for i:=0 to MaxLen+1 do Sum[i]:=0; Sum[0]:=MaxLen;
        for i:=1 to aLen do Sum[i]:=Sum[i]+ord(MNumberA[aLen+1-i])-48;
        for i:=1 to bLen do Sum[i]:=Sum[i]+ord(MNumberB[bLen+1-i])-48;
        i:=1;
        while(i<Sum[0])or(Sum[i]>=10) do
         begin
           Sum[i+1]:=Sum[i+1]+Sum[i] div 10;
           Sum[i]:=Sum[i] mod 10;
           inc(i);
         end;
        Result:='';
        for ii:=Sum[0]+1 downto 1  do
           if Sum[ii]<>0 then break;
        for i:=ii downto 1 do Result:=result+chr(sum[i]+48);
        SetLength(Sum,0);
    end;
    -----------------------------------------------------------------------------
      

  3.   

    不错测试成功,参考函数Low(),High()
    有小数的情况呢?
      

  4.   

    hehe,有小数的情况,我忘考虑了!
    其实也好办,整数部分先相加,小数部分再相加,
    最后合到一起就行了(注意可能有的进位)
    如0.991+0.1可以当成991+100得1091,左移max(3,1)位得1.091,1进上去,小数部分为0.091
      

  5.   

    function Calc(mNumberA, mNumberB: string): string;  function Max(mIntA, mIntB: Integer): Integer;
      begin
        if mIntA > mIntB then
          Result := mIntA
        else Result := mIntB;
      end;var
      LA, LB: Integer;
      I: Integer;
      T: Integer;
    begin
      Result := '';
      T := 0;
      LA := Length(mNumberA);
      LB := Length(mNumberB);
      for I := 1 to Max(LA, LB) do begin
        if LA - I + 1 > 0 then
          T := StrToIntDef(mNumberA[LA - I + 1], 0) + T;
        if LB - I + 1 > 0 then
          T := StrToIntDef(mNumberB[LB - I + 1], 0) + T;
        Result := IntToStr(T mod 10) + Result;
        T := T div 10
      end;
      if T <> 0 then Result := IntToStr(T mod 10) + Result;
      while Pos('0', Result) = 1 do Delete(Result, 1, 1);
      if Result = '' then Result := '0';
    end;procedure TForm1.Button1Click(Sender: TObject);
    begin
      Edit3.Text := Calc(Edit1.Text, Edit2.Text);
    end;