{ Copyright EHomsoft(zhouwei@ehom.net) }
function InfiniteDiv(mNumberA, mNumberB: string;n:integer): string; { 无限位数除法 }
function vDecimal(mNumber: string):integer;
var m,x:integer;
begin
x:=0;
if Pos('.', mNumber) <= 0 then
begin
for m:=Length(mNumber) downto 1 do
begin
if mNumber[m]='0' then x:=x+1 else Break;
end;
Result:=-x;
end
else
Result:=Length(StrRight(mNumber, '.'));
end;
function formatnum2(mNumber: string):string;
begin
Result:=mNumber;
if Pos('.', Result )<= 0 then Result := Result + '.';
Result:=StrLeft(Result,'.')+StrRight(Result,'.');
while Pos('0', Result) = 1 do Delete(Result, 1, 1);
while Copy(Result, Length(Result), 1) = '0' do Delete(Result, Length(Result), 1);
end;
var
I,J,t,v,y,Len: Integer;
TemSub,TemNum: string;
begin
Result := '';
mNumberA:=formatnum(mNumberA);
mNumberB:=formatnum(mNumberB);
v:=vDecimal(mNumberA)-vDecimal(mNumberB);
mNumberA:=formatnum2(mNumberA);
mNumberB:=formatnum2(mNumberB);
if mNumberB='' then
Result:='Err'
else if mNumberA='' then
Result:='0'
else
begin
I:=0;
if Length(mNumberA)>Length(mNumberB) then
Len:=Length(mNumberB)
else
Len:=Length(mNumberA);
if Copy(mNumberA,1,Len)>=Copy(mNumberB,1,Len) then
J:=Length(mNumberB)
else
J:=Length(mNumberB)+1;
for y:=1 to J do
begin
if Length(mNumberA)>=y then
TemSub:=TemSub+mNumberA[y]
else
begin
TemSub:=TemSub+'0';
v:=v+1;
end;
end;
while I<=n do
begin
if TemSub[1]>mNumberB[1] then
t:=StrToInt(TemSub[1]) Div StrToInt(mNumberB[1])
else
t:=StrToInt(TemSub[1]+TemSub[2]) Div StrToInt(mNumberB[1]);
TemNum:=InfiniteMult(mNumberB,IntToStr(t));
while (Length(TemNum)>Length(TemSub)) or ((Length(TemNum)=Length(TemSub))and(TemNum>TemSub)) do
begin
t:=t-1;
TemNum:=InfiniteMult(mNumberB,IntToStr(t));
end;
Result:=Result+IntToStr(t);
I:=I+1;
TemSub:=InfiniteSub(TemSub,TemNum);
if (TemSub='0') and (Length(mNumberA)<J) then
begin
v:=v+1;
Break;
end;
if TemSub='0' then TemSub:='';
J:=J+1;
if Length(mNumberA)>=J then
begin
TemSub:=TemSub+mNumberA[J];
end
else
begin
TemSub:=TemSub+'0';
v:=v+1;
end;
end;
if Length(mNumberA)>=J then
v:=v-(Length(mNumberA)-J)-1
else
v:=v-1;
while Copy(Result, Length(Result), 1) = '0' do
begin
v:=v-1;
Delete(Result, Length(Result), 1);
end;
if v>Length(Result) then
Result:='.'+DupeString('0',v-Length(Result)) + Result
else if v>0 then
Insert('.', Result, Length(Result) - v +1);
if v<0 then Result:=Result+DupeString('0',0-v);
if Copy(Result, 1, 1)='.' then Result:='0'+Result;
end;
end; { InfiniteDiv}