uhluhtc 2023-03-12 17:35 采纳率: 0%
浏览 68
已结题

如何使用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;
}

```

  • 写回答

5条回答 默认 最新

  • dahe0825 2023-03-12 18:02
    关注

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

    评论

报告相同问题?

问题事件

  • 系统已结题 3月20日
  • 创建了问题 3月12日

悬赏问题

  • ¥100 求懂行的大ge给小di解答下!
  • ¥15 pcl运行在qt msvc2019环境运行效率低于visual studio 2019
  • ¥15 MAUI,Zxing扫码,华为手机没反应。可提高悬赏
  • ¥15 python运行报错 ModuleNotFoundError: No module named 'torch'
  • ¥100 华为手机私有App后台保活
  • ¥15 sqlserver中加密的密码字段查询问题
  • ¥20 有谁能看看我coe文件到底哪儿有问题吗?
  • ¥20 我的这个coe文件到底哪儿出问题了
  • ¥15 matlab使用自定义函数时一直报错输入参数过多
  • ¥15 设计一个温度闭环控制系统