Advertisement
Guest User

Big Int

a guest
Mar 29th, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pascal 3.26 KB | None | 0 0
  1. type
  2.   BigInt = class
  3.   private
  4.     value: array of integer;
  5.     isneg: boolean;
  6.     procedure addStack(v: integer);
  7.     begin
  8.       SetLength(value, length(value) + 1);
  9.       value[high(value)] := v;
  10.     end;
  11.  
  12.   public
  13.     constructor(s: string);
  14.     var
  15.       str, t: string;
  16.       i, zero_cnt: integer;
  17.     begin
  18.       str := trim(s);
  19.       if (str[1] = '-') or (str[1] = '+') then
  20.       begin
  21.         isneg := (str[1] = '-');
  22.         delete(str, 1, 1);
  23.       end;
  24.       while (str[1] = '0') and (length(str) > 1) do
  25.       begin
  26.         delete(str, 1, 1);  
  27.       end;
  28.       i := length(str) - 8; //up to 10^9
  29.       if i <= 0 then
  30.         addStack(StrToInt(str));
  31.       while i > 0 do
  32.       begin
  33.         if i < 0 then
  34.           i := 0;
  35.         t := copy(str, i, 9);
  36.         delete(str, i, 9);
  37.         zero_cnt := 0;
  38.         if (length(t) <> 0) then
  39.           addStack(StrToInt(t));
  40.         if i > 0 then
  41.           dec(i, 9);
  42.       end;
  43.       //if (length(str) <> 0) then
  44.         //addStack(StrToInt(str));
  45.       writeln('value = ', value);
  46.     end;
  47.    
  48.     function toStr: string;
  49.     var
  50.       i: integer;
  51.       s: string;
  52.     begin
  53.       for i := high(value) downto low(value) do
  54.       begin
  55.         s := IntToStr(value[i]);
  56.         if (i <> high(value)) then
  57.         begin
  58.           while (length(s) <> 9) do
  59.             s := '0' + s;
  60.         end;
  61.         Result := concat(Result, s);
  62.       end;
  63.     end;
  64.    
  65.     function neg(n1: BigInt): BigInt;
  66.     begin
  67.       Result := n1;
  68.       Result.isneg := not isneg;
  69.     end;
  70.    
  71.     class function operator+(n1, n2: BigInt): BigInt;
  72.     var
  73.       i: cardinal;
  74.       s2: BigInt;
  75.       carry, sum: cardinal;
  76.       both_neg: boolean;
  77.     begin
  78.       carry := 0;
  79.       both_neg := (n1.isneg and n2.isneg);
  80.       if length(n1.value) > length(n2.value) then
  81.       begin
  82.         Result := n1;
  83.         s2 := n2;
  84.       end
  85.       else
  86.       begin
  87.         result := n2;
  88.         s2 := n1;
  89.       end;
  90.       for i := low(s2.value) to high(s2.value) do
  91.       begin
  92.         sum := result.value[i] + s2.value[i] + carry;
  93.         carry := sum div 1000000000;
  94.         result.value[i] := sum mod 1000000000;
  95.       end;
  96.       while (carry > 0) do
  97.       begin
  98.         inc(i);
  99.         if (i  > high(result.value)) then
  100.           result.addStack(0);
  101.         sum := result.value[i] + carry;
  102.         carry := sum div 1000000000;
  103.         result.value[i] := sum mod 1000000000;
  104.       end;
  105.     end;
  106.    
  107.     class function operator*(n1: BigInt; n2: integer): BigInt;
  108.     var
  109.       i: Cardinal;
  110.     begin
  111.       Result := new BigInt('0');
  112.       for i := 1 to n2 do
  113.         Result := Result + n1;
  114.     end;
  115.   end;
  116.  
  117. var
  118.   a, b: BigInt;
  119.  
  120. begin
  121.   //a := new BigInt('000000000000000000000001011246910000000000000000000000000000000028691117');
  122.   //a := new BigInt('0100000000000000000000000000000000001');
  123.   //a := new BigInt('0100000000099900000000000000000000002300034000000324000000000000000000000100001488');
  124.   //a := new BigInt('9000000228');
  125.   //b := new BigInt('912345678123948712394867239586228');
  126.   a := new BigInt('0');
  127.   b := new BigInt('0');
  128.   writeln(a.toStr, ' + ', b.toStr, ' = '); writeln((a + b).toStr);
  129.   writeln(-1488 mod 1000, ' ', -1488 div 1000);
  130.   readln;
  131. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement