C++大数运算_size_t几个字节

2022-11-10 15:08:51 浏览数 (1)

大家好,又见面了,我是你们的朋友全栈君。

代码语言:javascript复制
#include <cstring>
#include <string>
#include <iostream>
using namespace std;
constexpr auto MAXN = 9999;
constexpr auto MAXSIZE = 1000;
constexpr auto DLEN = 4;
class BigN
{
private:
int a[MAXSIZE / 20] = { 0 };    //可以控制大数的位数,越小性能越高,但支持的数字会变短
int len;       //大数长度  
public:
BigN() { len = 1; memset(a, 0, sizeof(a)); }   //构造函数  
BigN(const int&);       //将一个int类型的变量转化为大数  
BigN(const char*);     //将一个字符串类型的变量转化为大数 
BigN(const string&);   //将一个字符串类型的变量转化为大数 
BigN(const BigN&);  //拷贝构造函数  
//BigN(BigN&&) {};  //移动构造函数  
BigN& operator()(const int&);	  //将一个int类型的变量转化为大数  
BigN& operator()(const char*);    //将一个字符串类型的变量转化为大数  
BigN& operator()(const string&);    //将一个字符串类型的变量转化为大数  
BigN& operator()(const BigN&);  //拷贝构造函数  
//BigN& operator()(BigN&&) { return *this; };  //移动构造函数 
//重载复制
BigN& operator=(const int&);
BigN& operator=(const char*);
BigN& operator=(const BigN&); 
//BigN& operator=(BigN&&)noexcept { return *this; };//移动赋值
BigN& operator=(const string&);
//重载比较
bool operator>(const BigN&) const;
bool operator>(const int&)const;
bool operator>=(const BigN&) const;
bool operator<(const BigN&) const;
bool operator<(const int&)const;
bool operator<=(const BigN&) const;
bool operator==(const BigN&) const;
bool operator!=(const BigN&) const;
bool operator!() const;
//四则运算重载
BigN operator (const BigN&) const;
BigN operator  ();
BigN operator  (int);
BigN operator =(const BigN&);
BigN operator-(const BigN&) const;
BigN operator--();
BigN operator--(int);
BigN operator-=(const BigN&);
BigN operator*(const BigN&) const;
BigN operator*(const int&) const;
BigN operator*=(const BigN&);
BigN operator/(const BigN&) const;
BigN operator/(const int&) const;  
BigN operator/=(const BigN&);
BigN operator^(const int&) const;    //大数的n次方运算  
int  operator%(const int&) const;    //大数对一个int类型的变量进行取模运算      
void display();       //输出大数  
friend istream& operator>>(istream&, BigN&);   //重载输入运算符  
friend ostream& operator<<(ostream&, const BigN&);   //重载输出运算符  
};
BigN::BigN(const int &b)     //将一个int类型的变量转化为大数  
{
int c, d = b;
len = 0;
while (d > MAXN)
{
c = d - (d / (MAXN   1)) * (MAXN   1);
d = d / (MAXN   1);
a[  len] = c;
}
a[  len] = d;
}
BigN::BigN(const char* s)     //将一个字符串类型的变量转化为大数  
{
int t, k, index, l, i;
l = strlen(s);
len = l / DLEN;
if (l % DLEN)
  len;
index = 0;
for (i = l - 1; i >= 0; i -= DLEN)
{
t = 0;
k = i - DLEN   1;
if (k < 0)
k = 0;
for (int j = k; j <= i;   j)
t = t * 10   s[j] - '0';
a[  index] = t;
}
}
BigN::BigN(const string& s)
{
int t, k, index, l, i;
l = s.size();
len = l / DLEN;
if (l % DLEN)
  len;
index = 0;
for (i = l - 1; i >= 0; i -= DLEN)
{
t = 0;
k = i - DLEN   1;
if (k < 0)
k = 0;
for (int j = k; j <= i;   j)
t = t * 10   s[j] - '0';
a[  index] = t;
}
}
BigN::BigN(const BigN& T) : len(T.len)  //拷贝构造函数  
{
int i;
for (i = 0; i < len;   i)
a[i] = T.a[i];
}
BigN& BigN::operator=(const BigN& n)   //重载赋值运算符,大数之间进行赋值运算  
{
int i;
len = n.len;
memset(a, 0, sizeof(a));
for (i = 0; i < len;   i)
a[i] = n.a[i];
return *this;
}
istream& operator>>(istream& in, BigN& b)   //重载输入运算符  
{
char ch[MAXSIZE * 4];
int i = -1;
in >> ch;
int l = strlen(ch);
int count = 0, sum = 0;
for (i = l - 1; i >= 0;)
{
sum = 0;
int t = 1;
for (int j = 0; j < 4 && i >= 0;   j, --i, t *= 10)
{
sum  = (ch[i] - '0') * t;
}
b.a[count] = sum;
  count;
}
b.len =   count;
return in;
}
ostream& operator<<(ostream& out, const BigN& b)   //重载输出运算符  
{
int i;
out << b.a[b.len - 1];
for (i = b.len - 2; i >= 0; i--)
{
out.width(DLEN);
out.fill('0');
out << b.a[i];
}
return out;
}
BigN& BigN::operator()(const int& b)
{
int c, d = b;
len = 0;
while (d > MAXN)
{
c = d - (d / (MAXN   1)) * (MAXN   1);
d = d / (MAXN   1);
a[len  ] = c;
}
a[  len] = d;
return *this;
}
BigN& BigN::operator()(const char* s)
{
int t, k, index, l, i;
l = strlen(s);
len = l / DLEN;
if (l % DLEN)
  len;
index = 0;
for (i = l - 1; i >= 0; i -= DLEN)
{
t = 0;
k = i - DLEN   1;
if (k < 0)
k = 0;
for (int j = k; j <= i;   j)
t = t * 10   s[j] - '0';
a[  index] = t;
}
return *this;
}
BigN& BigN::operator()(const string& s)
{
int t, k, index, l, i;
l = s.size();
len = l / DLEN;
if (l % DLEN)
len  ;
index = 0;
for (i = l - 1; i >= 0; i -= DLEN)
{
t = 0;
k = i - DLEN   1;
if (k < 0)
k = 0;
for (int j = k; j <= i; j  )
t = t * 10   s[j] - '0';
a[index  ] = t;
}
return *this;
}
BigN& BigN::operator()(const BigN& T)
{
int i;
for (i = 0; i < len; i  )
a[i] = T.a[i];
return *this;
}
BigN BigN::operator (const BigN& T) const   //两个大数之间的相加运算  
{
BigN t(*this);
int i, big;      //位数     
big = T.len > len ? T.len : len;
for (i = 0; i < big;   i)
{
t.a[i]  = T.a[i];
if (t.a[i] > MAXN)
{
  t.a[i   1];
t.a[i] -= MAXN   1;
}
}
if (t.a[big] != 0)
t.len = big   1;
else
t.len = big;
return t;
}
BigN BigN::operator-(const BigN& T) const   //两个大数之间的相减运算   
{
int i, j, big;
bool flag;
BigN t1, t2;
if (*this > T)
{
t1 = *this;
t2 = T;
flag = 0;
}
else
{
t1 = T;
t2 = *this;
flag = 1;
}
big = t1.len;
for (i = 0; i < big;   i)
{
if (t1.a[i] < t2.a[i])
{
j = i   1;
while (t1.a[j] == 0)
  j;
--t1.a[--j];
while (j > i)
t1.a[--j]  = MAXN;
t1.a[i]  = MAXN   1 - t2.a[i];
}
else
t1.a[i] -= t2.a[i];
}
t1.len = big;
while (t1.a[t1.len - 1] == 0 && t1.len > 1)
{
--t1.len;
--big;
}
if (flag)
t1.a[big - 1] = 0 - t1.a[big - 1];
return t1;
}
BigN BigN::operator*(const BigN& T) const   //两个大数之间的相乘运算   
{
BigN ret;
int i, j, up;
int temp, temp1;
for (i = 0; i < len;   i)
{
up = 0;
for (j = 0; j < T.len;   j)
{
temp = a[i] * T.a[j]   ret.a[i   j]   up;
if (temp > MAXN)
{
temp1 = temp - temp / (MAXN   1) * (MAXN   1);
up = temp / (MAXN   1);
ret.a[i   j] = temp1;
}
else
{
up = 0;
ret.a[i   j] = temp;
}
}
if (up != 0)
ret.a[i   j] = up;
}
ret.len = i   j;
while (ret.a[ret.len - 1] == 0 && ret.len > 1)
--ret.len;
return ret;
}
BigN BigN::operator/(const int& b) const   //大数对一个整数进行相除运算  
{
BigN ret;
int i, down = 0;
for (i = len - 1; i >= 0; --i)
{
ret.a[i] = (a[i]   down * (MAXN   1)) / b;
down = a[i]   down * (MAXN   1) - ret.a[i] * b;
}
ret.len = len;
while (ret.a[ret.len - 1] == 0 && ret.len > 1)
--ret.len;
return ret;
}
int BigN::operator %(const int& b) const    //大数对一个int类型的变量进行取模运算      
{
int i, d = 0;
for (i = len - 1; i >= 0; --i)
{
d = ((d * (MAXN   1)) % b   a[i]) % b;
}
return d;
}
BigN BigN::operator^(const int& n) const    //大数的n次方运算  
{
BigN t, ret(1);
int i;
if (n < 0)
exit(-1);
if (n == 0)
return 1;
if (n == 1)
return *this;
int m = n;
while (m > 1)
{
t = *this;
for (i = 1; i << 1 <= m; i <<= 1)
{
t = t * t;
}
m -= i;
ret = ret * t;
if (m == 1)
ret = ret * (*this);
}
return ret;
}
bool BigN::operator>(const BigN& T) const   //大数和另一个大数的大小比较  
{
int ln;
if (len > T.len)
return true;
else if (len == T.len)
{
ln = len - 1;
while (a[ln] == T.a[ln] && ln >= 0)
--ln;
if (ln >= 0 && a[ln] > T.a[ln])
return true;
else
return false;
}
else
return false;
}
bool BigN::operator >(const int& t) const    //大数和一个int类型的变量的大小比较  
{
BigN b(t);
return *this > b;
}
void BigN::display()    //输出大数  
{
int i;
cout << a[len - 1];
for (i = len - 2; i >= 0; --i)
{
cout.width(DLEN);
cout.fill('0');
cout << a[i];
}
cout << endl;
}
#include <time.h>
int main()
{
int i = 2000000;
BigN a, b, c;
cin >> a >> b;
time_t t1 = clock();
while (--i) {
c = a * b;
}
time_t t2 = clock();
cout << t2 - t1 << endl;
cout << c << endl;
return 0;
}

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

0 人点赞