Advertisement
Sanlover

Untitled

Nov 9th, 2020
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.06 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4.  
  5. using namespace std;
  6.  
  7. int getUnsignedLongIntSize(const unsigned long int& number)
  8. {
  9. int size = 0;
  10. int num = number;
  11. while (abs(num) != 0)
  12. {
  13. size++;
  14. num /= 10;
  15. }
  16. return size;
  17. }
  18.  
  19. class Real
  20. {
  21. private:
  22. long int* left;
  23. unsigned short int* right;
  24. size_t zeros;
  25.  
  26. public:
  27. Real()
  28. {
  29. left = new long int;
  30. right = new unsigned short int;
  31. *left = 0;
  32. *right = 0;
  33. zeros = 0;
  34. }
  35. Real(const char* left, const char* right)
  36. {
  37. this->left = new long int;
  38. this->right = new unsigned short int;
  39. zeros = 0;
  40. *this->left = atoi(left);
  41. size_t i = 0;
  42. while ((i < strlen(right)) & (right[i] == '0'))
  43. {
  44. zeros++;
  45. i++;
  46. }
  47. *this->right = atoi(right);
  48. }
  49.  
  50. Real operator+(const Real& other)
  51. {
  52. if (*other.left < 0)
  53. {
  54. return Real("1", "2");
  55. }
  56. else
  57. {
  58. long int newLeft = *left + *other.left;
  59. unsigned short int newRight = 0;
  60. unsigned long int first = *right, second = *other.right;
  61.  
  62. if (first != 0)
  63. while (first % 10 == 0)
  64. first /= 10;
  65. if (second != 0)
  66. while (second % 10 == 0)
  67. second /= 10;
  68. size_t sizeF = 1, sizeS = 1;
  69. if (first != 0)
  70. sizeF = getUnsignedLongIntSize(first) + zeros;
  71. if (second != 0)
  72. sizeS = getUnsignedLongIntSize(second) + other.zeros;
  73.  
  74. int difference = abs((int)sizeF - (int)sizeS);
  75.  
  76. if (sizeF > sizeS)
  77. {
  78. second *= static_cast<unsigned short int>(pow(10, difference));
  79. }
  80. else
  81. {
  82. first *= static_cast<unsigned short int>(pow(10, difference));
  83. }
  84. difference = (long int)(sizeF > sizeS ? sizeF : sizeS) - (long int)getUnsignedLongIntSize(second + first);
  85.  
  86. size_t newZeros = 0;
  87. if (difference < 0)
  88. {
  89. difference = abs(difference);
  90. newLeft += (long int)(first + second) / pow(10, sizeF > sizeS ? sizeF : sizeS);
  91.  
  92. std::string s = std::to_string(first + second);
  93. s.erase(0, difference);
  94.  
  95. bool isZero = true;
  96. for (size_t i = 0; i < s.size(); i++)
  97. if (s[i] != '0')
  98. {
  99. isZero = false;
  100. break;
  101. }
  102. int i = 0;
  103. while ((!isZero) & (i < s.size() & s[i] == '0'))
  104. {
  105. newZeros++;
  106. i++;
  107. }
  108. s.erase(0, newZeros);
  109. newRight = stoi(s);
  110. if (!isZero)
  111. while (newRight % 10 == 0)
  112. newRight /= 10;
  113. }
  114. else
  115. {
  116. newRight = first + second;
  117. if (newRight != 0)
  118. while (newRight % 10 == 0)
  119. newRight /= 10;
  120. newZeros = difference;
  121. }
  122.  
  123. if (zeros == other.zeros)
  124. {
  125. zeros = abs(pow(10, int(zeros - 1)) - int(getUnsignedLongIntSize(newRight)));
  126. }
  127. Real toReturn;
  128. *toReturn.left = newLeft;
  129. *toReturn.right = newRight;
  130. toReturn.zeros = newZeros;
  131. return toReturn;
  132. }
  133. }
  134.  
  135. friend ostream& operator<< (ostream& out, const Real& my);
  136. };
  137. ostream& operator<< (ostream& out, const Real& my)
  138. {
  139. out << *my.left << '.';
  140.  
  141. for (size_t i = 0; i < my.zeros; i++)
  142. out << '0';
  143. out << *my.right;
  144. return out;
  145. }
  146. int main()
  147. {
  148. Real a("3", "009");
  149. Real b("4", "991");
  150.  
  151. cout << a + b;
  152. return 0;
  153. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement