int128的实现(未完成)

说实话我要裂了

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

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

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

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

其他的功能都测验过了

包含:

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

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

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

输出该变量中包含的数字

加上一个常数

对一个常数取模

乘一个常数

加上一个int128变量

未完成的有:

除以一个常数

除以一个int128变量

乘以一个int128变量

对一个int128变量取模

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

代码如下:

#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

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

更新(2024/2/22):

发现致命错误了,总算知道为什么除法不行了(输入的时候就出问题了,测试的时候没测试出来)

上面的函数几乎不能用了,等会大刀阔斧地改一下,预计今晚发一篇更新的博客

更新(2024/2/22):

改完了

int128的实现(基本完成)-CSDN博客

  • 7
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值