Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- using namespace std;
- class BigInteger {
- public:
- explicit BigInteger(long long value = 0) {
- if (value < 0) {
- isNegative = true;
- } else {
- isNegative = false;
- }
- while (value > 0) {
- digits.push_back(value % 10);
- value /= 10;
- }
- // reverse(digits.begin(), digits.end());
- };
- BigInteger(string value) {
- if (value[0] == '-') {
- isNegative = true;
- value = value.substr(1);
- } else {
- isNegative = false;
- }
- for (int i = (int)value.size() - 1; i >= 0; i--) {
- digits.push_back(value[i] - '0');
- }
- }
- BigInteger(const BigInteger &value) {
- digits = value.digits;
- isNegative = value.isNegative;
- };
- // Na potom
- /*BigInteger(BigInteger &&value) {
- digits = value.digits;
- isNegative = value.isNegative;
- };*/
- // Na potom
- BigInteger& operator +=(const BigInteger &value) {
- return *this = *this + value;
- };
- BigInteger operator +(const BigInteger &valueTwo) const {
- BigInteger returnValue;
- int carryBit = 0;
- for (int i = 0; i < min((int)digits.size(), (int)valueTwo.digits.size()); i++) {
- int currentValue = digits[i] + valueTwo.digits[i] + carryBit;
- carryBit = (int)(currentValue >= 10);
- returnValue.digits.push_back(currentValue % 10);
- }
- if (digits.size() < valueTwo.digits.size()) {
- for (int i = (int)digits.size(); i < (int)valueTwo.digits.size(); i++) {
- int currentValue = valueTwo.digits[i] + carryBit;
- carryBit = (int)(currentValue >= 10);
- returnValue.digits.push_back(currentValue % 10);
- }
- if (carryBit) {
- returnValue.digits.push_back(carryBit);
- }
- } else {
- for (int i = (int)valueTwo.digits.size(); i < (int)digits.size(); i++) {
- int currentValue = digits[i] + carryBit;
- carryBit = (int)(currentValue >= 10);
- returnValue.digits.push_back(currentValue % 10);
- }
- if (carryBit) {
- returnValue.digits.push_back(carryBit);
- }
- }
- return returnValue;
- };
- BigInteger& operator -=(const BigInteger &value) {
- return *this = *this - value;
- };
- BigInteger operator -(const BigInteger &value) const {
- };
- private:
- friend std::ostream & operator <<(std::ostream &os, const BigInteger &value);
- friend std::istream & operator >>(std::istream &is, BigInteger &value);
- vector<int> digits;
- bool isNegative;
- };
- std::ostream & operator <<(std::ostream &os, const BigInteger &value) {
- vector<int> currentDigits = value.digits;
- reverse(currentDigits.begin(), currentDigits.end());
- string s = "";
- for (int i = 0; i < (int)currentDigits.size(); i++) {
- s += ('0' + currentDigits[i]);
- }
- return os << s;
- };
- std::istream & operator >>(std::istream &is, BigInteger &value) {
- string s;
- is >> s;
- value = BigInteger(s);
- return is;
- };
- int main() {
- BigInteger x("-12");
- cin >> x;
- BigInteger y("34");
- BigInteger z(x += y);
- cout << z;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement