Advertisement
Guest User

Untitled

a guest
Jun 25th, 2017
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.30 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdlib>
  3. #include <string>
  4. #include <cassert>
  5. #include <cctype>
  6.  
  7. using namespace std;
  8.  
  9. const int BIGINT_SIZE(100);
  10. typedef unsigned int BIGINT[BIGINT_SIZE];
  11.  
  12. istream & operator >>(istream & is, BIGINT & b)
  13. {
  14. string s;
  15. is >> s;
  16.  
  17. for (int i = 0; i < BIGINT_SIZE; ++i)
  18. b[i] = 0;
  19.  
  20. for (int i = 0; i < s.length(); ++i)
  21. {
  22. assert(isdigit(s[i]));
  23. b[s.length() - 1 - i] = s[i] - '0';
  24. }
  25.  
  26. return is;
  27.  
  28. }
  29.  
  30.  
  31. ostream & operator <<(ostream & os, const BIGINT b)
  32. {
  33. int i;
  34.  
  35. for (i = BIGINT_SIZE - 1; i >= 0 && b[i] == 0; --i)
  36. ;
  37.  
  38. if (i < 0) // number zero
  39. os << "0";
  40. else
  41. while (i >= 0)
  42. os << b[i--];
  43.  
  44.  
  45. return os;
  46. }
  47.  
  48. void add(const BIGINT x, const BIGINT y, BIGINT answer)
  49. {
  50. for (int i = 0; i < BIGINT_SIZE; ++i)
  51. answer[i] = 0;
  52.  
  53.  
  54. unsigned int carry(0);
  55. for (int i = 0; i < BIGINT_SIZE; ++i)
  56. {
  57. int sum = x[i] + y[i] + carry;
  58. answer[i] = sum % 10;
  59. carry = sum / 10;
  60. }
  61.  
  62.  
  63. }
  64.  
  65.  
  66. int main()
  67. {
  68.  
  69. BIGINT b, c, a;
  70.  
  71.  
  72. cout << "Enter two bigint: ";
  73. cin >> b >> c;
  74. cout << "Read bigint: " << b << ", " << c << endl;
  75.  
  76. add(b, c, a);
  77.  
  78. cout << "Addition: " << a << endl;
  79.  
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement