高精度除以高精度

[复制链接]
发表于 2023-12-30 09:44:17 | 显示全部楼层 |阅读模式
高精度除以高精度
程序如下:
programHighPrecision4_Multiply2;
const
  fn_inp='hp6.inp';
  fn_out='hp6.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,w:hp; { x:input ; yutput }
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 Subtract(a,b:hp;var c:hp); { c:=a-b, suppose 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 { subtract from low to high }
    begin
      inc(c.s,a.s-b.s);
      if c.s<0 then
      begin
        inc(c.s,10);
        dec(c.s[i+1]); { add 1 to a higherposition }
      end;
    end;
    while(len>1) and (c.s[len]=0) do dec(len);
    c.len:=len;
  end;
  functionCompare(const a,b:hp):integer;
  {
    1 if a>b
    0 if a=b
   -1 if a<b
  }
  var len:integer;
  begin
    if a.len>b.len then len:=a.len  { get the biggerlength of a,b }
                  else len:=b.len;
    while(len>0) and (a.s[len]=b.s[len]) do dec(len);
    { find a position which have a different digit }
    if len=0 then compare:=0 { no difference }
             elsecompare:=a.s[len]-b.s[len];
  end;
procedure Multiply10(var a:hp); { a:=a*10 }
  var i:Integer;
  begin
    for i:=a.len downto 1 do
      a.s[i+1]:=a.s;
    a.s[1]:=0;
    inc(a.len);
    while(a.len>1) and (a.s[a.len]=0) do dec(a.len);
  end;
procedure Divide(a,b:hp;var c,d:hp); { c:=a div b ; d:=a mod b }
  var i,j,len:integer;
  begin
    fillchar(c,sizeof(c),0);
    len:=a.len;
    fillchar(d,sizeof(d),0);
    d.len:=1;
    for i:=len downto 1 do
    begin
      Multiply10(d);
      d.s[1]:=a.s; { d:=d*10+a.s }
      { c.s:=d div b ; d:=d mod b; }
      { while(d>=b) do begin d:=d-b;inc(c.s) end}
      while(compare(d,b)>=0) do
      begin
        Subtract(d,b,d);
        inc(c.s);
      end;
    end;
    while(len>1)and(c.s[len]=0) do dec(len);
    c.len:=len;
  end;
procedure main;
  begin
    Divide(x[1],x[2],y,w);
  end;
procedure out;
  begin
    assign(output,fn_out);
    rewrite(output);
    PrintHP(y);
    writeln;
    PrintHP(w);
    writeln;
    close(output);
    end;
  begin
    init;
    main;
    out;
  end.

回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

快速回复 返回顶部 返回列表