参考GPT和自己的思路,以下是int20的头文件和源文件实现:
头文件:int20.h:
#ifndef INT20_H
#define INT20_H
#include <iostream>
namespace std {
class int20 {
friend std::ostream& operator<<(std::ostream& os, const int20& num);
friend std::istream& operator>>(std::istream& is, int20& num);
public:
int20();
int20(int num);
int20 operator+(const int20& other) const;
int20 operator-(const int20& other) const;
int20 operator*(const int20& other) const;
int20 operator/(const int20& other) const;
int20 operator%(const int20& other) const;
int20& operator++();
int20 operator++(int);
int20& operator--();
int20 operator--(int);
private:
int digits[20];
bool is_negative;
};
}
#endif // INT20_H
源文件:int20.cpp:
#include "int20.h"
namespace std {
int20::int20() {
for (int i = 0; i < 20; i++) {
digits[i] = 0;
}
is_negative = false;
}
int20::int20(int num) {
if (num < 0) {
is_negative = true;
num = -num;
} else {
is_negative = false;
}
for (int i = 0; i < 20; i++) {
digits[i] = num % 10;
num /= 10;
}
}
int20 int20::operator+(const int20& other) const {
int20 result;
int carry = 0;
for (int i = 0; i < 20; i++) {
int sum = digits[i] + other.digits[i] + carry;
result.digits[i] = sum % 10;
carry = sum / 10;
}
result.is_negative = (is_negative && other.is_negative);
return result;
}
int20 int20::operator-(const int20& other) const {
int20 result;
int borrow = 0;
for (int i = 0; i < 20; i++) {
int diff = digits[i] - other.digits[i] - borrow;
if (diff < 0) {
diff += 10;
borrow = 1;
} else {
borrow = 0;
}
result.digits[i] = diff;
}
if (is_negative && other.is_negative) {
result.is_negative = !result.is_negative;
} else if (is_negative && !other.is_negative) {
result.is_negative = true;
} else if (!is_negative && other.is_negative) {
result.is_negative = false;
} else {
int cmp = (*this) < other;
if (cmp == 0) {
result.is_negative = false;
} else if (cmp < 0) {
result.is_negative = true;
} else {
result.is_negative = false;
}
}
return result;
}
int20 int20::operator*(const int20& other) const {
int20 result;
for (int i = 0; i < 20; i++) {
for (int j = 0; j < 20; j++) {
int k = i + j;
int product = digits[i] * other.digits[j];
result.digits[k] += product % 10;
result.digits[k+1] += product / 10;
}
}
for (int i = 0; i < 19; i++) {
result.digits[i+1] += result.digits[i] / 10;
result.digits[i] %= 10;
}
result.is_negative = (is_negative != other.is_negative);
return result;
}
int20 int20::operator/(const int20& other) const {
int20 result;
int20 dividend(*this);
int20 divisor(other);
dividend.is_negative = false;
divisor.is_negative = false;
if (dividend < divisor) {
return result;
}
int20 quotient;
for (int i = 19; i >= 0; i--) {
quotient = quotient * 10;
int digit = 0;
while (dividend >= divisor) {
dividend = dividend - divisor;
digit++;
}
quotient = quotient + digit;
divisor = divisor / 10;
}
result = quotient;
result.is_negative = (is_negative != other.is_negative);
return result;
}
int20 int20::operator%(const int20& other) const {
int20 dividend(*this);
int20 divisor(other);
dividend.is_negative = false;
divisor.is_negative = false;
if (dividend < divisor) {
return dividend;
}
for (int i = 19; i >= 0; i--) {
while (dividend >= divisor) {
dividend = dividend - divisor;
}
divisor = divisor / 10;
}
dividend.is_negative = is_negative;
return dividend;
}
int20& int20::operator++() {
*this = *this + 1;
return *this;
}
int20 int20::operator++(int) {
int20 old_value(*this);
*this = *this + 1;
return old_value;
}
int20& int20::operator--() {
*this = *this - 1;
return *this;
}
int20 int20::operator--(int) {
int20 old_value(*this);
*this = *this - 1;
return old_value;
}
std::ostream& operator<<(std::ostream& os, const int20& num) {
if (num.is_negative) {
os << '-';
}
bool leading_zero = true;
for (int i = 19; i >= 0; i--) {
if (num.digits[i] != 0) {
leading_zero = false;
}
if (!leading_zero) {
os << num.digits[i];
}
}
if (leading_zero) {
os << '0';
}
return os;
}
std::istream& operator>>(std::istream& is, int20& num) {
std::string input;
is >> input;
int len = input.length();
num = int20();
int pos = 0;
if (input[pos] == '-') {
num.is_negative = true;
pos++;
}
while (pos < len) {
num = num * 10 + (input[pos] - '0');
pos++;
}
return is;
}
然后,在main.cpp中,我们可以这样使用int20类:
#include "int20.h"
int main
() {
int20 first, second;
std::cin >> first >> second;
long long sum1 = 0 ,sum2 = 0;
for (long long loop = 0; loop < 1048560; loop += 1)
{
first++;
second--;
sum1 += first;
sum2 -= second;
}
std::cout << first + second << ' ' << sum1 << ' ' << sum2 << std::endl;
return 0;
}
这个测试程序会从标准输入中读取两个int20类型的整数,然后对它们进行1048560次加减运算,并统计结果的和。最后输出两个数之和,sum1和sum2的值。
回答不易,还请采纳!!!