Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace WindowsFormsApplication1
- {
- class BigInteger
- {
- int[] value = new int[200];
- private BigInteger()
- {
- }
- public BigInteger(String a)
- {
- var tmp = a.ToCharArray();
- for (int i = tmp.Length - 1, j = value.Length - 1; i >= 0 && j >= 1; i--, j--)
- {
- int buf;
- value[0] = 0;
- if (int.TryParse(Convert.ToString(tmp[i]), out buf) == true)
- {
- value[j] = buf;
- }
- else
- {
- }
- }
- if (tmp[0] == '-')
- {
- //Console.WriteLine("go");
- value = toComp(value);
- //value[0] = 9;
- }
- }
- public BigInteger add(BigInteger a)
- {
- BigInteger result = new BigInteger();
- if (a.value[0] == 9)
- {
- BigInteger b = new BigInteger();
- b.value = toComp(a.value);
- return sub(b);
- }
- else
- {
- int carry = 0;
- for (int i = value.Length - 1; i >= 0; i--)
- {
- result.value[i] = value[i] + a.value[i] + carry;
- if (result.value[i] < 10)
- {
- carry = 0;
- }
- else
- {
- result.value[i] %= 10;
- carry = 1;
- }
- }
- }
- return result;
- }
- public BigInteger sub(BigInteger a)
- {
- BigInteger result = new BigInteger();
- //Console.WriteLine(String.Concat(a.value));
- if (a.value[0] == 9)
- {
- BigInteger b = new BigInteger();
- b.value = toComp(a.value);
- return add(b);
- }
- else
- {
- int borrow = 0;
- for (int i = value.Length - 1; i >= 0; i--)
- {
- result.value[i] = value[i] - a.value[i] - borrow;
- if (result.value[i] >= 0)
- {
- borrow = 0;
- }
- else
- {
- result.value[i] += 10;
- borrow = 1;
- }
- }
- }
- return result;
- }
- public int[] toComp(int[] a)
- {
- //BigInteger b = new BigInteger();
- int[] b = new int[200];
- for (int i = 0; i < 200; i++)
- {
- b[i] = 9 - a[i];
- }
- b[200 - 1] += 1;
- //BigInteger tmp = new BigInteger("1");
- //b.add(tmp);
- //Console.WriteLine(String.Concat(b));
- return b;
- }
- public static BigInteger operator +(BigInteger a, BigInteger b)
- {
- return a.add(b);
- }
- public static BigInteger operator -(BigInteger a, BigInteger b)
- {
- return a.sub(b);
- }
- public override string ToString()
- {
- string output = "";
- int zeroCheck = 0, check = 0;
- if (value[0] == 9)
- {
- value = toComp(value);
- output = "-";
- }
- for (int i = 1; i < value.Length; i++)
- {
- if (value[i] != 0)
- {
- zeroCheck = 1;
- }
- if (zeroCheck == 1)
- {
- output = String.Concat(output, value[i]);
- check = 1;
- }
- }
- if (check == 0)
- {
- output = String.Concat(output, 0);
- }
- return output;
- }
- }
- }
Add Comment
Please, Sign In to add comment