Раздел «Алгоритмы».LongArithmeticsPAS:

Длинная арифметика на Pascal

//(C) Igor Kvasov
const
  maxl = 100;

type
  Long = record
    l:longint;
    d:array[1..maxl] of longint;{must be signed type,        }
  end;                          {important fo sub proc.      }

procedure short2long(a:longint;var b:long);
begin
  b.l:=0;
  while a>0 do begin
    inc(b.l);
    b.d[b.l]:=a mod 10;
    a:=a div 10;
  end;
  if a=0 then begin b.l:=1; b.d[1]:=0; end;
end;

procedure readlong(var a:long);
var
  ch:char;
  i,buf:longint;

begin
  a.l:=0;
  while not eoln do begin
    read(ch); inc(a.l); a.d[a.l]:=ord(ch)-ord('0');
  end;
  if not eof then readln;
  if a.l=0 then begin a.l:=1; a.d[1]:=0; end;
  for i:=1 to a.l div 2 do begin
    buf:=a.d[i]; a.d[i]:=a.d[a.l-i+1]; a.d[a.l-i+1]:=buf;
  end;
end;

procedure outlong(a:long);
var
  i:longint;

begin
  for i:=a.l downto 1 do write(a.d[i]);writeln;
end;

function cmp(a,b:long):longint;
var
  i:longint;

begin
  if a.l<b.l then begin cmp:=-1;exit;end else
  if a.l>b.l then begin cmp:=1;exit;end else
  for i:=a.l downto 1 do
  if a.d[i]<b.d[i] then begin cmp:=-1;exit;end else
  if a.d[i]>b.d[i] then begin cmp:=1;exit;end;
  cmp:=0;
end;

procedure sum(a,b:long;var c:long);
var
  i,um:longint;

begin
  if a.l<b.l then begin for i:=a.l+1 to b.l do a.d[i]:=0; a.l:=b.l;end;
  if a.l>b.l then begin for i:=b.l+1 to a.l do b.d[i]:=0; b.l:=a.l;end;
  um:=0;
  for i:=1 to a.l do begin
    c.d[i]:=(a.d[i]+b.d[i]+um)mod 10;
    um:=(a.d[i]+b.d[i]+um)div 10;
  end;
  if um=0 then c.l:=a.l else begin c.l:=a.l+1; c.d[c.l]:=1;end;
end;

procedure sub(a,b:long;var c:long); {expected a>=b}
var
  i,um:longint;

begin
  for i:=b.l+1 to a.l do b.d[i]:=0;
  um:=0;
  for i:=1 to a.l do begin
    c.d[i]:=a.d[i]-b.d[i]-um;
    um:=ord(c.d[i]<0);
    if um=1 then c.d[i]:=c.d[i]+10;
  end;
  c.l:=a.l;
  while (c.l>1)and(c.d[c.l]=0) do dec(c.l);
end;

procedure mul(a,b:long;var c:long);
var
  i,j,um,s:longint;

begin
  c.l:=a.l+b.l;
  for i:=1 to c.l do c.d[i]:=0;
  for i:=1 to a.l do
    for j:=1 to b.l do c.d[i+j-1]:=c.d[i+j-1]+a.d[i]*b.d[j];
  um:=0;
  for i:=1 to c.l do begin
    s:=(c.d[i]+um)mod 10;
    um:=(c.d[i]+um)div 10;
    c.d[i]:=s;
  end;
  while (c.l>1)and(c.d[c.l]=0) do dec(c.l);
end;

procedure mulShort(a:long;b:longint;var c:long);
var
  i,um:longint;

begin
  for i:=a.l+1 to a.l+10 do a.d[i]:=0;
  um:=0;
  for i:=1 to a.l+10 do begin
    c.d[i]:=(a.d[i]*b+um)mod 10;
    um:=(a.d[i]*b+um)div 10;
  end;
  c.l:=a.l+10;
  while (c.l>1)and(c.d[c.l]=0) do dec(c.l);
end;

procedure del(a,b:long;var c,d:long);
var
  i,j,l,r,m:longint;
  e:long;

begin
  d.l:=0;
  for i:=a.l downto 1 do begin
    if (d.l=1)and(d.d[1]=0)then d.l:=0;
    for j:=d.l downto 1 do d.d[j+1]:=d.d[j];
    d.d[1]:=a.d[i]; inc(d.l);
    l:=0; r:=9;
    while l<r do begin
      m:=(l+r+1)div 2;
      mulShort(b,m,e);
      if cmp(e,d)=1 then r:=m-1 else l:=m;
    end;
    if l<>m then mulShort(b,l,e);
    c.d[i]:=l; sub(d,e,d);
  end;
  c.l:=a.l;
  while (c.l>1)and(c.d[c.l]=0) do dec(c.l);
end;

procedure delShort(a:long;b:longint;var c:long;var d:longint);
var
  i:longint;

begin
  d:=0;
  for i:=a.l downto 1 do begin
    d:=d*10+a.d[i];
    c.d[i]:=d div b;
    d:=d mod b;
  end;
  c.l:=a.l;
  while (c.l>1)and(c.d[c.l]=0) do dec(c.l);
end;

begin
end.
-- IgorKvasov? - 28 Dec 2004
AlgorithmClasifyForm
Type: Код
Scope: Математика
Strategy:  
Complexity: Low