pascal高精度加法编译正确,计算结果错误。帮我指出里面的错误。

来源:百度知道 编辑:UC知道 时间:2024/05/17 03:49:56
type arr=array[1..1000] of shortint;
var a,b,c:arr;
i,j:longint;
s:string;
x,y,z:longint;
begin
for i:=1 to 1000 do begin
a[i]:=0;
b[i]:=0;
c[i]:=0;
end;
readln(s);
x:=length(s);
for i:=1 to x do begin
a[i]:=ord(s[i])-48;
end;
readln(s);
y:=length(s);
for i:=1 to y do begin
b[i]:=ord(s[i])-48;
end;
if (x<y) then z:=y else z:=x;
for i:=1 to z do begin
c[z]:=c[z]+a[z]+b[z];
c[z+1]:=c[z+1]+(c[z] div 10);
c[z]:=c[z] mod 10;
end;
if (c[z+1]=0) then begin
for i:=z downto 1 do write(c[i]);
end;
if (c[z+1]<>0) then begin
for i:=z+1 downto 1 do write(c[i]);
end;
writeln;
end.

以上是程序。
指出里面存在的问题,
并且把正确的程序发送上来(修改后的)。

type arr=array[1..1000] of shortint;
var a,b,c:arr;
i,j:longint;
s:string;
x,y,z:longint;
begin
fillchar(a,sizeof(a),0);
fillchar(b,sizeof(b),0);
fillchar(c,sizeof(c),0);
readln(s);
x:=length(s);
for i:=1 to x do begin
a[x-i+1]:=ord(s[i])-ord('0');
end;
readln(s);
y:=length(s);
for i:=1 to y do begin
b[y-i+1]:=ord(s[i])-ord('0');
end;
if (x<y) then z:=y else z:=x;
for i:=1 to z do begin
c[i]:=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 begin
for i:=z downto 1 do write(c[i]);
end;
if (c[z+1]<>0) then begin
for i:=z+1 downto 1 do write(c[i]);
end;
writeln;
end.

有好几段错的。。。
for i:=1 to y do begin
b[i]:=ord(s[i])-48; <---------应该倒序存储,第一位存最后一位
改为for i:=1 to y do begin
b[y-i+1]:=ord(s[i])-ord('0');

第二个你的循环变量很强大。