swattw

Untitled

Jan 1st, 2017
195
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.14 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace WindowsFormsApplication1
  8. {
  9. class BigInteger
  10. {
  11. int[] value = new int[200];
  12.  
  13. private BigInteger()
  14. {
  15.  
  16. }
  17. public BigInteger(String a)
  18. {
  19. var tmp = a.ToCharArray();
  20. for (int i = tmp.Length - 1, j = value.Length - 1; i >= 0 && j >= 1; i--, j--)
  21. {
  22. int buf;
  23. value[0] = 0;
  24. if (int.TryParse(Convert.ToString(tmp[i]), out buf) == true)
  25. {
  26. value[j] = buf;
  27. }
  28. else
  29. {
  30.  
  31. }
  32. }
  33. if (tmp[0] == '-')
  34. {
  35. //Console.WriteLine("go");
  36. value = toComp(value);
  37. //value[0] = 9;
  38. }
  39. }
  40. public BigInteger add(BigInteger a)
  41. {
  42. BigInteger result = new BigInteger();
  43. if (a.value[0] == 9)
  44. {
  45. BigInteger b = new BigInteger();
  46. b.value = toComp(a.value);
  47. return sub(b);
  48. }
  49. else
  50. {
  51. int carry = 0;
  52. for (int i = value.Length - 1; i >= 0; i--)
  53. {
  54. result.value[i] = value[i] + a.value[i] + carry;
  55. if (result.value[i] < 10)
  56. {
  57. carry = 0;
  58. }
  59. else
  60. {
  61. result.value[i] %= 10;
  62. carry = 1;
  63. }
  64. }
  65. }
  66.  
  67. return result;
  68. }
  69.  
  70. public BigInteger sub(BigInteger a)
  71. {
  72. BigInteger result = new BigInteger();
  73. //Console.WriteLine(String.Concat(a.value));
  74. if (a.value[0] == 9)
  75. {
  76. BigInteger b = new BigInteger();
  77. b.value = toComp(a.value);
  78. return add(b);
  79. }
  80. else
  81. {
  82. int borrow = 0;
  83. for (int i = value.Length - 1; i >= 0; i--)
  84. {
  85. result.value[i] = value[i] - a.value[i] - borrow;
  86. if (result.value[i] >= 0)
  87. {
  88. borrow = 0;
  89. }
  90. else
  91. {
  92. result.value[i] += 10;
  93. borrow = 1;
  94. }
  95. }
  96. }
  97. return result;
  98. }
  99.  
  100. public int[] toComp(int[] a)
  101. {
  102. //BigInteger b = new BigInteger();
  103. int[] b = new int[200];
  104. for (int i = 0; i < 200; i++)
  105. {
  106. b[i] = 9 - a[i];
  107. }
  108. b[200 - 1] += 1;
  109. //BigInteger tmp = new BigInteger("1");
  110. //b.add(tmp);
  111.  
  112. //Console.WriteLine(String.Concat(b));
  113. return b;
  114. }
  115. public static BigInteger operator +(BigInteger a, BigInteger b)
  116. {
  117. return a.add(b);
  118. }
  119. public static BigInteger operator -(BigInteger a, BigInteger b)
  120. {
  121. return a.sub(b);
  122. }
  123. public override string ToString()
  124. {
  125. string output = "";
  126. int zeroCheck = 0, check = 0;
  127. if (value[0] == 9)
  128. {
  129. value = toComp(value);
  130. output = "-";
  131. }
  132. for (int i = 1; i < value.Length; i++)
  133. {
  134. if (value[i] != 0)
  135. {
  136. zeroCheck = 1;
  137. }
  138. if (zeroCheck == 1)
  139. {
  140. output = String.Concat(output, value[i]);
  141. check = 1;
  142. }
  143. }
  144. if (check == 0)
  145. {
  146. output = String.Concat(output, 0);
  147. }
  148. return output;
  149. }
  150.  
  151.  
  152. }
  153. }
Add Comment
Please, Sign In to add comment