IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> C++知识库 -> C++大整数类(加减乘除) -> 正文阅读

[C++知识库]C++大整数类(加减乘除)

最近看到身边有中学生用C++写大整数加减乘除,我也试着写了一下。

思路是用一堆无符号32位整数连接在一起,需要借助原生的64位整数加法和乘法运算。

本人不是计算机专业,这是我第0次在CSDN上贴代码,欢迎挑虫,轻喷。

MYUINT4000.H

#pragma once
#include <iostream>
#define MYUINT32_MAX_UI64 (0xffffffffui64)
#define MYHIBIWORD(MYUInt64) (static_cast<MYUINT32>(MYUInt64 >> 32))
#define MYLOBIWORD(MYUInt64) (static_cast<MYUINT32>(MYUInt64 & MYUINT32_MAX_UI64))

class MYUINT4000  /* 无符号四千位(二进制)整数类 */
{
    typedef unsigned long long MYUINT64;
    typedef unsigned int MYUINT32;
    typedef signed int MYINT32;
    typedef signed long long MYINT64;
protected:
    MYUINT32 _m_[125];  /* 下标0是最低位 */
protected:
    MYUINT4000(MYINT32 dgt, MYUINT64 m) : MYUINT4000() { this->_m_[dgt] = MYLOBIWORD(m); if (dgt < 124) { this->_m_[dgt + 1] = MYHIBIWORD(m); } }
    MYINT32 m_dgt() const { MYINT32 cnt; for (cnt = 124; cnt >= 0; cnt--) { if (this->_m_[cnt] != 0) { break; } } return (cnt + 1); }
    void state(MYINT32 dgt) { this->_m_[dgt / 32] += (MYUINT32)(1) << (dgt % 32); }
    bool operator[](MYINT32 dgt) const { if (this->_m_[dgt / 32] & ((MYUINT32)(1) << (dgt % 32))) { return (true); } else { return (false); } }
    static void addone(char* dec) { dec[0] += 1; for (MYINT32 dgt = 0; dec[dgt] > '9'; dgt++) { dec[dgt] = '0'; dec[dgt + 1] += 1; } }
    static void bytwo(char* dec)
    {
        MYINT32 dgt;
        char temp;
        char carry = 0;
        for (dgt = 0; dec[dgt] != '\0'; dgt++)
        {
            temp = (dec[dgt] - '0') * 2 + carry;
            if (temp > 9) { carry = 1; dec[dgt] = temp - 10 + '0'; }
            else { carry = 0; dec[dgt] = temp + '0'; }
        }
        if (carry == 1) { dec[dgt] = '1'; }
    }
public:
    MYUINT4000() { for (MYINT32 cnt = 0; cnt < 125; cnt++) { this->_m_[cnt] = 0; } }
    MYUINT4000(MYUINT32 low) : MYUINT4000() { this->_m_[0] = low; }
    MYUINT4000(MYUINT64 low) : MYUINT4000() { this->_m_[0] = MYLOBIWORD(low); this->_m_[1] = MYHIBIWORD(low); }
    MYUINT4000(MYINT32 low) { if (low >= 0) { (*this) = MYUINT4000(static_cast<MYUINT32>(low)); } else { (*this) = -MYUINT4000(static_cast<MYUINT32>(-low)); } }
    MYUINT4000(MYINT64 low) { if (low >= 0) { (*this) = MYUINT4000(static_cast<MYUINT64>(low)); } else { (*this) = -MYUINT4000(static_cast<MYUINT64>(-low)); } }
    MYUINT4000 operator+(const MYUINT4000 another) const
    {
        MYUINT64 temp;
        MYUINT64 carry = 0;
        MYUINT4000 res;
        for (MYINT32 cnt = 0; cnt < 125; cnt++)
        {
            temp = static_cast<MYUINT64>(this->_m_[cnt]) + static_cast<MYUINT64>(another._m_[cnt]) + carry;
            if (temp > MYUINT32_MAX_UI64) { carry = 1; }
            else { carry = 0; }
            res._m_[cnt] = MYLOBIWORD(temp);
        }
        return (res);
    }
    MYUINT4000 operator~() const
    {
        MYUINT4000 res;
        for (MYINT32 cnt = 0; cnt < 125; cnt++)
        {
            res._m_[cnt] = ~(this->_m_[cnt]);
        }
        return (res);
    }
    MYUINT4000 operator-() const { return ((~(*this)) + MYUINT4000(1)); }
    MYUINT4000 operator-(const MYUINT4000 another) const { return ((*this) + (-(another))); }
    MYUINT4000 operator*(const MYUINT4000 another) const
    {
        MYUINT64 temp;
        MYUINT32 carry = 0;
        MYUINT4000 res(0);
        for (MYINT32 dgt = 0; dgt < 125; dgt++)
        {
            temp = 0;
            for (MYINT32 cnt = 0; cnt <= dgt; cnt++)
            {
                temp = temp + static_cast<MYUINT64>(this->_m_[cnt]) * static_cast<MYUINT64>(another._m_[dgt - cnt]);
            }
            res = res + MYUINT4000(dgt, temp);
        }
        return (res);
    }
    bool operator==(const MYUINT4000 another) const
    {
        for (MYINT32 cnt = 124; cnt >= 0; cnt--)
        {
            if (this->_m_[cnt] != another._m_[cnt]) { return (false); }
        }
        return (true);
    }
    bool operator<(const MYUINT4000 another) const
    {
        for (MYINT32 cnt = 124; cnt >= 0; cnt--)
        {
            if (this->_m_[cnt] < another._m_[cnt]) { return (true); }
            else if (this->_m_[cnt] > another._m_[cnt]) { return (false); }
        }
        return (false);
    }
    bool operator>(const MYUINT4000 another) const
    {
        for (MYINT32 cnt = 124; cnt >= 0; cnt--)
        {
            if (this->_m_[cnt] > another._m_[cnt]) { return (true); }
            else if (this->_m_[cnt] < another._m_[cnt]) { return (false); }
        }
        return (false);
    }
    bool operator>=(const MYUINT4000 another) const { return (!((*this) < another)); }
    bool operator<=(const MYUINT4000 another) const { return (!((*this) > another)); }
    MYUINT4000 operator>>(MYINT32 dgt) const
    {
        MYUINT4000 res(0);
        MYUINT4000 res1(0);
        MYUINT64 temp;
        MYINT32 dgt_m = dgt / 32;
        dgt = dgt % 32;
        for (MYINT32 cnt = 0; cnt < 125 - dgt_m; cnt++)
        {
            res._m_[cnt] = this->_m_[cnt + dgt_m];
        }
        for (MYINT32 cnt = 124; cnt >= 1; cnt--)
        {
            temp = (static_cast<MYUINT64>(res._m_[cnt])) << (32 - dgt);
            res1._m_[cnt] = res1._m_[cnt] + MYHIBIWORD(temp);
            res1._m_[cnt - 1] = res1._m_[cnt - 1] + MYLOBIWORD(temp);
        }
        temp = (static_cast<MYUINT64>(res._m_[0])) << (32 - dgt);
        res1._m_[0] = res1._m_[0] + MYHIBIWORD(temp);
        return (res1);
    }
    MYUINT4000 operator<<(MYINT32 dgt) const
    {
        MYUINT4000 res(0);
        MYUINT4000 res1(0);
        MYUINT64 temp;
        MYINT32 dgt_m = dgt / 32;
        dgt = dgt % 32;
        for (MYINT32 cnt = dgt_m; cnt < 125; cnt++)
        {
            res._m_[cnt] = this->_m_[cnt - dgt_m];
        }
        for (MYINT32 cnt = 0; cnt < 124; cnt++)
        {
            temp = (static_cast<MYUINT64>(res._m_[cnt])) << dgt;
            res1._m_[cnt + 1] = res1._m_[cnt + 1] + MYHIBIWORD(temp);
            res1._m_[cnt] = res1._m_[cnt] + MYLOBIWORD(temp);
        }
        temp = (static_cast<MYUINT64>(res._m_[124])) << dgt;
        res1._m_[124] = res1._m_[124] + MYLOBIWORD(temp);
        return (res1);
    }
    MYUINT4000 operator/(const MYUINT4000 another) const
    {
        /* 这个移位很头疼 */
        MYUINT4000 num(*this);
        MYUINT4000 den;
        MYUINT4000 res(0);
        MYINT32 dgt = (this->m_dgt() - another.m_dgt() + 1) * 32;
        den = another << dgt;
        if (dgt < 0) { return (MYUINT4000(0)); }
        for (MYINT32 cnt = dgt; cnt >= 0; cnt--)
        {
            if (num >= den)
            {
                num = num - den;
                res.state(cnt);
            }
            den = den >> 1;
        }
        return (res);
    }
    MYUINT4000 operator&(const MYUINT4000 another) const { MYUINT4000 res; for (MYINT32 cnt = 0; cnt < 125; cnt++) { res._m_[cnt] = this->_m_[cnt] & another._m_[cnt]; } return (res); }
    MYUINT4000 operator|(const MYUINT4000 another) const { MYUINT4000 res; for (MYINT32 cnt = 0; cnt < 125; cnt++) { res._m_[cnt] = this->_m_[cnt] | another._m_[cnt]; } return (res); }
    friend std::ostream& operator<<(std::ostream& out, const MYUINT4000 myUInt4000)
    {
        char dec[1210] = { '0' };
        for (MYINT32 cnt = 3999; cnt >= 1; cnt--)
        {
            if (myUInt4000[cnt]) { addone(dec); }
            bytwo(dec);
        }
        if (myUInt4000[0]) { addone(dec); }
        for (MYINT32 pos = strlen(dec) - 1; pos >= 0; pos--) { out << dec[pos]; }
        return (out);
    }
    void print(const char* sep = "_", const char* end = "\n") const
    {
        for (MYINT32 cnt = 124; cnt >= 1; cnt--)
        {
            printf("%08X%s", this->_m_[cnt], sep);
        }
        printf("%08X%s", this->_m_[0], end);
    }
};

  C++知识库 最新文章
【C++】友元、嵌套类、异常、RTTI、类型转换
通讯录的思路与实现(C语言)
C++PrimerPlus 第七章 函数-C++的编程模块(
Problem C: 算法9-9~9-12:平衡二叉树的基本
MSVC C++ UTF-8编程
C++进阶 多态原理
简单string类c++实现
我的年度总结
【C语言】以深厚地基筑伟岸高楼-基础篇(六
c语言常见错误合集
上一篇文章      下一篇文章      查看所有文章
加:2021-07-16 11:05:17  更:2021-07-16 11:07:39 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年4日历 -2024/4/28 16:12:14-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码