Advertisement
Yanislav29

Untitled

Mar 27th, 2020
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.85 KB | None | 0 0
  1. #include "BigInteger.h"
  2.  
  3. BigInteger::BigInteger(): BigInt(new char[4])
  4. {
  5. strcpy_s(BigInt, 3, "111");
  6. }
  7.  
  8. BigInteger::BigInteger(char * BigInt):BigInt(new char[strlen(BigInt+1)])
  9. {
  10. strcpy_s(this->BigInt, strlen(BigInt + 1), BigInt);
  11. }
  12.  
  13. BigInteger::~BigInteger()
  14. {
  15. if (BigInt != nullptr) {
  16. delete[] BigInt;
  17. BigInt = nullptr;
  18. }
  19. }
  20.  
  21. BigInteger::BigInteger(const BigInteger & rhs):BigInt(new char[strlen(rhs.BigInt)+1])
  22. {
  23. strcpy_s(this->BigInt, strlen(BigInt)+1,rhs.BigInt);
  24. }
  25.  
  26. BigInteger & BigInteger::operator=(const BigInteger & rhs)
  27. {
  28. if (this != &rhs) {
  29. if (BigInt != nullptr) {
  30. delete[] BigInt;
  31. BigInt = nullptr;
  32. }
  33. BigInt = new char[strlen(rhs.BigInt + 1)];
  34. strcpy_s(BigInt, strlen(rhs.BigInt) + 1, rhs.BigInt);
  35.  
  36. return *this;
  37. }
  38.  
  39. }
  40.  
  41. int BigInteger::printBigInt() const
  42. {
  43. for (int i = 0;i < strlen(this->BigInt);i++) {
  44. std::cout << BigInt[i];
  45. }
  46. return 0;
  47. }
  48. int BigInteger::setBigInt(char * BigInt)
  49. {
  50.  
  51. if (this->BigInt != nullptr) {
  52. delete[] this->BigInt;
  53. this->BigInt = nullptr;
  54. }
  55. this->BigInt = new char[strlen(BigInt) + 1];
  56. strcpy_s(this->BigInt, strlen(BigInt) + 1, BigInt);
  57. return 0;
  58. }
  59.  
  60. char * BigInteger::getBigInt() const
  61. {
  62. return BigInt;
  63. }
  64.  
  65. BigInteger operator+(const BigInteger & lhs, const BigInteger & rhs)
  66. {
  67. BigInteger res;
  68. int len = strlen(lhs.BigInt);
  69. int len2 = strlen(rhs.BigInt);
  70. int remainder = 0;
  71. int tmp = 0;
  72. int size = ((len > len2) ? len : len2);
  73. int size2 = ((len < len2) ? len : len2);
  74.  
  75. for (int i = 0;i < size;i++) {
  76.  
  77. if (i < size2 - 1) {
  78. tmp = (int)lhs.BigInt[i] + (int)rhs.BigInt[i] + remainder;
  79. }
  80. else {
  81. if (size2 < len) {
  82. tmp = (int)rhs.BigInt[i] + remainder;
  83. }
  84. else {
  85. tmp = (int)lhs.BigInt[i] + remainder;
  86. }
  87. }
  88. remainder = tmp / 10;
  89. res.BigInt[i] = (char)(tmp % 10);
  90. }
  91. return res;
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement