int128的实现(未完成)

说实话我要裂了

除法部分不知道为什么一直不行

写这个是因为昨天去看了类的构建以及重载

就把之前的int128的那个抓过来重新写了

话说之前的int128的除法好像也不行

其他的功能都测验过了

包含:

初始化(低于2^64的好用)

赋值(只支持2^64以内的)

从输入中读取数字(低于2^128的,而且会吞掉数字后面的一个字符)

输出该变量中包含的数字

加上一个常数

对一个常数取模

乘一个常数

加上一个int128变量

未完成的有:

除以一个常数

除以一个int128变量

乘以一个int128变量

对一个int128变量取模

暂时想到这些了,还有什么提醒我一下

代码如下:

cpp 复制代码
#ifndef CSTDIO_
#define CSTDIO_
#include<cstdio>
#endif

#ifndef CCTYPE_
#define CCTYPE_
#include<cctype>
#endif

#ifndef VECTOR_
#define VECTOR_
#include<vector>
#endif

#ifndef INT128_H_
#define INT128_H_

typedef unsigned long long LLU;//64位
typedef unsigned int U;//32位
const U MAX32 = 0xFFFFFFFF;
const LLU MAX64_U = 0xFFFFFFFF00000000;

class INT128{
    LLU H;
    LLU L;
public:
    INT128(LLU tmp1 = 0, LLU tmp2 = 0){H = tmp1, L = tmp2;};
    void getnum(void);
    INT128 operator+(const U & tmp) const;
    INT128 operator+(const INT128 & tmp) const;
    INT128 operator*(const U & tmp) const;
    INT128 operator/(const U & tmp) const;
    INT128 operator%(const U & tmp) const;
    void operator=(const LLU & tmp);
    void show(void);
};

