高精度加法 高精度加法程序如下: programHighPrecision1_Plus;
const
fn_inp='hp1.inp';
fn_out='hp1.out';
maxlen=100; { max length of the number }
type
hp=record
len:integer; { length of the number }
s:array[1..maxlen] of integer
{ s[1] is the lowest position
s[len] is the highest position}
end;
var
x:array[1..2] of hp;
y:hp; { x:input ; y ![](static/image/smiley/default/shocked.gif) utput } procedure PrintHP(const p:hp);
var i:integer;
begin
for i:=p.len downto 1 do write(p.s);
end; procedure init;
var
st:string;
j,i:integer;
begin
assign(input,fn_inp);
reset(input);
for j:=1 to 2 do
begin
readln(st);
x[j].len:=length(st);
for i:=1 to x[j].len do { change string to HP }
x[j].s:=ord(st[x[j].len+1-i])-ord('0');
end;
close(input);
end; procedure Plus(a,b:hp;var c:hp); { c:=a+b }
var i,len:integer;
begin
fillchar(c,sizeof(c),0);
if a.len>b.len then len:=a.len { get the biggerlength of a,b }
else len:=b.len;
for i:=1 to len do { plus from low to high }
begin
inc(c.s,a.s+b.s);
if c.s>=10 then
begin
dec(c.s,10);
inc(c.s[i+1]); { add 1 to a higherposition }
end;
end;
if c.s[len+1]>0 then inc(len);
c.len:=len;
end; procedure main;
begin
Plus(x[1],x[2],y);
end; procedure out;
begin
assign(output,fn_out);
rewrite(output);
PrintHP(y);
writeln;
close(output);
end; begin
init;
main;
out;
end.
|