如何使用C++设计一种数据类型, 名称为int20。 仅限于3.12号完成

使用C++设计一种数据类型, 名称为int20。名思义它是一个20(16+4)位的int整形,要求使用类来设计,位于std命名空间,重载加减乘除取余输入输出等基本运算,使用方法与int无异。
要求提交一个头文件以及源文件,头文件中包括类的声明以及功能注释,源文件中包含方法实现等。我们的测试将如下,新建一个main.cpp文件写入以下内容,将提交的头文件与源文件同时放入同级目录之后运行。


```c++
#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;
}

```

参考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的值。
回答不易,还请采纳!!!

这是老师在布置作业吗?

参考gpt和自己的思路,以下是头文件 int20.h 的实现:


#ifndef INT20_H
#define INT20_H

#include <iostream>

namespace std {

    class int20 {
    private:
        unsigned int data[1];

    public:
        int20() {
            data[0] = 0;
        }

        int20(unsigned int num) {
            data[0] = num;
        }

        int20(const int20& other) {
            data[0] = other.data[0];
        }

        // Overloaded operators
        int20 operator+ (const int20& other);
        int20 operator- (const int20& other);
        int20 operator* (const int20& other);
        int20 operator/ (const int20& other);
        int20 operator% (const int20& other);
        int20& operator= (const int20& other);
        int20& operator+= (const int20& other);
        int20& operator-= (const int20& other);
        int20& operator*= (const int20& other);
        int20& operator/= (const int20& other);
        int20& operator%= (const int20& other);
        int20& operator++ ();
        int20 operator++ (int);
        int20& operator-- ();
        int20 operator-- (int);
        bool operator== (const int20& other);
        bool operator!= (const int20& other);
        bool operator< (const int20& other);
        bool operator> (const int20& other);
        bool operator<= (const int20& other);
        bool operator>= (const int20& other);
        int20 operator~ ();
        int20 operator& (const int20& other);
        int20 operator| (const int20& other);
        int20 operator^ (const int20& other);
        int20 operator<< (unsigned int n);
        int20 operator>> (unsigned int n);
        int20& operator<<= (unsigned int n);
        int20& operator>>= (unsigned int n);
        int20 operator- () const;

        // Friend functions for input and output
        friend std::ostream& operator<< (std::ostream& os, const int20& num);
        friend std::istream& operator>> (std::istream& is, int20& num);
    };

}

#endif


以下是源文件 int20.cpp 的实现:


#include "int20.h"

#include <cmath>

namespace std {

    // Overloaded operators
    int20 int20::operator+ (const int20& other) {
        int20 result(data[0] + other.data[0]);
        return result;
    }

    int20 int20::operator- (const int20& other) {
        int20 result(data[0] - other.data[0]);
        return result;
    }

    int20 int20::operator* (const int20& other) {
        int20 result(data[0] * other.data[0]);
        return result;
    }

    int20 int20::operator/ (const int20& other) {
        int20 result(data[0] / other.data[0]);
        return result;
    }

    int20 int20::operator% (const int20& other) {
        int20 result(data[0] % other.data[0]);
        return result;
    }

    int20& int20::operator= (const int20& other) {
        data[0] = other.data[0];
        return *this;
    }

    int20& int20::operator+= (const int20& other) {
    data[0] += other.data[0];
    return *this;
}

int20& int20::operator-= (const int20& other) {
    data[0] -= other.data[0];
    return *this;
}

int20& int20::operator*= (const int20& other) {
    data[0] *= other.data[0];
    return *this;
}

int20& int20::operator/= (const int20& other) {
    data[0] /= other.data[0];
    return *this;
}

int20& int20::operator%= (const int20& other) {
    data[0] %= other.data[0];
    return *this;
}

int20& int20::operator++ () {
    ++data[0];
    return *this;
}

int20 int20::operator++ (int) {
    int20 result(*this);
    ++data[0];
    return result;
}

int20& int20::operator-- () {
    --data[0];
    return *this;
}

int20 int20::operator-- (int) {
    int20 result(*this);
    --data[0];
    return result;
}

bool int20::operator== (const int20& other) {
    return data[0] == other.data[0];
}

bool int20::operator!= (const int20& other) {
    return data[0] != other.data[0];
}

bool int20::operator< (const int20& other) {
    return data[0] < other.data[0];
}

bool int20::operator> (const int20& other) {
    return data[0] > other.data[0];
}

bool int20::operator<= (const int20& other) {
    return data[0] <= other.data[0];
}

bool int20::operator>= (const int20& other) {
    return data[0] >= other.data[0];
}

int20 int20::operator~ () {
    int20 result(~data[0]);
    return result;
}

int20 int20::operator& (const int20& other) {
    int20 result(data[0] & other.data[0]);
    return result;
}

int20 int20::operator| (const int20& other) {
    int20 result(data[0] | other.data[0]);
    return result;
}

int20 int20::operator^ (const int20& other) {
    int20 result(data[0] ^ other.data[0]);
    return result;
}

int20 int20::operator<< (unsigned int n) {
    int20 result(data[0] << n);
    return result;
}

int20 int20::operator>> (unsigned int n) {
    int20 result(data[0] >> n);
    return result;
}

int20& int20::operator<<= (unsigned int n) {
    data[0] <<= n;
    return *this;
}

int20& int20::operator>>= (unsigned int n) {
    data[0] >>= n;
    return *this;
}

int20 int20::operator- () const {
    int20 result(-data[0]);
    return result;
}

std::ostream& operator<< (std::ostream& os, const int20& num) {
    os << num.data[0];
    return os;

}

std::istream& operator>> (std::istream& is, int20& num) {
    is >> num.data[0];
    return is;
}

int20 int20::operator+ (const int20& other) const {
    int20 result(*this);
    result += other;
    return result;
}

int20 int20::operator- (const int20& other) const {
    int20 result(*this);
    result -= other;
    return result;
}

int20 int20::operator* (const int20& other) const {
    int20 result(*this);
    result *= other;
    return result;
}

int20 int20::operator/ (const int20& other) const {
    int20 result(*this);
    result /= other;
    return result;
}

int20 int20::operator% (const int20& other) const {
    int20 result(*this);
    result %= other;
    return result;
}

int20 int20::operator&= (const int20& other) {
    data[0] &= other.data[0];
    return *this;
}

int20 int20::operator|= (const int20& other) {
    data[0] |= other.data[0];
    return *this;
}

int20 int20::operator^= (const int20& other) {
    data[0] ^= other.data[0];
    return *this;
}

bool int20::operator! () const {
    return !data[0];
}

int20 int20::operator&& (const int20& other) const {
    return int20(data[0] && other.data[0]);
}

int20 int20::operator|| (const int20& other) const {
    return int20(data[0] || other.data[0]);
}

下面是完整的 int20.h 和 int20.cpp 文件。

int20.h:

#pragma once

#include <iostream>

namespace std {

    class int20 {
    public:
        // 构造函数
        int20(int num = 0);

        // 赋值运算符重载
        int20& operator=(const int20& num);

        // 加减乘除运算符重载
        int20 operator+(const int20& num) const;
        int20 operator-(const int20& num) const;
        int20 operator*(const int20& num) const;
        int20 operator/(const int20& num) const;

        // 取余运算符重载
        int20 operator%(const int20& num) const;

        // 前缀自增、自减运算符重载
        int20& operator++();
        int20& operator--();

        // 后缀自增、自减运算符重载
        int20 operator++(int);
        int20 operator--(int);

        // 输入输出运算符重载
        friend std::istream& operator>>(std::istream& in, int20& num);
        friend std::ostream& operator<<(std::ostream& out, const int20& num);

    private:
        // 存储整数的数组
        int num[5];

        // 获取 num[i] 的二进制第 j 位(从右往左数)
        bool getBit(int i, int j) const;

        // 设置 num[i] 的二进制第 j 位(从右往左数)
        void setBit(int i, int j, bool val);

        // 获取 num 的二进制位数
        int bitCount() const;
    };

}

int20.cpp:

```c++
#include "int20.h"

#include
#include

namespace std {

// 构造函数
int20::int20(int num)
{
    memset(this->num, 0, sizeof(this->num));
    if (num == 0) {
        return;
    }
    int i = 0;
    while (num != 0 && i < 5) {
        this->num[i] = num & 0xffff;
        num >>= 16;
        i++;
    }
}

// 赋值运算符重载
int20& int20::operator=(const int20& num)
{
    memcpy(this->num, num.num, sizeof(this->num));
    return *this;
}

// 加法运算符重载
int20 int20::operator+(const int20& num) const
{
    int20 res;
    int carry = 0;
    for (int i = 0; i < 5; i++) {
        res.num[i] = this->num[i] + num.num[i] + carry;
        carry = (res.num[i] >> 16) & 1;
        res.num[i] &= 0xffff;
    }
    return res;
}

// 减法运算符重载
int20 int20::operator-(const int20& num) const
{
    int20 res;
    int borrow = 0;
    for (int i = 0; i < 5; i++) {
        res.num[i] = this->num[i] - num.num[i] - borrow;
        borrow = (res.num[i] >> 31) & 1;
        res.num[i] &= 0xffff;
    }
    return res;
}

// 乘法运算符重载
int20 int20::operator*(const int20& num) const
{
    int20 res;
    for (int i = 0; i < 5; i++) {
        if (this->num[i] == 0) {
            continue;
        }
        int carry = 0;
        for (int j = 0; j < 5; j++) {
            long long cur = (long long)this->num[i] * num.num[j] + res.num[i+j] + carry;
            res.num[i+j] = cur & 0xffff;
            carry = cur >> 16;
        }
    }
    return res;
}

// 除法运算符重载
int20 int20::operator/(const int20& num) const
{
    int20 res, remainder(*this);
    for (int i = 79; i >= 0; i--) {
        // 将余数左移一位,加上最高位
        remainder = remainder << 1;
        remainder.setBit(0, 0, this->getBit(i, 0));
        if (remainder >= num) {
            remainder = remainder - num;
            res.setBit(i, 0, 1);
        }
    }
    return res;
}

// 取余运算符重载
int20 int20::operator%(const int20& num) const
{
    int20 res, remainder(*this);
    for (int i = 79; i >= 0; i--) {
        remainder = remainder << 1;
        remainder.setBit(0, 0, this->getBit(i, 0));
        if (remainder >= num) {
            remainder = remainder - num;
        }
    }
    return remainder;
}

// 前缀自增运算符重载
int20& int20::operator++()
{
    *this = *this + 1;
    return *this;
}

// 后缀自增运算符重载
int20 int20::operator++(int)
{
    int20 res(*this);
    *this = *this + 1;
    return res;
}

// 前缀自减运算符重载
int20& int20::operator--()
{
    *this = *this - 1;
    return *this;
}

// 后缀自减运算符重载
int20 int20::operator--(int)
{
    int20 res(*this);
    *this = *this - 1;
    return res;
}

// 输入运算符重载
std::istream& operator>>(std::istream& in, int20& num)
{
    std::string s;
    in >> s;
    int len = s.length();
    for (int i = len-1, j = 

以下答案由GPT-3.5大模型与博主波罗歌共同编写:
设计思路:

为了实现一个像int一样的数据类型,我们可以使用类来实现。对于int,它可以被视为一个32位的二进制数。对于int20,它应该是一个20位的二进制数。我们可以使用一个unsigned int类型的成员变量来存储这个数。但是,要提供合理的加、减、乘、除、取余等操作,我们需要深入了解这些运算符的执行方式。下面分别分析这些操作的实现方法:

  1. 输入和输出:

重载输入输出运算符,本质上是对<< 和 >> 进行了重载。因此我们可以通过std::ostream和std::istream来实现。

  1. 加、减:

加减运算的实现相对比较简单,只需要将两个int20类型的对象的unsigned int成员变量进行加减运算即可。

  1. 乘:

乘法的实现需要考虑到20位的二进制数将会在运算时产生的进位影响,可以使用标准的竖式乘法。

  1. 除和取余:

除法和取余的实现较为困难。我们可以使用移位运算实现除法,对于取余,只需要实现一下竖式除法,将得到的余数返回即可。

实现:

结合上述设计思路,我们可以完成类int20的定义。以下是头文件int20.h的实现:

#ifndef _INT20_H_
#define _INT20_H_

#include <iostream>

namespace std {

    class int20 {
    private:
        unsigned int value;
    public:
        int20() : value(0) {}
        int20(const int& num);
        friend std::ostream& operator<<(std::ostream& os, const int20& num);
        friend std::istream& operator>>(std::istream& is, int20& num);
        friend int20 operator+(const int20& num1, const int20& num2);
        friend int20 operator-(const int20& num1, const int20& num2);
        friend int20 operator*(const int20& num1, const int20& num2);
        friend int20 operator/(const int20& num1, const int20& num2);
        friend int20 operator%(const int20& num1, const int20& num2);
        int20 operator++(); // ++n
        const int20 operator++(int); // n++
        int20 operator--(); // --n
        const int20 operator--(int); // n--
    };

}

#endif

在int20.h的实现中,我们声明了一个std命名空间下的类int20,并提供了构造函数和运算符重载等必要的成员函数。

其中,构造函数将一个int类型的数转换为一个int20类型的数。

重载了输入输出运算符:std::ostream& operator<<(std::ostream& os, const int20& num) 和 std::istream& operator>>(std::istream& is, int20& num)

重载了加减乘除、取余运算:int20 operator+(const int20& num1, const int20& num2), int20 operator-(const int20& num1, const int20& num2), int20 operator*(const int20& num1, const int20& num2), int20 operator/(const int20& num1, const int20& num2), int20 operator%(const int20& num1, const int20& num2)

重载了++,--运算符。

具体类int20的实现以及各个函数的定义如下:

#include "int20.h"
#include <string>
#include <algorithm>

std::int20::int20(const int& num) {
    this->value = num & ((1 << 20) - 1);
}

std::ostream& std::operator<<(std::ostream& os, const std::int20& num) {
    os << num.value;
    return os;
}

std::istream& std::operator>>(std::istream& is, std::int20& num) {
    std::string str;
    is >> str;
    int len = str.length();
    std::reverse(str.begin(), str.end());
    int sum = 0;
    for (int i = 0; i < len; i += 1) {
        sum += (str[i] - '0') << (i * 4);
    }
    num.value = sum;
    return is;
}

std::int20 std::operator+(const std::int20& num1, const std::int20& num2) {
    int20 res;
    res.value = num1.value + num2.value;
    return res;
}

std::int20 std::operator-(const std::int20& num1, const std::int20& num2) {
    int20 res;
    res.value = num1.value - num2.value;
    return res;
}

std::int20 std::operator*(const std::int20& num1, const std::int20& num2) {
    int20 res;
    unsigned int num1_ = num1.value;
    unsigned int num2_ = num2.value;
    for (int i = 0; i < 20; i += 1) {
        if (num2_ & 1) {
            res.value += num1_ << i;
        }
        num2_ >>= 1;
    }
    return res;
}

std::int20 std::operator/(const std::int20& num1, const std::int20& num2) {
    int20 res;
    if (num2.value == 0) return res;

    unsigned int num1_ = num1.value;
    unsigned int num2_ = num2.value;
    for (int i = 19; i >= 0; i -= 1) {
        if ((num2_ << i) <= num1_) {
            num1_ -= (num2_ << i);
            res.value |= (1 << i);
        }
    }
    return res;
}

std::int20 std::operator%(const std::int20& num1, const std::int20& num2) {
    int20 res;
    if (num2.value == 0) return res;

    unsigned int num1_ = num1.value;
    unsigned int num2_ = num2.value;
    for (int i = 19; i >= 0; i -= 1) {
        if ((num2_ << i) <= num1_) {
            num1_ -= (num2_ << i);
            res.value |= (1 << i);
        }
    }
    return int20(num1_ & ((1 << 20) - 1));
}

std::int20 std::int20::operator++() {
    value++;
    return *this;
}

const std::int20 std::int20::operator++(int) {
    int20 old(*this);
    ++(*this);
    return old;
}

std::int20 std::int20::operator--() {
    value--;
    return *this;
}

const std::int20 std::int20::operator--(int) {
    int20 old(*this);
    --(*this);
    return old;
}

以上代码是重载C++中的 <<、>>、+、-、*、/、%、++、-- 及构造函数的实现。下面是main.cpp的代码

#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.h 的头文件,并在其内部写上并保存上述C++的代码。在int20.h所在的文件夹内新建一个main.cpp文件,输入以上main函数的代码,并在此文件夹内打开终端,输入以下指令进行编译并运行:

g++ main.cpp -o main && ./main

如果输出的结果与预期一致,则说明实现正确。
如果我的回答解决了您的问题,请采纳!