void INT128::getnum(void)
{
    L = H = 0;
    std::vector<int> tmp;
    char c;
    while(!isdigit(c = getchar()))
        ;//清空数字前面的东西
    do
    {
        tmp.insert(tmp.begin(), c - '0');
    }while(isdigit(c = getchar()));
    //数组取余MAX32
    LLU ext = 0;
    for(int i = tmp.size() - 1; i >= 0; i--)
    {
        ext = ext * 10 + tmp[i];
        ext %= MAX32;
    }
    //数组减去得到的余数
    L = ext;
    int i = 0;
    while(ext)
    {
        tmp[i] -= ext % 10, ext /= 10;
        if(tmp[i] < 0){tmp[i] += 10, tmp[i + 1]--;}
        i++;
    }
    //数组除以MAX32
    for(i = tmp.size() - 1; i >= 0; i--)
    {
        ext = ext * 10 + tmp[i];
        H = H * 10 + ext / MAX32;
        ext %= MAX32;
    }
}
INT128 INT128::operator+(const U & tmp) const
{
    LLU A1, A2, A3, A4, B1, B2, B3, B4;
    INT128 result(0, 0);
    A1 = A2 = A3 = A4 = B3 = B4 = MAX32, B1 = B2 = B3 = 0;
    A1 &= H >> 32, A2 &= H;
    A3 &= L >> 32, A4 &= L, B4 &= tmp;
    A1 += B1, A2 += B2, A3 += B3, A4 += B4;
    result.H = (A1 << 32) + A2 + ((A3 & MAX64_U) >> 32);
    result.L = (A3 << 32) + A4;
    return result;
}
INT128 INT128::operator*(const U & tmp) const
{
    LLU A1, A2, A3, A4;
    INT128 result(0, 0);
    A1 = A2 = A3 = A4 = MAX32;
    A1 &= H >> 32, A2 &= H;
    A3 &= L >> 32, A4 &= L;
    A1 *= tmp, A2 *= tmp, A3 *= tmp, A4 *= tmp;
    result.H = (A1 << 32) + A2 + ((A3 & MAX64_U) >> 32);
    result.L = (A3 << 32) + A4;
    return result;
}
INT128 INT128::operator/(const U & tmp) const
{
    LLU A1, A2, A3, A4;
    INT128 result(0, 0);
    A1 = A2 = A3 = A4 = MAX32;
    A1 &= H >> 32, A2 &= H;
    A3 &= L >> 32, A4 &= L;
    A2 += (A1 % tmp) << 32, A1 /= tmp;
    A3 += (A2 % tmp) << 32, A2 /= tmp;
    A4 += (A3 % tmp) << 32, A3 /= tmp;
    A4 /= tmp;
    result.H = (A1 << 32) + A2 + ((A3 & MAX64_U) >> 32);
    result.L = (A3 << 32) + A4;
    return result;
}//未完成
INT128 INT128::operator%(const U & tmp) const
{
    LLU A1, A2, A3, A4;
    INT128 result(0, 0);
    A1 = A2 = A3 = A4 = MAX32;
    A1 &= H >> 32, A2 &= H;
    A3 &= L >> 32, A4 &= L;
    A2 += (A1 % tmp) << 32;
    A3 += (A2 % tmp) << 32;
    A4 += (A2 % tmp) << 32;
    result.L = A4 % tmp;
    
    return result;
}
INT128 INT128::operator+(const INT128 & tmp) const
{
    LLU A1, A2, A3, A4, B1, B2, B3, B4;
    INT128 result(0, 0);
    A1 = A2 = A3 = A4 = B1 = B2 = B3 = B4 = MAX32;
    A1 &= H >> 32, A2 &= H, B1 &= tmp.H >> 32, B2 &= tmp.H;
    A3 &= L >> 32, A4 &= L, B3 &= tmp.L >> 32, B4 &= tmp.L;
    A1 += B1, A2 += B2, A3 += B3, A4 += B4;
    result.H = (A1 << 32) + A2 + ((A3 & MAX64_U) >> 32);
    result.L = (A3 << 32) + A4;
    return result;
}
void INT128::operator=(const LLU & tmp)
{
    H = 0, L = tmp;
}
void INT128::show(void)
{
    std::vector<int> tmp;
    LLU n_tmp = H;
    while(n_tmp)
    {
        tmp.push_back(n_tmp % 10);
        n_tmp /= 10;
    }
    //数组乘以MAX32
    for(int i = 0; i < tmp.size(); i++)
        n_tmp += MAX32 * (LLU)tmp[i], tmp[i] = n_tmp % 10, n_tmp /= 10;
    while(n_tmp)
    {
        tmp.push_back(n_tmp % 10);
        n_tmp /= 10;
    }
    //数组加上L
    n_tmp = L;
    int ext = 0;
    for(int i = 0; i < tmp.size(); i++)
    {
        ext += n_tmp % 10 + tmp[i];
        tmp[i] = ext % 10, ext /= 10, n_tmp /= 10;
    }
    n_tmp += ext;
    while(n_tmp)
    {
        tmp.push_back(n_tmp % 10);
        n_tmp /= 10;
    }
    //输出
    if(!tmp.size())  tmp.push_back(0);
    for(int i = tmp.size() - 1; i >= 0; i--)
        printf("%d", tmp[i]);
}

#endif

复制到文件头上就可以用了(也可以作为头文件)

相关推荐
jyan_敬言7 分钟前
【C++】string类(二)相关接口介绍及其使用
android·开发语言·c++·青少年编程·visual studio
liulilittle31 分钟前
SNIProxy 轻量级匿名CDN代理架构与实现
开发语言·网络·c++·网关·架构·cdn·通信
tan77º1 小时前
【Linux网络编程】Socket - UDP
linux·服务器·网络·c++·udp
GiraKoo2 小时前
【GiraKoo】C++14的新特性
c++
悠悠小茉莉2 小时前
Win11 安装 Visual Studio(保姆教程 - 更新至2025.07)
c++·ide·vscode·python·visualstudio·visual studio
坏柠2 小时前
C++ Qt 基础教程:信号与槽机制详解及 QPushButton 实战
c++·qt
泽02022 小时前
C++之红黑树认识与实现
java·c++·rpc
岁忧3 小时前
(LeetCode 每日一题) 1865. 找出和为指定值的下标对 (哈希表)
java·c++·算法·leetcode·go·散列表
whoarethenext4 小时前
使用 C++ 实现 MFCC 特征提取与说话人识别系统
开发语言·c++·语音识别·mfcc
R-G-B4 小时前
【MFC】Combobox下拉框中4个选项,运行后点击下拉框选项不能全部展示出来,只能显示2个选项,需要垂直滚动条滚动显示其余选项
c++·mfc