说实话我要裂了
除法部分不知道为什么一直不行
写这个是因为昨天去看了类的构建以及重载
就把之前的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
复制到文件头上就可以用了(也可以作为头文件)