大整数/高精度(暂不支持除法)

大整数/高精度(暂不支持除法)前言使用方法实现代码部分习题

前言

有些题的数据范围很大,以至于开了long long还是会爆…
为此我写了大整数,大致思想是开long long数组存储每九位数字(多了在处理乘法的时候会爆),然后用一个布尔值来判断正负。
目前只支持赋值,加法,减法,乘法,比较。
感觉除法太难,暂时没想出来怎么写,可能以后用到了会补上吧(orz)。

使用方法

声明:

bignum a,b,c;

初始化:

a.clear();
b.clear();

赋值:

a = 123; //赋值int类型
a = "123"; //赋值字符串类型(char *)
a = b; //赋值bignum类型

运算(支持 + - *):

c = a + b; //bignum + bignum
c = a + 1; //bignum + int
c += a; //bignum += bignum
c += 1; //bignum += int
c = -a; //bignum的负数

比较:

if (a < c)
{
//...
}
if (a >= c)
{
//...
}

实现代码

#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<cmath>
using namespace std;
const int maxn = 100;
const long long max_digit = 1e9;
struct big_num
{
long long digits[maxn];
bool minus = 0;
void clear(void)
{
this->minus = 0;
memset(this->digits, 0, sizeof(this->digits));
}
big_num operator=(const big_num b)
{
for (int i = 0; i < maxn; i++)
this->digits[i] = b.digits[i];
this->minus = b.minus;
return *this;
}
big_num operator=(const int b)
{
this->clear();
this->minus = b < 0;
if (b < 0)
this->digits[0] = -b;
else
this->digits[0] = b;
return *this;
}
big_num operator=(const char* b)
{
int len = strlen(b), st = 0;
this->clear();
if (b[0] == '-')
{
st = 1;
this->minus = 1;
}
for (int i = st; i < len; i++)
{
this->digits[(len - i - 1 + st) / 9] *= 10;
this->digits[(len - i - 1 + st) / 9] += b[i] - '0';
}
return *this;
}
bool operator<(const big_num b)
{
if (this->minus != b.minus) return this->minus > b.minus;
for (int i = 0; i < maxn; i++)
if (this->digits[i] != b.digits[i])
return (this->digits[i] < b.digits[i]) ^ minus;
return false;
}
bool operator>(const big_num b)
{
if (this->minus != b.minus) return this->minus < b.minus;
for (int i = 0; i < maxn; i++)
if (this->digits[i] != b.digits[i])
{
cout << i;
return (this->digits[i] > b.digits[i]) ^ this->minus;
}
return false;
}
bool operator==(const big_num b)
{
if (this->minus != b.minus) return false;
for (int i = 0; i < maxn; i++)
if (this->digits[i] != b.digits[i])
return false;
return true;
}
bool operator<=(const big_num b)
{
return *this < b || *this == b;
}
bool operator>=(const big_num b)
{
return *this > b || *this == b;
}
const big_num operator-() const
{
big_num temp = *this;
temp.minus ^= 1;
return temp;
}
big_num operator+(const big_num b)const
{
big_num temp = *this;
if (temp.minus == b.minus)
{
for (int i = 0; i < maxn; i++)
{
temp.digits[i] += b.digits[i];
}
for (int i = 1; i < maxn; i++)
{
temp.digits[i] += temp.digits[i - 1] / max_digit;
temp.digits[i - 1] %= max_digit;
}
return temp;
}
else
{
return temp - (-b);
}
}
big_num operator+(const int b)const
{
big_num temp = *this;
big_num b2;
b2 = b;
return temp + b2;
}
big_num operator-(const big_num b) const
{
big_num temp = *this;
if (temp.minus == b.minus)
{
if (temp >= b && temp.minus == 0 || temp <= b && temp.minus == 1)
{
for (int i = 0; i < maxn; i++)
{
temp.digits[i] -= b.digits[i];
}
for (int i = 1; i < maxn; i++)
{
if (temp.digits[i - 1] < 0)
{
temp.digits[i - 1] += max_digit;
temp.digits[i]--;
}
}
return temp;
}
else
{
return -(b - temp);
}
}
else
{
return temp + (-b);
}
}
big_num operator-(const int b) const
{
big_num temp = *this;
big_num b2;
b2 = abs(b);
b2.minus = b < 0;
return temp - b2;
}
big_num operator*(const big_num b)const
{
big_num temp;
temp.clear();
temp.minus = this->minus ^ b.minus;
for (int i = maxn - 1; i >= 0; i--)
{
for (int i2 = maxn - i - 1; i2 >= 0; i2--)
{
temp.digits[i + i2] += this->digits[i] * b.digits[i2];
if (temp.digits[i + i2] >= max_digit && i + i2 + 1 < maxn)
{
temp.digits[i + i2 + 1] += temp.digits[i + i2] / max_digit;
}
temp.digits[i + i2] %= max_digit;
}
}
for (int i = 1; i < maxn; i++)
{
temp.digits[i] += temp.digits[i - 1] / max_digit;
temp.digits[i - 1] %= max_digit;
}
return temp;
}
big_num operator*(const int b)const
{
big_num temp = *this;
temp.minus ^= (b < 0);
for (int i = 0; i < maxn; i++)
{
temp.digits[i] *= b;
}
for (int i = 1; i < maxn; i++)
{
temp.digits[i] += temp.digits[i - 1] / max_digit;
temp.digits[i - 1] %= max_digit;
}
return temp;
}
big_num operator+=(const big_num b)
{
*this = *this + b;
return *this;
}
big_num operator-=(const big_num b)
{
*this = *this - b;
return *this;
}
big_num operator*=(const big_num b)
{
*this = *this * b;
return *this;
}
big_num operator+=(const int b)
{
*this = *this + b;
return *this;
}
big_num operator-=(const int b)
{
*this = *this - b;
return *this;
}
big_num operator*=(const int b)
{
*this = *this * b;
return *this;
}
void show(void)
{
bool is_begun = 0;
for (int i = maxn - 1; i >= 0; i--)
{

if (is_begun)
printf("%0.9lld", this->digits[i]);
if (this->digits[i] != 0 && !is_begun)
{
if (this->minus == 1)
putchar('-');
is_begun = 1;
printf("%lld", this->digits[i]);
}
}
if (is_begun == 0) putchar('0');
putchar('\n');
}
};

