例如求任意A除B的精确值运算:
1、A、B均可使用数值变量来接收与存储(A、B均为计算机允许显示的精度)。
2、A除B的精确值有两种情况:
其一A能被B整除,没有余数。
其二A不能被B整除,须对余数进行处理。
所以在做除法运算时(不变量--除数,变量--被除数、商和余数),每次都是用被除数减去商与除数的积,如果所得余数不为零,则将其扩大10倍再次作为被除数,继续试除,直至余数为零或达到要求的精确度为止。最后,必须对精确度的后一位求商,然后判断其值而四舍五入得出最后的结果。
Program gjdcf;//高精度运算
var a,b,c:string;
ch,yu:array[0..20001]of string;
tp:array[0..20001]of boolean;
re,d:array[0..20001]of char;
i,st,en,dep,totd,poi,tt:longint;
function gjf(s1,s2:string):string;
var la,lb,k,x,y,t:longint;
c:string;
pd:boolean;
begin
la:=length(s1);
lb:=length(s2);
k:=0; x:=0; y:=0; c:=''; t:=0;
while (la>0)and(lb>0) do
begin
x:=10+ord(s1[la])-48-k;
y:=ord(s2[lb])-48;
t:=(x-y) mod 10;
if x-y<10 then k:=1 else k:=0;
la:=la-1;
lb:=lb-1;
c:=char(t+48)+c;
end;
while la>0 do
begin
x:=10+ord(s1[la])-48-k;
t:=x mod 10;
if x<10 then k:=1 else k:=0;
la:=la-1;
lb:=lb-1;
c:=char(t+48)+c;
end;
while lb>0 do
begin
x:=10+ord(s1[la])-48-k;
t:=x mod 10;
if x<10 then k:=1 else k:=0;
la:=la-1;
lb:=lb-1;
c:=char(t+48)+c;
end;
if (length(c)>1) and (c[1]='0') then
begin
t:=2;
while c[t]='0' do inc(t);
pd:=true;
for i:=1 to length(c) do if c[i]<>'0' then begin pd:=false;break; end;
if pd=true then dec(t);
delete(c,1,t-1);
end;
gjf:=c;
end;
procedure calc;
var x,y,i,j:longint;
fin:boolean;
ts,tmp:string;
begin
fin:=false;
i:=1; ts:=''; tmp:='';
dep:=0;
repeat
begin
if (i<>poi) then ts:=ts+d[i] else begin inc(dep);re[i]:='.';end;
if i<>poi then
begin
x:=length(ts); y:=length(c);
if (x<y)or((x=y)and(ts<c)) then
begin
if i=totd then begin inc(dep);re[dep]:='0';if poi=0 then begin dep:=dep+1;re[dep]:='.';poi:=-1;end;tp[dep+1]:=true;inc(totd);d[totd]:='0'; end
else begin inc(dep);re[dep]:='0'; end;
end
else if ((x=y)and(ts>=c)) or (x>y) then
begin
for j:=0 to 9 do
begin
if length(ch[j])<=length(ts) then
tmp:=gjf(ts,ch[j]);
if ((length(tmp)=length(c))and(tmp<c))
or(length(tmp)<length(c)) then
begin inc(dep);re[dep]:=chr(j+48); yu[dep]:=tmp;
if yu[dep]='0' then
begin if i=totd then exit
else ts:=''; break;
end
else begin
ts:=yu[dep];
if i=totd then
begin inc(totd);d[totd]:='0';if poi=0 then begin inc(dep);re[dep]:='.';poi:=-1;end;tp[dep+1]:=true; end;
break;
end;
end;
end;
end;
end;
inc(i);
if (yu[dep]<>'0')and(yu[dep]<>'') then
for j:=1 to dep-1 do
if (yu[j]=yu[dep])and(tp[j]=true)and(tp[dep]=tp[j]) then
begin if re[dep]<>re[j] then begin st:=j+1;en:=dep; end
else begin st:=j;en:=dep-1; end ;
fin:=true;break;
end;
end;
until (fin=true)or((poi=0)and(dep=10000))or((poi<>0)and(dep-poi=10000));
end;
procedure spe(y:string);
var i:longint;
begin
for i:=1 to totd do d[i]:=y[i];
end;
procedure removepoint;
var s,t,i:longint;
pd:boolean;
d:string;
begin
t:=pos('.',b);
d:='';
if t<>0 then
begin
for i:=1 to t-1 do c:=c+b[i];
for i:=t+1 to length(b) do c:=c+b[i];
s:=pos('.',a);
if s=0 then begin d:=a;for i:=1 to length(b)-t do d:=d+'0'; end
else begin
if length(b)-t+s >=length(a) then
begin
for i:=1 to length(a) do if a[i]<>'.' then d:=d+a[i];
for i:=1 to length(b)-t+s-length(a) do d:=d+'0';
end
else
begin
for i:=1 to length(b)-t+s do
if a[i]<>'.' then d:=d+a[i];
d:=d+'.'; for i:=length(b)-t+s+1 to length(a) do d:=d+a[i];
end;
end;
end
else begin d:=a;c:=b; end;
for i:=1 to length(c) do if c[i]<>'0' then break;
delete(c,1,i-1);
pd:=true;
for i:=1 to length(d) do if d[i]<>'0' then begin if d[i]='.' then pd:=false;break;end;
if (i<>length(d)) then if pd=false then begin if i<>2 then delete(d,2,i-2); end else delete(d,1,i-1);
totd:=length(d);
spe(d);
poi:=pos('.',d);
end;
function multiply(t1,t2:string):string;
var x,y,z:array[0..255]of longint;
f,p:string;
i,j,t,k:longint;
begin
fillchar(x,sizeof(x),0);
fillchar(y,sizeof(y),0);
fillchar(z,sizeof(z),0);
f:='';
for i:=1 to length(t1) do
x[length(t1)-i]:=ord(t1[i])-48;
for i:=1 to length(t2) do
y[length(t2)-i]:=ord(t2[i])-48;
for i:=0 to length(t1)-1 do
for j:=0 to length(t2)-1 do
z[i+j]:=z[i+j]+x[i]*y[j];
t:=0;
k:=length(t1)+length(t2)-1;
for j:=0 to length(t1)+length(t2)-1 do
begin
if (j=length(t1)+length(t2)-1)and(t=0) then k:=j-1;
z[j]:=z[j]+t;
if z[j]>=10 then t:=z[j] div 10 else t:=0;
z[j]:=z[j] mod 10;
end;
for i:=0 to k do
begin
p:=char(z[i]+48);
f:=p+f;
end;
multiply:=f;
end;
begin
assign(input,'gjdcf.in');
reset(input);
assign(output,'gjdcf.out');
rewrite(output);
readln(a);
readln(b);
if b='0' then begin writeln('Error:Division by zero!');exit;end
else if a='0' then begin writeln(0);exit;end
else if a=b then begin writeln(1);exit; end;
removepoint;
ch[0]:='0';
for i:=1 to 9 do ch[i]:=multiply(c,chr(48+i));
calc;
if st=0 then
begin
tt:=0;
while re[tt+1]='0' do inc(tt);
if re[tt+1]='.' then dec(tt);
if dep=10000 then write('about');
for i:=tt+1 to dep do
write(re[i]);
end
else begin
tt:=0;
while re[tt+1]='0' do inc(tt);
if re[tt+1]='.' then dec(tt);
for i:=tt+1 to st-1 do write(re[i]);
write('(',re[st]);
for i:=st+1 to en-1 do write(re[i]);
write(re[en],')');
end;
writeln;
close(input);
close(output);
end.
|