vimix

bigint.cpp

Mar 18th, 2013
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.23 KB | None | 0 0
  1.  
  2. //Venix Cador
  3. //Cs23001
  4. //02/10/2013
  5.  
  6. #include"bigint.h"
  7.  
  8. bigint::bigint()
  9. {
  10. zero();
  11. }
  12.  
  13. void bigint::zero()
  14. {
  15. for(int i=0; i <MaxValue; i++)// to search each value in the integer to zero
  16. {
  17. big[i]= 0;
  18. }
  19. size = 0;
  20.  
  21. }
  22.  
  23. bigint::bigint(int d)
  24. {
  25. zero();
  26. for (int i = 0; d != 0; i++, ++size) // To set the values in the integer array
  27. {
  28. big[i] = d % 10;
  29. d /=10;
  30. }
  31. }
  32.  
  33. bigint::bigint(const char d[]) //declaring the character array here.
  34. {
  35. zero();
  36. int y = 0;
  37.  
  38. do //to initialize each character array value y to the integer value
  39.  
  40. {
  41. ++y;
  42.  
  43. }while (d[y] != '\0');
  44.  
  45. --y;
  46.  
  47. for (int i = 0; d[i] != '\0'; i++, y--, ++size) //setting each array value = to the integer array value.
  48. {
  49. big[y] = (int(d[i]) - int('0')); //Casting the big(bigint array) array to an integer value when assigning it to the character array.
  50. }
  51. }
  52.  
  53. bool bigint::operator ==(const bigint& y) // using y to compare two bigints.
  54. {
  55. for(int i = 0; i < MaxValue; i++)
  56. {
  57. if(y.big[i] != big[i])
  58. {
  59. return false;
  60. }
  61. }
  62. return true;
  63. }
  64.  
  65. bool bigint::operator ==(int d)
  66. {
  67. for (int i = 0; d != 0; ++i)
  68. {
  69. if (big[i] != d % 10)
  70. {
  71. return false;
  72. }
  73. d /= 10;
  74. }
  75. return true;
  76. }
  77.  
  78. bool bigint::operator ==(const char d[])
  79. {
  80. int y = 0;
  81.  
  82. do
  83. {
  84. ++y;
  85. }while (d[y] != '\0');
  86. --y;
  87.  
  88. for (int i = 0; d[i] != '\0'; i++, y--)
  89. if (big[y] != (int(d[i]) - int('0'))) return false;
  90.  
  91. return true;
  92.  
  93. }
  94.  
  95. void bigint::output(std::ostream& out) const
  96. {
  97. int a = MaxValue;
  98. int outi = 0;
  99.  
  100. do
  101. {
  102. --a;
  103. }while (big[a] == 0);
  104. do
  105. {
  106. if(a & 80)
  107. {
  108. out << big[a];
  109. }
  110. else
  111. out << std::endl << big[a];
  112.  
  113. --a;
  114. ++outi;
  115. }while (a >= 0);
  116.  
  117.  
  118. }
  119. bigint bigint::operator +(bigint rhs)const
  120. {
  121. int i = 0;
  122. int remainder = 0;
  123. int temp = 0; //Need temp to hold the remainder values.
  124.  
  125.  
  126. while (i < MaxValue)
  127. {
  128. remainder = rhs.big[i] + big[i] +remainder;
  129. temp = remainder % 10;
  130. remainder /= 10;
  131. rhs.big[i] = temp;
  132. ++i;
  133. }
  134.  
  135. return rhs;
  136. }
  137.  
  138.  
  139. int bigint::operator [](int i)
  140. {
  141. if ((i < 0)||(i >= size))
  142. return 0;
  143. return big[i];
  144. }
  145.  
  146. int bigint::operator [](int i) const
  147. {
  148. if((i < 0) || (i >= size))
  149. return 0;
  150. return big[i];
  151.  
  152. }
  153.  
  154. std::istream& operator >>(std::istream& iput, bigint& rhs)
  155. {
  156. int tempi = 0;
  157. char intry;
  158. char temp[MaxValue];
  159.  
  160. iput >> intry;
  161.  
  162. while(intry != ';' && !iput.eof())//.eof means end o file
  163. // to set the char array temp to the user's input
  164. {
  165. temp[tempi] = intry;
  166. iput >> intry;
  167. ++tempi;
  168. }
  169. temp[tempi] = 0; //To set the final value of the temp array to 0.
  170. rhs = bigint(temp);
  171. return iput;
  172.  
  173. }
  174.  
  175. std::ostream& operator <<(std::ostream& out, const bigint& rhs) //calling rhs from std::istream&.
  176. //This time it will display the input.
  177.  
  178. {
  179. rhs.output(out);
  180. return out;
  181. }
  182. //Multiply bigint by powers of 10
  183.  
  184. void bigint::times10(const int x){ //Shift all elements in the array x number of spaces
  185.  
  186. for(int i = ( MaxValue - 1); i >= 0; i--){
  187. big[i]= big[i - x];
  188.  
  189. }
  190. for(int i = (x -1); i >= 0; --i){ //when previous loop ends, fill remaining x elements with 0.
  191. big[i] = 0;
  192. }
  193. }
  194. void bigint::times_single_digit(int x){ //multiply a bigint and a single digit
  195.  
  196. int temp = 0;
  197. int carry = 0;
  198.  
  199. for(int i = 0; i < MaxValue; i++){
  200.  
  201. temp = ((big[i] * x) + carry) % 10;
  202. carry = ((big[i] * x) + carry) / 10;
  203. big[i]= temp;
  204. }
  205.  
  206. }
  207. bigint bigint::operator*(const bigint& rhs)
  208. {
  209. bigint part, result;
  210.  
  211. for( int i = 0; i <MaxValue; i++){
  212.  
  213. part = * this; //copy the value of the left handside to part
  214.  
  215. part.times_single_digit(rhs.big[i]); //Multiply left handside by the spot in rhs
  216. part.times10(i);// shift the left by i
  217. result = result + part;
  218. }
  219. return result;
  220. }
  221. bigint bigint::factorial() const{
  222.  
  223. bigint result(1);
  224.  
  225. if (bigint(0) == *this) {
  226. return result;
  227. }
  228.  
  229. for (bigint i = 1; i != *this + 1; i = i + 1){
  230. result = result * i;
  231. }
  232.  
  233. return result;
  234. }
Advertisement
Add Comment
Please, Sign In to add comment