pascal的高精度算法
1个回答
2014-01-22
展开全部
高精度加法
var
a,b,c:array[1..201] of 0..9;
n:string;
lena,lenb,lenc,i,x:integer;
begin
write('Input augend:'); readln(n);lena:=length(n);
for i:=1 to lena do a[lena-i+1]:=ord(n[i])-ord('0');{加数放入a数组}
write('Input addend:'); readln(n); lenb:=length(n);
for i:=1 to lenb do b[lenb-i+1]:=ord(n[i])-ord('0');{被加数放入b数组}
i:=1;
while (i<=lena) or(i<=lenb) do
begin
x := a[i] + b[i] + x div 10; {两数相加,然后加前次进位}
c[i] := x mod 10; {保存第i位的值}
i := i + 1
end;
if x>=10 {处理最高进位}
then begin lenc:=i; c[i]:=1 end
else lenc:=i-1;
for i:=lenc downto 1 do write(c[i]); writeln {输出结果}
end.
高精度乘法(低对高)
const max=100; n=20;
var a:array[1..max]of 0..9;
i,j,k;x:integer;
begin
k:=1; a[k]:=1;{a=1}
for i:=2 to n do{a*2*3….*n}
begin
x:=0;{进位初始化}
for j:=1 do k do{a=a*i}
begin
x:=x+a[j]*i; a[j]:=x mod 10;x:=x div 10
end;
while x>0 do {处理最高位的进位}
begin
k:=k+1;a[k]:=x mod 10;x:=x div 10
end
end;
writeln;
for i:=k dowento 1 write(a[i]){输出a}
end.
高精度乘法(高对高)
var a,b,c:array[1..200] of 0..9;
n1,n2:string; lena,lenb,lenc,i,j,x:integer;
begin
write('Input multiplier:'); readln(n1);
rite('Input multiplicand:'); readln(n2);
lena:=length(n1); lenb:=length(n2);
for i:=1 to lena do a[lena-i+1]:=ord(n1[i])-ord('0');
for i:=1 to lenb do b[lenb-i+1]:=ord(n2[i])-ord('0');
for i:=1 to lena do
begin
x:=0;
for j:=1 to lenb do{对乘数的每一位进行处理}
begin
x := a[i]*b[j]+x div 10+c[i+j-1];{当前乘积+上次乘积进位+原数}
c[i+j-1]:=x mod 10;
end;
c[i+j]:= x div 10;{进位}
end;
lenc:=i+j;
while (c[lenc]=0) and (lenc>1) do dec(lenc); {最高位的0不输出}
for i:=lenc downto 1 do write(c[i]); writeln
end.
高精度除法
fillchar(s,sizeof(s),0);{小数部分初始化}
fillchar(posi,sizeof(posi),0); {小数值的位序列初始化}
len←0;st←0; {小数部分的指针和循环节的首指针初始化}
read(x,y);{读被除数和除数}
write(x div y);{输出整数部分}
x←x mod y;{计算x除以y的余数}
if x=0 then exit;{若x除尽y,则成功退出}
while len<limit do{若小数位未达到上限,则循环}
begin
inc(len);posi[x]←len;{记下当前位小数,计算下一位小数和余数}
x←x*10; s[len]←x div y;x←x mod y;
if posi[x]<>0 {若下一位余数先前出现过,则先前出现的位置为循环节的开始}
then begin st←posi[x]; break;end;{then}
if x=0 then break; {若除尽,则成功退出}
end;{while}
if len=0
then begin writeln;exit;end;{若小数部分的位数为0,则成功退出;否则输出小数点}
write('.');
if st=0 {若无循环节,则输出小数部分,否则输出循环节前的小数和循环节}
then for i←1 to len do write(s[i])
else begin
for i←1 to st-1 do write(s[i]);
write('(');
for i←st to len do write(s[i]);
write(')');
end;{else}
var
a,b,c:array[1..201] of 0..9;
n:string;
lena,lenb,lenc,i,x:integer;
begin
write('Input augend:'); readln(n);lena:=length(n);
for i:=1 to lena do a[lena-i+1]:=ord(n[i])-ord('0');{加数放入a数组}
write('Input addend:'); readln(n); lenb:=length(n);
for i:=1 to lenb do b[lenb-i+1]:=ord(n[i])-ord('0');{被加数放入b数组}
i:=1;
while (i<=lena) or(i<=lenb) do
begin
x := a[i] + b[i] + x div 10; {两数相加,然后加前次进位}
c[i] := x mod 10; {保存第i位的值}
i := i + 1
end;
if x>=10 {处理最高进位}
then begin lenc:=i; c[i]:=1 end
else lenc:=i-1;
for i:=lenc downto 1 do write(c[i]); writeln {输出结果}
end.
高精度乘法(低对高)
const max=100; n=20;
var a:array[1..max]of 0..9;
i,j,k;x:integer;
begin
k:=1; a[k]:=1;{a=1}
for i:=2 to n do{a*2*3….*n}
begin
x:=0;{进位初始化}
for j:=1 do k do{a=a*i}
begin
x:=x+a[j]*i; a[j]:=x mod 10;x:=x div 10
end;
while x>0 do {处理最高位的进位}
begin
k:=k+1;a[k]:=x mod 10;x:=x div 10
end
end;
writeln;
for i:=k dowento 1 write(a[i]){输出a}
end.
高精度乘法(高对高)
var a,b,c:array[1..200] of 0..9;
n1,n2:string; lena,lenb,lenc,i,j,x:integer;
begin
write('Input multiplier:'); readln(n1);
rite('Input multiplicand:'); readln(n2);
lena:=length(n1); lenb:=length(n2);
for i:=1 to lena do a[lena-i+1]:=ord(n1[i])-ord('0');
for i:=1 to lenb do b[lenb-i+1]:=ord(n2[i])-ord('0');
for i:=1 to lena do
begin
x:=0;
for j:=1 to lenb do{对乘数的每一位进行处理}
begin
x := a[i]*b[j]+x div 10+c[i+j-1];{当前乘积+上次乘积进位+原数}
c[i+j-1]:=x mod 10;
end;
c[i+j]:= x div 10;{进位}
end;
lenc:=i+j;
while (c[lenc]=0) and (lenc>1) do dec(lenc); {最高位的0不输出}
for i:=lenc downto 1 do write(c[i]); writeln
end.
高精度除法
fillchar(s,sizeof(s),0);{小数部分初始化}
fillchar(posi,sizeof(posi),0); {小数值的位序列初始化}
len←0;st←0; {小数部分的指针和循环节的首指针初始化}
read(x,y);{读被除数和除数}
write(x div y);{输出整数部分}
x←x mod y;{计算x除以y的余数}
if x=0 then exit;{若x除尽y,则成功退出}
while len<limit do{若小数位未达到上限,则循环}
begin
inc(len);posi[x]←len;{记下当前位小数,计算下一位小数和余数}
x←x*10; s[len]←x div y;x←x mod y;
if posi[x]<>0 {若下一位余数先前出现过,则先前出现的位置为循环节的开始}
then begin st←posi[x]; break;end;{then}
if x=0 then break; {若除尽,则成功退出}
end;{while}
if len=0
then begin writeln;exit;end;{若小数部分的位数为0,则成功退出;否则输出小数点}
write('.');
if st=0 {若无循环节,则输出小数部分,否则输出循环节前的小数和循环节}
then for i←1 to len do write(s[i])
else begin
for i←1 to st-1 do write(s[i]);
write('(');
for i←st to len do write(s[i]);
write(')');
end;{else}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询