Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- type
- BigInt = class
- private
- value: array of integer;
- isneg: boolean;
- procedure addStack(v: integer);
- begin
- SetLength(value, length(value) + 1);
- value[high(value)] := v;
- end;
- public
- constructor(s: string);
- var
- str, t: string;
- i, zero_cnt: integer;
- begin
- str := trim(s);
- if (str[1] = '-') or (str[1] = '+') then
- begin
- isneg := (str[1] = '-');
- delete(str, 1, 1);
- end;
- while (str[1] = '0') and (length(str) > 1) do
- begin
- delete(str, 1, 1);
- end;
- i := length(str) - 8; //up to 10^9
- if i <= 0 then
- addStack(StrToInt(str));
- while i > 0 do
- begin
- if i < 0 then
- i := 0;
- t := copy(str, i, 9);
- delete(str, i, 9);
- zero_cnt := 0;
- if (length(t) <> 0) then
- addStack(StrToInt(t));
- if i > 0 then
- dec(i, 9);
- end;
- //if (length(str) <> 0) then
- //addStack(StrToInt(str));
- writeln('value = ', value);
- end;
- function toStr: string;
- var
- i: integer;
- s: string;
- begin
- for i := high(value) downto low(value) do
- begin
- s := IntToStr(value[i]);
- if (i <> high(value)) then
- begin
- while (length(s) <> 9) do
- s := '0' + s;
- end;
- Result := concat(Result, s);
- end;
- end;
- function neg(n1: BigInt): BigInt;
- begin
- Result := n1;
- Result.isneg := not isneg;
- end;
- class function operator+(n1, n2: BigInt): BigInt;
- var
- i: cardinal;
- s2: BigInt;
- carry, sum: cardinal;
- both_neg: boolean;
- begin
- carry := 0;
- both_neg := (n1.isneg and n2.isneg);
- if length(n1.value) > length(n2.value) then
- begin
- Result := n1;
- s2 := n2;
- end
- else
- begin
- result := n2;
- s2 := n1;
- end;
- for i := low(s2.value) to high(s2.value) do
- begin
- sum := result.value[i] + s2.value[i] + carry;
- carry := sum div 1000000000;
- result.value[i] := sum mod 1000000000;
- end;
- while (carry > 0) do
- begin
- inc(i);
- if (i > high(result.value)) then
- result.addStack(0);
- sum := result.value[i] + carry;
- carry := sum div 1000000000;
- result.value[i] := sum mod 1000000000;
- end;
- end;
- class function operator*(n1: BigInt; n2: integer): BigInt;
- var
- i: Cardinal;
- begin
- Result := new BigInt('0');
- for i := 1 to n2 do
- Result := Result + n1;
- end;
- end;
- var
- a, b: BigInt;
- begin
- //a := new BigInt('000000000000000000000001011246910000000000000000000000000000000028691117');
- //a := new BigInt('0100000000000000000000000000000000001');
- //a := new BigInt('0100000000099900000000000000000000002300034000000324000000000000000000000100001488');
- //a := new BigInt('9000000228');
- //b := new BigInt('912345678123948712394867239586228');
- a := new BigInt('0');
- b := new BigInt('0');
- writeln(a.toStr, ' + ', b.toStr, ' = '); writeln((a + b).toStr);
- writeln(-1488 mod 1000, ' ', -1488 div 1000);
- readln;
- end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement