Advertisement
Guest User

Untitled

a guest
Mar 13th, 2018
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.39 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3.  
  4. using namespace std;
  5.  
  6. class BigInteger {
  7. public:
  8.     explicit BigInteger(long long value = 0) {
  9.         if (value < 0) {
  10.             isNegative = true;
  11.         } else {
  12.             isNegative = false;
  13.         }
  14.         while (value > 0) {
  15.             digits.push_back(value % 10);
  16.             value /= 10;
  17.         }
  18.         // reverse(digits.begin(), digits.end());
  19.     };
  20.  
  21.     BigInteger(string value) {
  22.         if (value[0] == '-') {
  23.             isNegative = true;
  24.             value = value.substr(1);
  25.         } else {
  26.             isNegative = false;
  27.         }
  28.         for (int i = (int)value.size() - 1; i >= 0; i--) {
  29.             digits.push_back(value[i] - '0');
  30.         }
  31.     }
  32.  
  33.     BigInteger(const BigInteger &value) {
  34.         digits = value.digits;
  35.         isNegative = value.isNegative;
  36.     };
  37.  
  38.     // Na potom
  39.     /*BigInteger(BigInteger &&value) {
  40.         digits = value.digits;
  41.         isNegative = value.isNegative;
  42.     };*/
  43.     // Na potom
  44.  
  45.     BigInteger& operator +=(const BigInteger &value) {
  46.         return *this = *this + value;
  47.     };
  48.  
  49.     BigInteger operator +(const BigInteger &valueTwo) const {
  50.         BigInteger returnValue;
  51.         int carryBit = 0;
  52.         for (int i = 0; i < min((int)digits.size(), (int)valueTwo.digits.size()); i++) {
  53.             int currentValue = digits[i] + valueTwo.digits[i] + carryBit;
  54.             carryBit = (int)(currentValue >= 10);
  55.             returnValue.digits.push_back(currentValue % 10);
  56.         }
  57.         if (digits.size() < valueTwo.digits.size()) {
  58.             for (int i = (int)digits.size(); i < (int)valueTwo.digits.size(); i++) {
  59.                 int currentValue = valueTwo.digits[i] + carryBit;
  60.                 carryBit = (int)(currentValue >= 10);
  61.                 returnValue.digits.push_back(currentValue % 10);
  62.             }
  63.             if (carryBit) {
  64.                 returnValue.digits.push_back(carryBit);
  65.             }
  66.         } else {
  67.             for (int i = (int)valueTwo.digits.size(); i < (int)digits.size(); i++) {
  68.                 int currentValue = digits[i] + carryBit;
  69.                 carryBit = (int)(currentValue >= 10);
  70.                 returnValue.digits.push_back(currentValue % 10);
  71.             }
  72.             if (carryBit) {
  73.                 returnValue.digits.push_back(carryBit);
  74.             }
  75.         }
  76.         return returnValue;
  77.  
  78.     };
  79.  
  80.     BigInteger& operator -=(const BigInteger &value) {
  81.         return *this = *this - value;
  82.     };
  83.     BigInteger operator -(const BigInteger &value) const {
  84.  
  85.     };
  86.  
  87. private:
  88.     friend std::ostream & operator <<(std::ostream &os, const BigInteger &value);
  89.     friend std::istream & operator >>(std::istream &is, BigInteger &value);
  90.  
  91.     vector<int> digits;
  92.     bool isNegative;
  93.  
  94. };
  95.  
  96. std::ostream & operator <<(std::ostream &os, const BigInteger &value) {
  97.     vector<int> currentDigits = value.digits;
  98.     reverse(currentDigits.begin(), currentDigits.end());
  99.     string s = "";
  100.     for (int i = 0; i < (int)currentDigits.size(); i++) {
  101.         s += ('0' + currentDigits[i]);
  102.     }
  103.     return os << s;
  104. };
  105. std::istream & operator >>(std::istream &is, BigInteger &value) {
  106.     string s;
  107.     is >> s;
  108.     value = BigInteger(s);
  109.     return is;
  110. };
  111.  
  112. int main() {
  113.     BigInteger x("-12");
  114.     cin >> x;
  115.     BigInteger y("34");
  116.     BigInteger z(x += y);
  117.  
  118.     cout << z;
  119.     return 0;
  120. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement