fork download
  1. #include <iostream>
  2. #include <string>
  3. #include <cstring>
  4.  
  5. #define LONG_INT_LENGTH 100
  6. #define LONG_INT_BASE 10
  7.  
  8. struct BigInt {
  9. int length;
  10. int digits[LONG_INT_LENGTH];
  11. BigInt();
  12. BigInt(int size);
  13. };
  14.  
  15. BigInt::BigInt()
  16. {
  17. std::memset(digits, 0, LONG_INT_LENGTH * sizeof(int));
  18. length = LONG_INT_LENGTH;
  19. }
  20.  
  21. BigInt::BigInt(int size)
  22. {
  23. std::memset(digits, 0, LONG_INT_LENGTH * sizeof(int));
  24. length = size;
  25. }
  26.  
  27. BigInt operator + (const BigInt& a, const BigInt& b)
  28. {
  29. BigInt result(std::max(a.length, b.length));
  30.  
  31. int carry = 0;
  32. for (int i = 0; i < result.length || carry; i++)
  33. {
  34. result.digits[i] = a.digits[i] + b.digits[i] + carry;
  35. if (result.digits[i] >= LONG_INT_BASE)
  36. {
  37. result.digits[i] -= LONG_INT_BASE;
  38. carry = 1;
  39. }
  40. else
  41. carry = 0;
  42. }
  43.  
  44. if (result.digits[result.length])
  45. result.length++;
  46.  
  47. return result;
  48. }
  49.  
  50. std::istream& operator >> (std::istream& is, BigInt& n)
  51. {
  52. std::string str;
  53. is >> str;
  54. for (int i = 0; i < str.size(); i++)
  55. {
  56. n.digits[str.size() - i - 1] = str[i] - '0';
  57. }
  58. n.length = str.size();
  59. return is;
  60. }
  61.  
  62. std::ostream& operator << (std::ostream& os, BigInt& n)
  63. {
  64. for (int i = n.length - 1; i >= 0; i--)
  65. {
  66. std::cout << n.digits[i];
  67. }
  68. return os;
  69. }
  70.  
  71.  
  72. int main()
  73. {
  74. BigInt a, b, c;
  75. std::cin >> a >> b;
  76. c = a + b;
  77. std::cout << c << std::endl;
  78.  
  79. return 0;
  80. }
Success #stdin #stdout 0s 3460KB
stdin
888888888
111111112
stdout
1000000000