Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <vector>
- using namespace std;
- int getUnsignedLongIntSize(const unsigned long int& number)
- {
- int size = 0;
- int num = number;
- while (abs(num) != 0)
- {
- size++;
- num /= 10;
- }
- return size;
- }
- class Real
- {
- private:
- long int* left;
- unsigned short int* right;
- size_t zeros;
- public:
- Real()
- {
- left = new long int;
- right = new unsigned short int;
- *left = 0;
- *right = 0;
- zeros = 0;
- }
- Real(const char* left, const char* right)
- {
- this->left = new long int;
- this->right = new unsigned short int;
- zeros = 0;
- *this->left = atoi(left);
- size_t i = 0;
- while ((i < strlen(right)) & (right[i] == '0'))
- {
- zeros++;
- i++;
- }
- *this->right = atoi(right);
- }
- Real operator+(const Real& other)
- {
- if (*other.left < 0)
- {
- return Real("1", "2");
- }
- else
- {
- long int newLeft = *left + *other.left;
- unsigned short int newRight = 0;
- unsigned long int first = *right, second = *other.right;
- if (first != 0)
- while (first % 10 == 0)
- first /= 10;
- if (second != 0)
- while (second % 10 == 0)
- second /= 10;
- size_t sizeF = 1, sizeS = 1;
- if (first != 0)
- sizeF = getUnsignedLongIntSize(first) + zeros;
- if (second != 0)
- sizeS = getUnsignedLongIntSize(second) + other.zeros;
- int difference = abs((int)sizeF - (int)sizeS);
- if (sizeF > sizeS)
- {
- second *= static_cast<unsigned short int>(pow(10, difference));
- }
- else
- {
- first *= static_cast<unsigned short int>(pow(10, difference));
- }
- difference = (long int)(sizeF > sizeS ? sizeF : sizeS) - (long int)getUnsignedLongIntSize(second + first);
- size_t newZeros = 0;
- if (difference < 0)
- {
- difference = abs(difference);
- newLeft += (long int)(first + second) / pow(10, sizeF > sizeS ? sizeF : sizeS);
- std::string s = std::to_string(first + second);
- s.erase(0, difference);
- bool isZero = true;
- for (size_t i = 0; i < s.size(); i++)
- if (s[i] != '0')
- {
- isZero = false;
- break;
- }
- int i = 0;
- while ((!isZero) & (i < s.size() & s[i] == '0'))
- {
- newZeros++;
- i++;
- }
- s.erase(0, newZeros);
- newRight = stoi(s);
- if (!isZero)
- while (newRight % 10 == 0)
- newRight /= 10;
- }
- else
- {
- newRight = first + second;
- if (newRight != 0)
- while (newRight % 10 == 0)
- newRight /= 10;
- newZeros = difference;
- }
- if (zeros == other.zeros)
- {
- zeros = abs(pow(10, int(zeros - 1)) - int(getUnsignedLongIntSize(newRight)));
- }
- Real toReturn;
- *toReturn.left = newLeft;
- *toReturn.right = newRight;
- toReturn.zeros = newZeros;
- return toReturn;
- }
- }
- friend ostream& operator<< (ostream& out, const Real& my);
- };
- ostream& operator<< (ostream& out, const Real& my)
- {
- out << *my.left << '.';
- for (size_t i = 0; i < my.zeros; i++)
- out << '0';
- out << *my.right;
- return out;
- }
- int main()
- {
- Real a("3", "009");
- Real b("4", "991");
- cout << a + b;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement