free pascal高精度加法计算,求高手看看程序!追加悬赏50!var s,s1,s2:string; a,b,c:array [1..260] of integer; i,l,k1,k2:integer; begin write('input s1:');readln(s1); write('input s2:');readln(s2); l:=length(s1); k
来源:学生作业帮助网 编辑:六六作业网 时间:2024/11/25 13:47:06
free pascal高精度加法计算,求高手看看程序!追加悬赏50!var s,s1,s2:string; a,b,c:array [1..260] of integer; i,l,k1,k2:integer; begin write('input s1:');readln(s1); write('input s2:');readln(s2); l:=length(s1); k
free pascal高精度加法计算,求高手看看程序!追加悬赏50!
var
s,s1,s2:string;
a,b,c:array [1..260] of integer;
i,l,k1,k2:integer;
begin
write('input s1:');readln(s1);
write('input s2:');readln(s2);
l:=length(s1);
k1:=260;
for i:=l downto 1 do
begin
a[k1]:=ord(s1)-48;
k1:=k1-1;
end;
k1:=k1+1;
l:=length(s2);
k2:=260;
for I+:=l downto 1 do
begin
b[k2]:=ord(s2)-48;
k2:=k2-1;
end;
k2:=k2+1;
if k1>k2 then k:=k2 else k:=k1;
y:=0;
for i:=260 downto k do
begin
x:=a+b+y;
c:=x mod 10;
y:=x div 10;
end;
if y<>0 then begin k:=k-1;c[k]:=y;
end;
for i:=k to 260 do write(c);
writeln;
end.
free pascal高精度加法计算,求高手看看程序!追加悬赏50!var s,s1,s2:string; a,b,c:array [1..260] of integer; i,l,k1,k2:integer; begin write('input s1:');readln(s1); write('input s2:');readln(s2); l:=length(s1); k
{楼上的Compile都有问题(其他的暂且不说).
以后可以自己先调试看看有没有问题}
var a,b,c:array[0..256] of integer;
s1,s2:string;
i,z:integer;
begin
write('input s1:');readln(s1);
write('input s2:');readln(s2);
for i:=length(s1) downto 1 do a[i]:=ord(s1[i])-48;{原来的ord的用法是错误的}
for i:=length(s2) downto 1 do b[i]:=ord(s2[i])-48;
if length(s1)>length(s2) then z:=length(s1) else z:=length(s2);
for i:=z downto 1 do
begin
c[i]:=a[i]+b[i]+c[i];{c[i]自己也要加因为可能存在进位问题,
不明白可以自己去掉再试试23+97}
c[i-1]:=c[i-1]+c[i] div 10;
c[i]:=c[i] mod 10;
end;
if c[0]=0 then{如果两数相加进位将存入c[0]}
for i:=1 to z do write(c[i])
else for i:=0 to z do write(c[i]);
writeln;
end.
你那k1和k2没什么用,直接比较length(s1)和length(s2)就行了。改为:
for i:=length(s1) downto 1 do a[i]:=ord(s1)-48;
for i:=length(s2) downto 1 do b[i]:=ord(s2)-48;
if length(s1)>length(s2) then z:=lengt...
全部展开
你那k1和k2没什么用,直接比较length(s1)和length(s2)就行了。改为:
for i:=length(s1) downto 1 do a[i]:=ord(s1)-48;
for i:=length(s2) downto 1 do b[i]:=ord(s2)-48;
if length(s1)>length(s2) then z:=length(s1) else z:=length(s2);
这样清晰又明了,我看你的程序看了好几分钟都还没明白你写了什么,次要的内容写了那么多。
下面的主要程序改为:
for i:=z downto 1 do
begin
c[i]:=a[i]+b[i];
c[i+1]:=c[i+1]+c[i] div 10;
c[i]:=c[i] mod 10;
end;
if c[z+1]<>0 then z:=z+1;
for i:=1 to z do write(a[i]) ;
最后程序就缩短了很多:
var a,b,c:array[1..256] of integer;
s1,s2:string;
i,z:integer;
begin
write('input s1:');readln(s1);
write('input s2:');readln(s2);
for i:=length(s1) downto 1 do a[i]:=ord(s1)-48;
for i:=length(s2) downto 1 do b[i]:=ord(s2)-48;
if length(s1)>length(s2) then a:=length(s1) else a:=length(s2);
for i:=z downto 1 do
begin
c[i]:=a[i]+b[i];
c[i+1]:=c[i+1]+c[i] div 10;
c[i]:=c[i] mod 10;
end;
if c[z+1]<>0 then z:=z+1;
for i:=1 to z do write(a[i]) ;
end.
收起