vimix

bigint.cpp

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