急求 在线等 大整数计算器的设计与实现
发布网友
发布时间:2023-08-28 00:30
我来回答
共1个回答
热心网友
时间:2024-10-28 09:28
直接给你贴几段代码,自己修改一些即可:
高精度PASCAL代码
一、高精度加高精度
program Jia;
uses math;
var i,j,w,l,l1,l2:longint; s1,s2:ansistring;
a,b:array[1..100000]of longint;
procere init;
begin
readln(s1);readln(s2);
l1:=length(s1);
for i:=1 to l1 do
a[i]:=ord(s1[l1-i+1])-48;
l2:=length(s2);
for i:=1 to l2 do
b[i]:=ord(s2[l2-i+1])-48;
l:=max(l1,l2)+1;
end;
procere main;
begin
for i:=1 to l2 do
begin
a[i]:=a[i]+b[i];
if a[i]>=10 then
begin inc(a[i+1],a[i] div 10);a[i]:=a[i] mod 10; end;
end;
end;
procere print;
begin
i:=l; while a[i]=0 do dec(i); l:=i;
for i:=l downto 1 do
write(a[i]);
end;
begin
assign(input,'input.txt');reset(input);
assign(output,'output.txt');rewrite(output);
init;
main;
print;
close(input);close(output);
end.
二、高精度减高精度
program Jian;
var i,j,w,l,l1,l2:longint; s1,s2:ansistring;
a,b:array[1..100000]of longint;
procere init;
begin
readln(s1);readln(s2);
l1:=length(s1);
for i:=1 to l1 do
a[i]:=ord(s1[l1-i+1])-48;
l2:=length(s2);
for i:=1 to l2 do
b[i]:=ord(s2[l2-i+1])-48;
l:=l1;
end;
procere main;
begin
for i:=1 to l2 do
begin
a[i]:=a[i]-b[i];
if a[i]<0 then begin dec(a[i+1]);inc(a[i],10); end;
end;
end;
procere print;
begin
i:=l; while a[i]=0 do dec(i); l:=i;
for i:=l downto 1 do
write(a[i]);
end;
begin
assign(input,'input.txt');reset(input);
assign(output,'output.txt');rewrite(output);
init;
main;
print;
close(input);close(output);
end.
三、高精度乘高精度(压位)
program Cheng;
const base=1000;
var i,j,la,lb,t,code:longint; sa,sb,s:ansistring;
a,b,c:array[0..10000]of longint;
procere init;
begin
readln(sa);readln(sb);la:=length(sa); lb:=length(sb);
t:=la div 3;
for i:=1 to t do
begin
s:=copy(sa,la-i*3+1,3); val(s,a[i],code);
end;
s:=copy(sa,1,la-t*3);
if sa='' then a[0]:=t
else begin val(s,a[t+1],code);a[0]:=t+1; end;
t:=lb div 3;
for i:=1 to t do
begin
s:=copy(sb,lb-i*3+1,3); val(s,b[i],code);
end;
s:=copy(sb,1,lb-t*3);
if sb='' then b[0]:=t
else begin val(s,b[t+1],code);b[0]:=t+1; end;
end;
procere main;
begin
for i:=1 to a[0] do
for j:=1 to b[0] do
c[i+j-1]:=c[i+j-1]+a[i]*b[j];
c[0]:=a[0]+b[0];
for i:=1 to c[0] do
begin
c[i+1]:=c[i+1]+c[i] div base; c[i]:=c[i] mod base;
end;
end;
procere print;
begin
i:=c[0]; while c[i]=0 do dec(i); c[0]:=i;
for i:=c[0] downto 1 do
write(c[i]);
end;
begin
assign(input,'input.txt');reset(input);
assign(output,'output.txt');rewrite(output);
init;
main;
print;
close(input);close(output);
end.