Advertisement
Guest User

Untitled

a guest
Jun 25th, 2017
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.55 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 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. if(s[0] == '-') {
  20. for (int i = 1; i < s.length(); ++i)
  21. {
  22. assert(isdigit(s[i]));
  23. b[0] = '-';
  24. b[s.length() - 1 - i] = s[i] - '0';
  25. }
  26. }
  27. else {
  28. for (int i = 0; i < s.length(); ++i)
  29. {
  30. assert(isdigit(s[i]));
  31. b[s.length() - 1 - i] = s[i] - '0';
  32. }
  33. }
  34.  
  35. return is;
  36.  
  37. }
  38.  
  39.  
  40. ostream & operator <<(ostream & os, const BIGINT b)
  41. {
  42. int i;
  43.  
  44. for (i = BIGINT_SIZE - 1; i >= 0 && b[i] == 0; --i)
  45. ;
  46.  
  47. if (i < 0) // number zero
  48. os << "0";
  49. else
  50. while (i >= 0)
  51. os << b[i--];
  52.  
  53.  
  54. return os;
  55. }
  56.  
  57. void add(const BIGINT x, const BIGINT y, BIGINT answer)
  58. {
  59. for (int i = 0; i < BIGINT_SIZE; ++i)
  60. answer[i] = 0;
  61.  
  62.  
  63. int carry(0);
  64. for (int i = 0; i < BIGINT_SIZE; ++i)
  65. {
  66. int sum = x[i] + y[i] + carry;
  67. answer[i] = sum % 10;
  68. carry = sum / 10;
  69. }
  70.  
  71.  
  72. }
  73.  
  74.  
  75. int main()
  76. {
  77.  
  78. BIGINT b, c, a;
  79.  
  80.  
  81. cout << "Enter two bigint: ";
  82. cin >> b >> c;
  83. cout << "Read bigint: " << b << ", " << c << endl;
  84.  
  85. add(b, c, a);
  86.  
  87. cout << "Addition: " << a << endl;
  88. while(true){}
  89. return 0;
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement