o1-mini-2024-09-12· OpenAI
WildChat-4.8M (AI2)
Realize big int class with all operations on C++ without comments.
- User
Realize big int class with all operations on C++ without comments.
- Assistant
Realize big int class with all operations on C++ without comments.
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <iomanip>
class BigInt {
private:
std::string digits;
bool negative;
void removeLeadingZeros() {
while (digits.size() > 1 && digits.back() == '0')
digits.pop_back();
if (digits.size() == 1 && digits[0] == '0')
negative = false;
}
static int compareAbs(const BigInt& a, const BigInt& b) {
if (a.digits.size() != b.digits.size())
return a.digits.size() > b.digits.size() ? 1 : -1;
for (int i = a.digits.size() - 1; i >= 0; --i) {
if (a.digits[i] != b.digits[i])
return a.digits[i] > b.digits[i] ? 1 : -1;
}
return 0;
}
static BigInt addAbs(const BigInt& a, const BigInt& b) {
BigInt result;
result.digits.clear();
int carry = 0;
size_t n = std::max(a.digits.size(), b.digits.size());
for (size_t i = 0; i < n || carry; ++i) {
int digit = carry;
if (i < a.digits.size()) digit += a.digits[i] - '0';
if (i < b.digits.size()) digit += b.digits[i] - '0';
carry = digit / 10;
result.digits += (digit % 10) + '0';
}
result.negative = false;
return result;
}
static BigInt subtractAbs(const BigInt& a, const BigInt& b) {
BigInt result;
result.digits.clear();
int carry = 0;
for (size_t i = 0; i < a.digits.size(); ++i) {
int digit = a.digits[i] - '0' - carry;
if (i < b.digits.size()) digit -= (b.digits[i] - '0');
if (digit < 0) {
digit += 10;
carry = 1;
}
else {
carry = 0;
}
result.digits += digit + '0';
}
result.negative = false;
result.removeLeadingZeros();
return result;
}
public:
BigInt() : digits("0"), negative(false) {}
BigInt(const std::string& s) {
if (s.empty()) {
digits = "0";
negative = false;
return;
}
size_t start = 0;
negative = false;
if (s[0] == '-') {
negative = true;
start = 1;
}
digits = "";
for (size_t i = s.size(); i > start; --i)
digits += s[i - 1];
removeLeadingZeros();
}
BigInt(long long num) {
negative = num < 0;
digits = "";
long long n = negative ? -num : num;
if (n == 0) digits = "0";
while (n > 0) {
digits += (n % 10) + '0';
n /= 10;
}
}
friend std::ostream& operator<<(std::ostream& os, const BigInt& n) {
if (n.negative) os << '-';
for (int i = n.digits.size() - 1; i >= 0; --i)
os << n.digits[i];
return os;
}
friend BigInt operator+(const BigInt& a, const BigInt& b) {
BigInt result;
if (a.negative == b.negative) {
result = addAbs(a, b);
result.negative = a.negative;
}
else {
if (compareAbs(a, b) >= 0) {
result = subtractAbs(a, b);
result.negative = a.negative;
}
else {
result = subtractAbs(b, a);
result.negative = b.negative;
}
}
result.removeLeadingZeros();
return result;
}
friend BigInt operator-(const BigInt& a, const BigInt& b) {
BigInt negB = b;
negB.negative = !b.negative;
return a + negB;
}
friend BigInt operator*(const BigInt& a, const BigInt& b) {
BigInt result;
result.digits.assign(a.digits.size() + b.digits.size(), '0');
for (size_t i = 0; i < a.digits.size(); ++i) {
int carry = 0;
int n1 = a.digits[i] - '0';
for (size_t j = 0; j < b.digits.size() || carry; ++j) {
int n2 = (j < b.digits.size()) ? (b.digits[j] - '0') : 0;
int sum = (result.digits[i + j] - '0') + n1 * n2 + carry;
carry = sum / 10;
result.digits[i + j] = (sum % 10) + '0';
}
}
result.negative = a.negative != b.negative;
result.removeLeadingZeros();
return result;
}
friend BigInt operator/(const BigInt& a, const BigInt& b) {
if (b.digits.size() == 1 && b.digits[0] == '0') throw std::invalid_argument("Division by zero");
BigInt dividend = a;
BigInt divisor = b;
dividend.negative = false;
divisor.negative = false;
if (compareAbs(dividend, divisor) < 0) return BigInt("0");
BigInt result;
result.digits = "";
BigInt current;
for (int i = dividend.digits.size() - 1; i >= 0; --i) {
current.digits.insert(current.digits.begin(), dividend.digits[i]);
current.removeLeadingZeros();
int x = 0, l = 0, r = 10;
while (l <= r) {
int m = (l + r) / 2;
BigInt t = divisor * BigInt(std::to_string(m));
if (compareAbs(t, current) <= 0) {
x = m;
l = m + 1;
}
else {
r = m - 1;
}
}
result.digits += (x + '0');
current = current - divisor * BigInt(std::to_string(x));
}
std::reverse(result.digits.begin(), result.digits.end());
result.negative = a.negative != b.negative;
result.removeLeadingZeros();
return result;
}
bool operator<(const BigInt& other) const {
if (negative != other.negative) return negative;
int cmp = compareAbs(*this, other);
return negative ? cmp > 0 : cmp < 0;
}
bool operator>(const BigInt& other) const {
return other < *this;
}
bool operator<=(const BigInt& other) const {
return !(other < *this);
}
bool operator>=(const BigInt& other) const {
return !(*this < other);
}
bool operator==(const BigInt& other) const {
return negative == other.negative && digits == other.digits;
}
bool operator!=(const BigInt& other) const {
return !(*this == other);
}
};
int main(){
BigInt a("123456789012345678901234567890");
BigInt b("987654321098765432109876543210");
std::cout << "a + b = " << a + b << std::endl;
std::cout << "b - a = " << b - a << std::endl;
std::cout << "a * b = " << a * b << std::endl;
std::cout << "b / a = " << b / a << std::endl;
std::cout << "a < b: " << (a < b) << std::endl;
std::cout << "a == b: " << (a == b) << std::endl;
return 0;
}