关于free pascal的高精度 15
我刚学高精度,我以前编的时候一般不用字符型,所以我想请教下高精度数组型的读入方法和加法的算法。别用字符!!!...
我刚学高精度,我以前编的时候一般不用字符型,所以我想请教下高精度数组型的读入方法和加法的算法。别用字符!!!
展开
3个回答
展开全部
高精度读入就是读进字符串
算法
高精加
program sub;
var s1,s2,s,w:string;t,x,y,z:integer;p:boolean;
c:char;
g:text;
begin
assign(g,'add.in');
reset(g);
readln(g,s1);
readln(g,s2);
close(g);
if length(s1)<length(s2) then begin s:=s1;s1:=s2;s2:=s;end;s:='';
if length(s2)<length(s1) then repeat s2:='0'+s2;until length(s2)=length(s1);
for t:=length(s1)downto 1 do begin
x:=ord(s1[t])-48;y:=ord(s2[t])-48;z:=x+y;if p then z:=z+1;
if z>9 then begin z:=z mod 10;p:=true;end else p:=false;
str(z,w);s:=w+s;
end;
if p then s:='1'+s;
assign(g,'add.out');
rewrite(g);
write(g,s);
close(g);
end.
高精度减法
program sub;
var s1,s2,s,w:string;t,x,y,z:integer;p,q:boolean;
c:char;
g:text;
begin
assign(g,'sub.in');
reset(g);
readln(g,s1);
readln(g,s2);
close(g);
s:='';
if length(s2)<length(s1) then repeat s2:='0'+s2;until length(s2)=length(s1);
if length(s1)<length(s2) then repeat s1:='0'+s1;until length(s2)=length(s1);
for t:=length(s1)downto 1 do begin
x:=ord(s1[t])-48;y:=ord(s2[t])-48;z:=x-y;if p then z:=z-1;
if z<0 then begin z:=10-abs(z);p:=true;end else p:=false;
if (t=1) and p then z:=z-1;
str(z,w);s:=w+s;
end;
repeat if s[1]='0' then delete(s,1,1);until (s[1]>'0') or (s='');if s='' then s:='0';
if p then begin for t:=1 to length(s) do begin c:=chr(9-ord(s[t])+48+48);s[t]:=c;end;
s[1]:=pred(s[1]);s[length(s)]:=succ(s[length(s)]);
repeat if s[1]='0' then delete(s,1,1);until (s[1]>'0') or (s='');if s='' then s:='0';
s:='-'+s; end;
assign(g,'sub.out');
rewrite(g);
write(g,s);
close(g);
end.
高精度乘法
program mul;
var
g:text;s,s1,s2:string;
a,b,c:array[0..500] of longint;
t,i,k,j,w,l1,l2:integer;
begin
assign(g,'mul.in');
reset(g);
readln(g,s1);readln(g,s2);
close(g);
l1:=length(s1);l2:=length(s2);i:=0;
for t:=l1 downto 1 do begin i:=i+1;a[i]:=ord(s1[t])-48;end;
i:=0;
for t:=l2 downto 1 do begin i:=i+1;b[i]:=ord(s2[t])-48;end;
for i:=1 to l1 do
for k:=1 to l2 do
c[i+k-1]:=a[i]*b[k]+c[i+k-1];
for t:=1 to l1+l2 do
if c[t]>9 then begin c[t+1]:=c[t] div 10+c[t+1];c[t]:=c[t] mod 10;end;
assign(g,'mul.out');
rewrite(g);
s:='';
for t:=l1+l2 downto 1 do begin
str(c[t],s1);s:=s+s1;end;
for t:=1 to l1+l2 do
if s[1]='0' then delete(s,1,1);
if s='' then s:='0';
write(g,s);close(g);
end.
高精度除法
program gjd_div;
type hp=array[0..100] of integer;
var
a,b,c,d:hp;
s1,s2:string;
i:integer;
f:text;
ans0,ans1:boolean;
procedure print;
var j:integer;
begin
assign(f,'div.out'); rewrite(f);
if ans0 then begin write(f,0); close(f); exit; end;
if ans1 then begin write(f,1); close(f); exit; end;
i:=1; while (i<=a[0]) and (c[i]=0) do inc(i);
if i>a[0] then write(f,0) else
for j:=i to a[0] do write(f,c[j]);
close(f);
end;
procedure init;
var temp:string;
begin
ans0:=false; ans1:=false;
assign(f,'div.in'); reset(f);
readln(f,s1); readln(f,s2);
close(f);
if (length(s2)>length(s1)) or ((length(s1)=length(s2) ) and (s2>s1)) then
begin ans0:=true; print; halt; end;
if s1=s2 then begin ans1:=true; print; halt; end;
fillchar(a,sizeof(a),0); a[0]:=length(s1);
fillchar(b,sizeof(b),0); b[0]:=length(s2);
fillchar(c,sizeof(c),0);
for i:=1 to a[0] do a[i]:=ord(s1[i])-ord('0');
for i:=1 to b[0] do b[i]:=ord(s2[i])-ord('0');
end;
procedure mul10(var d:hp);
begin
inc(d[0]); d[d[0]]:=0;
end;
function compare(d,b:hp):integer;
var
i,j:integer;
begin
if b[0]>d[0] then begin compare:=-1;exit; end;
if d[0]>b[0] then begin compare:=1; exit; end;
if b[0]=d[0] then
begin
i:=1;
while (i<=d[0]) and (d[i]=b[i]) do inc(i);
if i>d[0] then begin compare:=0; exit;end;
if d[i]>b[i] then compare:=1 else compare:=-1;
end;
end;
procedure subtract(a,b:hp; var d:hp);
var i,jw,wei:integer;
aa,bb,dd:hp;
begin
fillchar(aa,sizeof(aa),0); fillchar(bb,sizeof(bb),0);
fillchar(dd,sizeof(dd),0);fillchar(d,sizeof(d),0);
aa[0]:=a[0]; bb[0]:=b[0];
for jw:=a[0] downto 1 do aa[a[0]+1-jw]:=a[jw];
for jw:=b[0] downto 1 do bb[b[0]+1-jw]:=b[jw];
jw:=0;
for i:=1 to aa[0] do
begin
wei:=aa[i]-jw-bb[i];
if wei<0 then
begin dd[i]:=wei+10; jw:=1; end
else begin jw:=0; dd[i]:=wei; end;
end;
wei:=a[0]; while (wei>0) and (dd[wei]=0) do dec(wei);
if wei=0 then dd[0]:=0 else dd[0]:=wei;
for wei:=dd[0] downto 1 do d[dd[0]+1-wei]:=dd[wei];
d[0]:=dd[0];
end;
procedure high_devide(a,b:hp; var c,d:hp);
var
i,len:integer;
begin
fillchar(c,sizeof(c),0);
fillchar(d,sizeof(d),0);
len:=a[0];d[0]:=0;
for i:=1 to len do begin
mul10(d);
d[d[0]]:=a[i];
while(compare(d,b)>=0) do {即d>=b}
begin
Subtract(d,b,d);
inc(c[i]);
end;
end;
end;
begin
init;
high_devide(a,b,c,d);
print;
end.
这类高精度的程序都很长,不过一般很少用,通常都可以在FP下用int64
算法
高精加
program sub;
var s1,s2,s,w:string;t,x,y,z:integer;p:boolean;
c:char;
g:text;
begin
assign(g,'add.in');
reset(g);
readln(g,s1);
readln(g,s2);
close(g);
if length(s1)<length(s2) then begin s:=s1;s1:=s2;s2:=s;end;s:='';
if length(s2)<length(s1) then repeat s2:='0'+s2;until length(s2)=length(s1);
for t:=length(s1)downto 1 do begin
x:=ord(s1[t])-48;y:=ord(s2[t])-48;z:=x+y;if p then z:=z+1;
if z>9 then begin z:=z mod 10;p:=true;end else p:=false;
str(z,w);s:=w+s;
end;
if p then s:='1'+s;
assign(g,'add.out');
rewrite(g);
write(g,s);
close(g);
end.
高精度减法
program sub;
var s1,s2,s,w:string;t,x,y,z:integer;p,q:boolean;
c:char;
g:text;
begin
assign(g,'sub.in');
reset(g);
readln(g,s1);
readln(g,s2);
close(g);
s:='';
if length(s2)<length(s1) then repeat s2:='0'+s2;until length(s2)=length(s1);
if length(s1)<length(s2) then repeat s1:='0'+s1;until length(s2)=length(s1);
for t:=length(s1)downto 1 do begin
x:=ord(s1[t])-48;y:=ord(s2[t])-48;z:=x-y;if p then z:=z-1;
if z<0 then begin z:=10-abs(z);p:=true;end else p:=false;
if (t=1) and p then z:=z-1;
str(z,w);s:=w+s;
end;
repeat if s[1]='0' then delete(s,1,1);until (s[1]>'0') or (s='');if s='' then s:='0';
if p then begin for t:=1 to length(s) do begin c:=chr(9-ord(s[t])+48+48);s[t]:=c;end;
s[1]:=pred(s[1]);s[length(s)]:=succ(s[length(s)]);
repeat if s[1]='0' then delete(s,1,1);until (s[1]>'0') or (s='');if s='' then s:='0';
s:='-'+s; end;
assign(g,'sub.out');
rewrite(g);
write(g,s);
close(g);
end.
高精度乘法
program mul;
var
g:text;s,s1,s2:string;
a,b,c:array[0..500] of longint;
t,i,k,j,w,l1,l2:integer;
begin
assign(g,'mul.in');
reset(g);
readln(g,s1);readln(g,s2);
close(g);
l1:=length(s1);l2:=length(s2);i:=0;
for t:=l1 downto 1 do begin i:=i+1;a[i]:=ord(s1[t])-48;end;
i:=0;
for t:=l2 downto 1 do begin i:=i+1;b[i]:=ord(s2[t])-48;end;
for i:=1 to l1 do
for k:=1 to l2 do
c[i+k-1]:=a[i]*b[k]+c[i+k-1];
for t:=1 to l1+l2 do
if c[t]>9 then begin c[t+1]:=c[t] div 10+c[t+1];c[t]:=c[t] mod 10;end;
assign(g,'mul.out');
rewrite(g);
s:='';
for t:=l1+l2 downto 1 do begin
str(c[t],s1);s:=s+s1;end;
for t:=1 to l1+l2 do
if s[1]='0' then delete(s,1,1);
if s='' then s:='0';
write(g,s);close(g);
end.
高精度除法
program gjd_div;
type hp=array[0..100] of integer;
var
a,b,c,d:hp;
s1,s2:string;
i:integer;
f:text;
ans0,ans1:boolean;
procedure print;
var j:integer;
begin
assign(f,'div.out'); rewrite(f);
if ans0 then begin write(f,0); close(f); exit; end;
if ans1 then begin write(f,1); close(f); exit; end;
i:=1; while (i<=a[0]) and (c[i]=0) do inc(i);
if i>a[0] then write(f,0) else
for j:=i to a[0] do write(f,c[j]);
close(f);
end;
procedure init;
var temp:string;
begin
ans0:=false; ans1:=false;
assign(f,'div.in'); reset(f);
readln(f,s1); readln(f,s2);
close(f);
if (length(s2)>length(s1)) or ((length(s1)=length(s2) ) and (s2>s1)) then
begin ans0:=true; print; halt; end;
if s1=s2 then begin ans1:=true; print; halt; end;
fillchar(a,sizeof(a),0); a[0]:=length(s1);
fillchar(b,sizeof(b),0); b[0]:=length(s2);
fillchar(c,sizeof(c),0);
for i:=1 to a[0] do a[i]:=ord(s1[i])-ord('0');
for i:=1 to b[0] do b[i]:=ord(s2[i])-ord('0');
end;
procedure mul10(var d:hp);
begin
inc(d[0]); d[d[0]]:=0;
end;
function compare(d,b:hp):integer;
var
i,j:integer;
begin
if b[0]>d[0] then begin compare:=-1;exit; end;
if d[0]>b[0] then begin compare:=1; exit; end;
if b[0]=d[0] then
begin
i:=1;
while (i<=d[0]) and (d[i]=b[i]) do inc(i);
if i>d[0] then begin compare:=0; exit;end;
if d[i]>b[i] then compare:=1 else compare:=-1;
end;
end;
procedure subtract(a,b:hp; var d:hp);
var i,jw,wei:integer;
aa,bb,dd:hp;
begin
fillchar(aa,sizeof(aa),0); fillchar(bb,sizeof(bb),0);
fillchar(dd,sizeof(dd),0);fillchar(d,sizeof(d),0);
aa[0]:=a[0]; bb[0]:=b[0];
for jw:=a[0] downto 1 do aa[a[0]+1-jw]:=a[jw];
for jw:=b[0] downto 1 do bb[b[0]+1-jw]:=b[jw];
jw:=0;
for i:=1 to aa[0] do
begin
wei:=aa[i]-jw-bb[i];
if wei<0 then
begin dd[i]:=wei+10; jw:=1; end
else begin jw:=0; dd[i]:=wei; end;
end;
wei:=a[0]; while (wei>0) and (dd[wei]=0) do dec(wei);
if wei=0 then dd[0]:=0 else dd[0]:=wei;
for wei:=dd[0] downto 1 do d[dd[0]+1-wei]:=dd[wei];
d[0]:=dd[0];
end;
procedure high_devide(a,b:hp; var c,d:hp);
var
i,len:integer;
begin
fillchar(c,sizeof(c),0);
fillchar(d,sizeof(d),0);
len:=a[0];d[0]:=0;
for i:=1 to len do begin
mul10(d);
d[d[0]]:=a[i];
while(compare(d,b)>=0) do {即d>=b}
begin
Subtract(d,b,d);
inc(c[i]);
end;
end;
end;
begin
init;
high_devide(a,b,c,d);
print;
end.
这类高精度的程序都很长,不过一般很少用,通常都可以在FP下用int64
展开全部
我直接贴一个加法的算法(包括了高精度数组型的读入方法)给YOU:
var s1,s2:string;
l1,l2,i,l:byte;
a,b:array[1..255] of byte;
begin
readln(s1);readln(s2);l1:=length(s1);l2:=length(s2);
fillchar(a,sizeof(a),0);
for i:=1 to l1 do
a[i]:=ord(s1[l1+1-i])-48;
fillchar(b,sizeof(b),0);
for i:=1 to l2 do
b[i]:=ord(s2[l2+1-i])-48;
if l1>l2 then l:=l1 else l:=l2;
for i:=1 to l do
begin
a[i]:=a[i]+b[i];
a[i+1]:=a[i+1]+a[i] div 10;
a[i]:=a[i] mod 10
end;
if a[l+1]<>0 then inc(l);
for i:=l downto 1 do
write(a[i]);
writeln
end.
var s1,s2:string;
l1,l2,i,l:byte;
a,b:array[1..255] of byte;
begin
readln(s1);readln(s2);l1:=length(s1);l2:=length(s2);
fillchar(a,sizeof(a),0);
for i:=1 to l1 do
a[i]:=ord(s1[l1+1-i])-48;
fillchar(b,sizeof(b),0);
for i:=1 to l2 do
b[i]:=ord(s2[l2+1-i])-48;
if l1>l2 then l:=l1 else l:=l2;
for i:=1 to l do
begin
a[i]:=a[i]+b[i];
a[i+1]:=a[i+1]+a[i] div 10;
a[i]:=a[i] mod 10
end;
if a[l+1]<>0 then inc(l);
for i:=l downto 1 do
write(a[i]);
writeln
end.
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
楼上写得很好了……
read(ch);
l1:=0;
while ch in['0'..'9'] do
begin
inc(l1);
a[l1]:=ord(ch)-48;
read(ch);
end;
l2:=0;
read(ch);
while ch in['0'..9'] do
begin
inc(l2);
b[l2]:=ord(ch)=48;
read(ch);
end;
类似吧……我用的字符……
适合于
1234 234
这样的输入
read(ch);
l1:=0;
while ch in['0'..'9'] do
begin
inc(l1);
a[l1]:=ord(ch)-48;
read(ch);
end;
l2:=0;
read(ch);
while ch in['0'..9'] do
begin
inc(l2);
b[l2]:=ord(ch)=48;
read(ch);
end;
类似吧……我用的字符……
适合于
1234 234
这样的输入
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询