// Sample class for string an integer with an arbitrary number of digits. This // version includes a member function to get the number of digits and an // overloaded [] operator for accessing/changing individual digits of the // number #include #include #include #include #include #include using namespace std; class big_integer { public: big_integer(int n = 0) { negative = n < 0; n = abs(n); digits.push_back(n % 10); n /= 10; while (n != 0) { digits.push_back(n % 10); n /= 10; } } big_integer(string s) { negative = s[0] == '-'; if (negative) { s = s.substr(1); } for (int i = s.size() - 1; i >= 0; i--) { digits.push_back(s[i] - '0'); } } string to_string() const { ostringstream out; if (negative) { out << "-"; } for (int i = digits.size() - 1; i >= 0; i--) { out << digits[i]; } return out.str(); } // pre : n < INT_MAX / 10 (to avoid integer overflow) big_integer & operator*=(int n) { if (n >= INT_MAX / 10) { throw runtime_error("int is too big"); } if (n < 0) { n = abs(n); negative = !negative; } int carry = 0; for (int & digit : digits) { digit = digit * n + carry; carry = digit / 10; digit = digit % 10; } while (carry > 0) { digits.push_back(carry % 10); carry /= 10; } return *this; } bool is_negative() const { return negative; } int number_of_digits() const { return digits.size(); } int & operator[](int index) { if (index >= digits.size() || index < 0) { throw out_of_range("index for [] is out of range"); } return digits[digits.size() - index]; } private: vector digits; bool negative; }; bool operator==(const big_integer & lhs, const big_integer & rhs) { return lhs.to_string() == rhs.to_string(); } bool operator!=(const big_integer & lhs, const big_integer & rhs) { return !(lhs == rhs); } ostream & operator<<(ostream & out, const big_integer & big) { out << big.to_string(); return out; } istream & operator>>(istream & in, big_integer & big) { string s; in >> s; big = big_integer(s); return in; } bool operator<(const big_integer & lhs, const big_integer & rhs) { if (lhs.is_negative() != rhs.is_negative()) { return lhs.is_negative(); } else { string left_string = lhs.to_string(); string right_string = rhs.to_string(); bool result; if (left_string.size() != right_string.size()) { result = left_string.size() < right_string.size(); } else { result = left_string < right_string; } if (lhs.is_negative()) { result = !result; } return result; } } int main() { big_integer x(1234); big_integer y(49); big_integer z("657894365382796543289"); big_integer a("1234"); cout << "give me a big int: "; big_integer b; cin >> b; cout << "x = " << x << endl; cout << "y = " << y << endl; cout << "z = " << z << endl; cout << "a = " << a << endl; cout << "b = " << b << endl; cout << endl; cout << "x == y returns " << (x == y) << endl; cout << "x == a returns " << (x == a) << endl; cout << "x == x returns " << (x == x) << endl; cout << "x == z returns " << (x == z) << endl; cout << "x == 1234 returns " << (x == 1234) << endl; cout << "1234 == x returns " << (1234 == x) << endl; cout << endl; big_integer fact(1); for (int i = 1; i <= 30; i++) { fact *= i; cout << i << "! = " << fact << endl; } cout << endl; big_integer c(-12345); big_integer d("-345"); cout << "c = " << c << ", d = " << d << endl; cout << endl; c *= -2; d *= -3; cout << "c = " << c << ", d = " << d << endl; cout << endl; d = c *= 2; cout << "c = " << c << ", d = " << d << endl; cout << endl; vector numbers; numbers.push_back(3089); numbers.push_back(42); numbers.push_back(308); numbers.push_back(63234); numbers.push_back(-15); numbers.push_back(0); numbers.push_back(-6823); numbers.push_back(-6822); numbers.push_back(299); numbers.push_back(-308); sort(numbers.begin(), numbers.end()); cout << "sorted list:"; for (const big_integer & n : numbers) { cout << " " << n; } cout << endl; cout << endl; big_integer n("12345678"); cout << "# of digits in " << n << " = " << n.number_of_digits() << endl; cout << "n[2] = " << n[2] << endl; n[2] = 0; n[6] = 0; cout << "after resetting n[2] and n[6], n = " << n << endl; return 0; }