C + + large number template (recommended) _c language

Source: Internet
Author: User
Tags strlen
Use the operator overloading method in C + + to implement mathematical operations between large numbers, including addition, subtraction, multiplication, division, n-th, modulo, size comparison, assignment and overload of input stream, output stream.
and using this large number of templates, smoothly AC hdoj 1134 on this topic Catalan count problem.
http://acm.hdu.edu.cn/showproblem.php?pid=1134
The code for the large number of templates is as follows:
Copy Code code as follows:

#include <iostream>
#include <string>
#include <iomanip>
#include <algorithm>
using namespace Std;
#define MAXN 9999
#define MAXSIZE 10
#define Dlen 4
Class Bignum
{
Private
int a[500]; Can control the number of digits in a large number
int Len; Large number of length
Public
Bignum () {len = 1;memset (A,0,sizeof (a));} Constructors
Bignum (const int); Converts a variable of type int to a large number
Bignum (const char*); Converts a variable of a string type to a large number
Bignum (const bignum &); Copy Constructors
Bignum &operator= (const bignum &); Overloaded assignment operators, assignment operations between large numbers
Friend istream& operator>> (istream&, bignum&); Overloaded input operator
Friend ostream& operator<< (ostream&, bignum&); Overloaded output operator
Bignum operator+ (const bignum &) const; Overloaded addition operator, add operation between two large numbers
Bignum operator-(const bignum &) const; Overloaded subtraction operator, subtraction between two large numbers
Bignum operator* (const bignum &) const; Overloaded multiplication operator, multiplication between two large numbers
Bignum operator/(const int &) const; Overloaded division operator, large number dividing an integer
Bignum operator^ (const int &) const; n-Th-square operation of large numbers
int operator% (const int &) const; A large number takes a modulo operation on a variable of type int
BOOL Operator> (const BIGNUM & T) const; Size comparison of large and another large number
BOOL Operator> (const INT & T) const; Size comparison of large numbers and variables of an int type
void print (); Output large number
};
Bignum::bignum (const int B)//Convert a variable of type int to a large number
{
int c,d = b;
len = 0;
memset (A,0,sizeof (a));
while (d > Maxn)
{
c = d-(d/(MAXN + 1)) * (MAXN + 1);
D = d/(MAXN + 1);
a[len++] = c;
}
a[len++] = D;
}
Bignum::bignum (const char*s)//Converts a variable of a string type to a large number
{
int t,k,index,l,i;
memset (A,0,sizeof (a));
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;
}
}
Bignum::bignum (const Bignum & T): Len (T.len)//copy constructor
{
int i;
memset (A,0,sizeof (a));
for (i = 0; i < len; i++)
A[i] = T.a[i];
}
Bignum & bignum::operator= (const BIGNUM & N)//overloaded assignment operators, assignment operations between large numbers
{
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, Bignum & B)//Overloaded input operator
{
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, bignum& B)//Overloaded output operator
{
int i;
cout << b.a[b.len-1];
for (i = b.len-2 i >= 0; i--)
{
Cout.width (Dlen);
Cout.fill (' 0 ');
cout << B.a[i];
}
return out;
}
Bignum bignum::operator+ (const BIGNUM & T) const//Two large number of addition operations
{
Bignum T (*this);
int i,big; Number of digits
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;
}
Bignum bignum::operator-(const BIGNUM & T) const//Two large number of subtraction operations
{
int i,j,big;
BOOL Flag;
Bignum 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[len-1] = = 0 && t1.len > 1)
{
t1.len--;
big--;
}
if (flag)
T1.A[BIG-1]=0-T1.A[BIG-1];
return t1;
}
Bignum bignum::operator* (const BIGNUM & T) const//two large number multiplication
{
BIGNUM 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;
}
Bignum bignum::operator/(const int & B) const//large number dividing an integer
{
BIGNUM 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 Bignum::operator% (const int & B) const//large number modulo a variable of type int
{
int i,d=0;
for (i = len-1; i>=0; i--)
{
D = ((d * (maxn+1))% B + a[i])% B;
}
return D;
}
Bignum bignum::operator^ (const int & N) const//large number of n-th-square operations
{
Bignum 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 Bignum::operator> (const BIGNUM & T) const//large and another large number size comparison
{
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 Bignum::operator > (const int & T) const//large number and size comparison of a variable of type int
{
Bignum B (t);
Return *this>b;
}
void Bignum::p rint ()//Output large number
{
int i;
cout << a[len-1];
for (i = len-2 i >= 0; i--)
{
Cout.width (Dlen);
Cout.fill (' 0 ');
cout << A[i];
}
cout << Endl;
}
int main (void)
{
int i,n;
Bignum x[101]; An array of objects that define large numbers
X[0]=1;
for (i=1;i<101;i++)
x[i]=x[i-1]* (4*i-2)/(i+1);
while (scanf ("%d", &n) ==1 && n!=-1)
{
X[n].print ();
}
}
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.