部分习题

洛谷1005 - 矩阵取数游戏
洛谷1797 - 克鲁斯的加减法

原创:https://www.panoramacn.com
源码网提供WordPress源码,帝国CMS源码discuz源码,微信小程序,小说源码,杰奇源码,thinkphp源码,ecshop模板源码,微擎模板源码,dede源码,织梦源码等。

专业搭建小说网站,小说程序,杰奇系列,微信小说系列,app系列小说

大整数/高精度(暂不支持除法)

免责声明,若由于商用引起版权纠纷,一切责任均由使用者承担。

您必须遵守我们的协议,如果您下载了该资源行为将被视为对《免责声明》全部内容的认可-> 联系客服 投诉资源
www.panoramacn.com资源全部来自互联网收集,仅供用于学习和交流,请勿用于商业用途。如有侵权、不妥之处,请联系站长并出示版权证明以便删除。 敬请谅解! 侵权删帖/违法举报/投稿等事物联系邮箱:2640602276@qq.com
未经允许不得转载:书荒源码源码网每日更新网站源码模板! » 大整数/高精度(暂不支持除法)
关注我们小说电影免费看
关注我们,获取更多的全网素材资源,有趣有料!
120000+人已关注
分享到:
赞(0) 打赏

评论抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

您的打赏就是我分享的动力!

支付宝扫一扫打赏

微信扫一扫打赏