C++ examples for Class:Member Field
Create BigInteger class to use a 40-element array of digits to store integers as large as 40 digits each.
#include <iostream> #include <string> class BigInteger { public:/*from w w w. j a v a 2 s. com*/ BigInteger() {} explicit BigInteger(const std::string&); explicit BigInteger(int[]); // Arithmetic Operations BigInteger operator+(const BigInteger& hi); BigInteger operator-(const BigInteger& hi); // Predicate Operations bool isZero(); bool operator==(const BigInteger& hi) const; bool operator!=(const BigInteger& hi) const; bool operator<(const BigInteger& hi) const; bool operator>=(const BigInteger& hi) const; bool operator>(const BigInteger& hi) const; bool operator<=(const BigInteger& hi) const; void quickPrint(int*); // I/O Operations friend std::istream& operator>>(std::istream& in, BigInteger& hi) { std::string digit; in >> digit; hi.input(digit); return in; } friend std::ostream& operator<<(std::ostream& out, BigInteger& hi) { return hi.output(out); } void input(const std::string&); std::ostream& output(std::ostream&); // UTILITY FUNCTIONS int size() { return _hiSize; } private: static const size_t MAX_INTEGER = 41; const int TERMINATOR = -99; // allow for trailing zeros int _hugeInteger[MAX_INTEGER] = {0}; int _hiSize = 0; // number of digits void reverse(int*, int); }; BigInteger::BigInteger(const std::string& s) { input(s); } BigInteger::BigInteger(int hi[]) { for (unsigned int i = 0; i < MAX_INTEGER; ++i) { _hugeInteger[i] = hi[i]; if (hi[i] == TERMINATOR) { break; } ++_hiSize; } } // add two BigIntegers - result as new BigInteger BigInteger BigInteger::operator+(const BigInteger& hi) { // result + carry over values int newHuge[MAX_INTEGER] = {0}; int* ptrNewHuge = &newHuge[0]; int sum = 0; int addend1 = _hiSize - 1; int addend2 = hi._hiSize - 1; unsigned int count = 0; // iterate both and add each value while ((addend1 >= 0 || addend2 >= 0) && count < MAX_INTEGER) { if (addend1 < 0) { sum = *ptrNewHuge + hi._hugeInteger[addend2]; } else if (addend2 < 0) { sum = *ptrNewHuge + _hugeInteger[addend1]; } else { sum = *ptrNewHuge + _hugeInteger[addend1] + hi._hugeInteger[addend2]; } if (sum >= 10) { *(ptrNewHuge++) = sum % 10; *ptrNewHuge = sum / 10; } else { *(ptrNewHuge++) = sum; } --addend1; --addend2; ++count; } // decrease pointer if limit is reached if (count == MAX_INTEGER - 1) { --ptrNewHuge; // carry over value is present - increase counters // result array is reversed so no trailing zeros } else if (*ptrNewHuge != 0) { ++ptrNewHuge; count++; } reverse(newHuge, count); *ptrNewHuge = TERMINATOR; ptrNewHuge = nullptr; return BigInteger(newHuge); } // subtract hi from _hugeInteger result as new BigInteger // uses quick subtraction BigInteger BigInteger::operator-(const BigInteger& hi) { // result + carry over values int newHuge[MAX_INTEGER] = {0}; int* ptrNewHuge = &newHuge[0]; int minuend = _hiSize - 1; int subtrahend = hi._hiSize - 1; int difference = 0; unsigned int count = 0; while ((minuend >= 0 || subtrahend >= 0) && (count < MAX_INTEGER)) { if (minuend < 0) { difference = 0 - (*ptrNewHuge + hi._hugeInteger[subtrahend]); } else if (subtrahend < 0) { difference = _hugeInteger[minuend] - *ptrNewHuge; } else { difference = _hugeInteger[minuend] - (hi._hugeInteger[subtrahend] + *ptrNewHuge); } if (difference < 0) { *ptrNewHuge = 10 - ((hi._hugeInteger[subtrahend] + *ptrNewHuge) - _hugeInteger[minuend]); *(++ptrNewHuge) = 1; } else { *(ptrNewHuge++) = difference; } --minuend; --subtrahend; ++count; } ptrNewHuge = nullptr; count = MAX_INTEGER - 1; // remove trailing zeros while (newHuge[count] == 0 && count > 0) { --count; } reverse(newHuge, count + 1); newHuge[count + 1] = TERMINATOR; return BigInteger(newHuge); } // ===== PREDICATE OPERATIONS ===== // bool BigInteger::isZero() { return (_hiSize == 0); } bool BigInteger::operator==(const BigInteger& hi) const { for (unsigned int i = 0; i < MAX_INTEGER; ++i) { if (_hugeInteger[i] != hi._hugeInteger[i]) { return false; } } return true; } bool BigInteger::operator!=(const BigInteger& hi) const { return !(*this == hi); } bool BigInteger::operator>(const BigInteger& hi) const { if (_hiSize > hi._hiSize) { return true; } // counts equal compare digits if (_hiSize == hi._hiSize) { for (int i = 0; i < _hiSize; ++i) { if (_hugeInteger[i] > hi._hugeInteger[i]) { return true; } } } return false; } bool BigInteger::operator>=(const BigInteger& hi) const { return (*this == hi || *this > hi); } bool BigInteger::operator<(const BigInteger& hi) const { if (_hiSize < hi._hiSize) { return true; } // counts equal compare digits if (_hiSize == hi._hiSize) { for (int i = 0; i < _hiSize; ++i) { if (_hugeInteger[i] < hi._hugeInteger[i]) { return true; } } } return false; } bool BigInteger::operator<=(const BigInteger& hi) const { return (*this == hi || *this < hi); } // ===== I/O OPERATIONS ===== // void BigInteger::input(const std::string& digit) { int* ptrBigInteger = &_hugeInteger[0]; for (unsigned int i = 0; i < digit.size(); ++i) { if (i < MAX_INTEGER) { *(ptrBigInteger++) = static_cast<int>(digit[i]) - 48; } else { break; } ++_hiSize; } *ptrBigInteger = TERMINATOR; ptrBigInteger = nullptr; } std::ostream& BigInteger::output(std::ostream& out) { for (int i = 0; i < _hiSize; ++i) { if (_hugeInteger[i] == TERMINATOR) { break; } out << _hugeInteger[i]; } return out; } // reverses a given int array void BigInteger::reverse(int* arr, int count) { // reverse the final value for (int i = 0; i < count / 2; ++i) { int temp = arr[count - i - 1]; arr[count - i - 1] = arr[i]; arr[i] = temp; } } // print every element in array void BigInteger::quickPrint(int* a) { printf("\n"); for (unsigned int i = 0; i < MAX_INTEGER; ++i) { std::cout << a[i]; } std::cout << std::endl; } int main(int argc, const char *argv[]) { BigInteger h1("123456"); BigInteger h2("123456"); BigInteger h3p = (h1 + h2); BigInteger h3m = (h1 - h2); std::cout << h1 << " + " << h2 << " = " << h3p << "\n"; std::cout << h1 << " - " << h2 << " = " << h3m << "\n"; std::cout << "h1 < h2: " << ((h1 < h2) ? "True" : "False") << "\n"; std::cout << "h1 > h2: " << ((h1 > h2) ? "True" : "False") << "\n"; std::cout << "h1 == h2: " << ((h1 == h2) ? "True" : "False") << "\n"; std::cout << "h1 != h2: " << ((h1 != h2) ? "True" : "False") << "\n"; std::cout << "h1 >= h2: " << ((h1 >= h2) ? "True" : "False") << "\n"; std::cout << "h1 <= h2: " << ((h1 <= h2) ? "True" : "False") << "\n\n"; BigInteger h4("923456"); BigInteger h5("923456"); BigInteger h6p = (h4 + h5); BigInteger h6m = (h4 - h5); std::cout << h4 << " + " << h5 << " = " << h6p << "\n"; std::cout << h4 << " - " << h5 << " = " << h6m << "\n"; std::cout << "h4 < h5: " << ((h4 < h5) ? "True" : "False") << "\n"; std::cout << "h4 > h5: " << ((h4 > h5) ? "True" : "False") << "\n"; std::cout << "h4 == h5: " << ((h4 == h5) ? "True" : "False") << "\n"; std::cout << "h4 != h5: " << ((h4 != h5) ? "True" : "False") << "\n"; std::cout << "h4 >= h5: " << ((h4 >= h5) ? "True" : "False") << "\n"; std::cout << "h4 <= h5: " << ((h4 <= h5) ? "True" : "False") << "\n\n"; BigInteger h7("1"); BigInteger h8("99999123999999214123123123123312312398"); BigInteger h9p = (h7 + h8); BigInteger h9m = (h8 - h7); std::cout << h7 << " + " << h8 << " = " << h9p << "\n"; std::cout << h8 << " - " << h7 << " = " << h9m << "\n"; std::cout << "h7 < h8: " << ((h7 < h8) ? "True" : "False") << "\n"; std::cout << "h7 > h8: " << ((h7 > h8) ? "True" : "False") << "\n"; std::cout << "h7 == h8: " << ((h7 == h8) ? "True" : "False") << "\n"; std::cout << "h7 != h8: " << ((h7 != h8) ? "True" : "False") << "\n"; std::cout << "h7 >= h8: " << ((h7 >= h8) ? "True" : "False") << "\n"; std::cout << "h7 <= h8: " << ((h7 <= h8) ? "True" : "False") << "\n\n"; BigInteger h10; std::cout << "h10.isZero(): " << ((h10.isZero()) ? "True" : "False") << "\n"; BigInteger h11("12345"); BigInteger h12("11726"); BigInteger h13m = (h11 - h12); std::cout << h11 << " - " << h12 << " = " << h13m << "\n"; BigInteger h14; BigInteger h15; std::cout << "Enter BigInteger 1 :"; std::cin >> h14; std::cout << "\nEnter BigInteger 2: "; std::cin >> h15; BigInteger h16 = (h14 + h15); std::cout << "h14 + h16 = " << h16 << "\n"; BigInteger h17 = (h14 - h15); std::cout << "h14 - h15 = " << h17 << "\n"; std::cout << "h17.size() = " << h17.size() << std::endl; return 0; }