Skip to content

Commit 6eba6ef

Browse files
author
taotao
committed
添加大数模板
1 parent 820fb41 commit 6eba6ef

File tree

1 file changed

+293
-0
lines changed

1 file changed

+293
-0
lines changed

Diff for: math/大数模板.md

+293
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,293 @@
1+
```c++
2+
class BigNum
3+
{
4+
private:
5+
int a[500]; //可以控制大数的位数
6+
int len; //大数长度
7+
public:
8+
BigNum(){ len = 1;memset(a,0,sizeof(a)); } //构造函数
9+
BigNum(const int); //将一个int类型的变量转化为大数
10+
BigNum(const char*); //将一个字符串类型的变量转化为大数
11+
BigNum(const BigNum &); //拷贝构造函数
12+
BigNum &operator=(const BigNum &); //重载赋值运算符,大数之间进行赋值运算
13+
14+
friend istream& operator>>(istream&, BigNum&); //重载输入运算符
15+
friend ostream& operator<<(ostream&, BigNum&); //重载输出运算符
16+
17+
BigNum operator+(const BigNum &) const; //重载加法运算符,两个大数之间的相加运算
18+
BigNum operator-(const BigNum &) const; //重载减法运算符,两个大数之间的相减运算
19+
BigNum operator*(const BigNum &) const; //重载乘法运算符,两个大数之间的相乘运算
20+
BigNum operator/(const int &) const; //重载除法运算符,大数对一个整数进行相除运算
21+
22+
BigNum operator^(const int &) const; //大数的n次方运算
23+
int operator%(const int &) const; //大数对一个int类型的变量进行取模运算
24+
bool operator>(const BigNum & T)const; //大数和另一个大数的大小比较
25+
bool operator>(const int & t)const; //大数和一个int类型的变量的大小比较
26+
27+
void print(); //输出大数
28+
};
29+
BigNum::BigNum(const int b) //将一个int类型的变量转化为大数
30+
{
31+
int c,d = b;
32+
len = 0;
33+
memset(a,0,sizeof(a));
34+
while(d > MAXN)
35+
{
36+
c = d - (d / (MAXN + 1)) * (MAXN + 1);
37+
d = d / (MAXN + 1);
38+
a[len++] = c;
39+
}
40+
a[len++] = d;
41+
}
42+
BigNum::BigNum(const char*s) //将一个字符串类型的变量转化为大数
43+
{
44+
int t,k,index,l,i;
45+
memset(a,0,sizeof(a));
46+
l=strlen(s);
47+
len=l/DLEN;
48+
if(l%DLEN)
49+
len++;
50+
index=0;
51+
for(i=l-1;i>=0;i-=DLEN)
52+
{
53+
t=0;
54+
k=i-DLEN+1;
55+
if(k<0)
56+
k=0;
57+
for(int j=k;j<=i;j++)
58+
t=t*10+s[j]-'0';
59+
a[index++]=t;
60+
}
61+
}
62+
BigNum::BigNum(const BigNum & T) : len(T.len) //拷贝构造函数
63+
{
64+
int i;
65+
memset(a,0,sizeof(a));
66+
for(i = 0 ; i < len ; i++)
67+
a[i] = T.a[i];
68+
}
69+
BigNum & BigNum::operator=(const BigNum & n) //重载赋值运算符,大数之间进行赋值运算
70+
{
71+
int i;
72+
len = n.len;
73+
memset(a,0,sizeof(a));
74+
for(i = 0 ; i < len ; i++)
75+
a[i] = n.a[i];
76+
return *this;
77+
}
78+
istream& operator>>(istream & in, BigNum & b) //重载输入运算符
79+
{
80+
char ch[MAXSIZE*4];
81+
int i = -1;
82+
//in>>ch;
83+
scanf("%s", ch);
84+
int l=strlen(ch);
85+
int count=0,sum=0;
86+
for(i=l-1;i>=0;)
87+
{
88+
sum = 0;
89+
int t=1;
90+
for(int j=0;j<4&&i>=0;j++,i--,t*=10)
91+
{
92+
sum+=(ch[i]-'0')*t;
93+
}
94+
b.a[count]=sum;
95+
count++;
96+
}
97+
b.len =count++;
98+
return in;
99+
100+
}
101+
ostream& operator<<(ostream& out, BigNum& b) //重载输出运算符
102+
{
103+
int i;
104+
cout << b.a[b.len - 1];
105+
for(i = b.len - 2 ; i >= 0 ; i--)
106+
{
107+
cout.width(DLEN);
108+
cout.fill('0');
109+
cout << b.a[i];
110+
}
111+
return out;
112+
}
113+
114+
BigNum BigNum::operator+(const BigNum & T) const //两个大数之间的相加运算
115+
{
116+
BigNum t(*this);
117+
int i,big; //位数
118+
big = T.len > len ? T.len : len;
119+
for(i = 0 ; i < big ; i++)
120+
{
121+
t.a[i] +=T.a[i];
122+
if(t.a[i] > MAXN)
123+
{
124+
t.a[i + 1]++;
125+
t.a[i] -=MAXN+1;
126+
}
127+
}
128+
if(t.a[big] != 0)
129+
t.len = big + 1;
130+
else
131+
t.len = big;
132+
return t;
133+
}
134+
BigNum BigNum::operator-(const BigNum & T) const //两个大数之间的相减运算
135+
{
136+
int i,j,big;
137+
bool flag;
138+
BigNum t1,t2;
139+
if(*this>T)
140+
{
141+
t1=*this;
142+
t2=T;
143+
flag=0;
144+
}
145+
else
146+
{
147+
t1=T;
148+
t2=*this;
149+
flag=1;
150+
}
151+
big=t1.len;
152+
for(i = 0 ; i < big ; i++)
153+
{
154+
if(t1.a[i] < t2.a[i])
155+
{
156+
j = i + 1;
157+
while(t1.a[j] == 0)
158+
j++;
159+
t1.a[j--]--;
160+
while(j > i)
161+
t1.a[j--] += MAXN;
162+
t1.a[i] += MAXN + 1 - t2.a[i];
163+
}
164+
else
165+
t1.a[i] -= t2.a[i];
166+
}
167+
t1.len = big;
168+
while(t1.a[len - 1] == 0 && t1.len > 1)
169+
{
170+
t1.len--;
171+
big--;
172+
}
173+
if(flag)
174+
t1.a[big-1]=0-t1.a[big-1];
175+
return t1;
176+
}
177+
178+
BigNum BigNum::operator*(const BigNum & T) const //两个大数之间的相乘运算
179+
{
180+
BigNum ret;
181+
int i,j,up;
182+
int temp,temp1;
183+
for(i = 0 ; i < len ; i++)
184+
{
185+
up = 0;
186+
for(j = 0 ; j < T.len ; j++)
187+
{
188+
temp = a[i] * T.a[j] + ret.a[i + j] + up;
189+
if(temp > MAXN)
190+
{
191+
temp1 = temp - temp / (MAXN + 1) * (MAXN + 1);
192+
up = temp / (MAXN + 1);
193+
ret.a[i + j] = temp1;
194+
}
195+
else
196+
{
197+
up = 0;
198+
ret.a[i + j] = temp;
199+
}
200+
}
201+
if(up != 0)
202+
ret.a[i + j] = up;
203+
}
204+
ret.len = i + j;
205+
while(ret.a[ret.len - 1] == 0 && ret.len > 1)
206+
ret.len--;
207+
return ret;
208+
}
209+
BigNum BigNum::operator/(const int & b) const //大数对一个整数进行相除运算
210+
{
211+
BigNum ret;
212+
int i,down = 0;
213+
for(i = len - 1 ; i >= 0 ; i--)
214+
{
215+
ret.a[i] = (a[i] + down * (MAXN + 1)) / b;
216+
down = a[i] + down * (MAXN + 1) - ret.a[i] * b;
217+
}
218+
ret.len = len;
219+
while(ret.a[ret.len - 1] == 0 && ret.len > 1)
220+
ret.len--;
221+
return ret;
222+
}
223+
int BigNum::operator %(const int & b) const //大数对一个int类型的变量进行取模运算
224+
{
225+
int i,d=0;
226+
for (i = len-1; i>=0; i--)
227+
{
228+
d = ((d * (MAXN+1))% b + a[i])% b;
229+
}
230+
return d;
231+
}
232+
BigNum BigNum::operator^(const int & n) const //大数的n次方运算
233+
{
234+
BigNum t,ret(1);
235+
int i;
236+
if(n<0)
237+
exit(-1);
238+
if(n==0)
239+
return 1;
240+
if(n==1)
241+
return *this;
242+
int m=n;
243+
while(m>1)
244+
{
245+
t=*this;
246+
for( i=1;i<<1<=m;i<<=1)
247+
{
248+
t=t*t;
249+
}
250+
m-=i;
251+
ret=ret*t;
252+
if(m==1)
253+
ret=ret*(*this);
254+
}
255+
return ret;
256+
}
257+
bool BigNum::operator>(const BigNum & T) const //大数和另一个大数的大小比较
258+
{
259+
int ln;
260+
if(len > T.len)
261+
return true;
262+
else if(len == T.len)
263+
{
264+
ln = len - 1;
265+
while(a[ln] == T.a[ln] && ln >= 0)
266+
ln--;
267+
if(ln >= 0 && a[ln] > T.a[ln])
268+
return true;
269+
else
270+
return false;
271+
}
272+
else
273+
return false;
274+
}
275+
bool BigNum::operator >(const int & t) const //大数和一个int类型的变量的大小比较
276+
{
277+
BigNum b(t);
278+
return *this>b;
279+
}
280+
281+
void BigNum::print() //输出大数
282+
{
283+
int i;
284+
cout << a[len - 1];
285+
for(i = len - 2 ; i >= 0 ; i--)
286+
{
287+
cout.width(DLEN);
288+
cout.fill('0');
289+
cout << a[i];
290+
}
291+
cout << endl;
292+
}
293+
```

0 commit comments

Comments
 (0)