#include <iostream>
using namespace std;
template<int LIM,int MAX> class hp
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif)
{
public:
//vars
int sect[MAX];
int scnt;
//constructors
hp()
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
scnt=1;
sect[0]=0;
}
//functions
void copy(const hp<LIM,MAX> &A)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
for (int i=0;i<A.scnt;i++)
sect[i]=A.sect[i];
scnt=A.scnt;
}
void copy(int A)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
scnt=0;
while (A)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
sect[scnt++]=A % LIM;
A /=LIM;
}
}
void print()
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
int i,k;
printf("%d",sect[scnt-1]);
for (i=scnt-2;i>=0;i--)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
k=LIM/10;
while(sect[i]<k)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
printf("0");
k/=10;
}
if (sect[i])
printf("%d",sect[i]);
}
}
void read(int LIE)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
char A[LIM*MAX];
int len,i,j,k,b=0;
scanf("%s",A);
len=strlen(A);
k=len % LIE;
j=scnt=len / LIE;
if (k)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
scnt++;
j=scnt-1;
sect[j]=0;
for (i=0;i<k;i++)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
sect[j]*=10;
sect[j]+=A[b++]-'0';
}
}
for (j--;j>=0;j--)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
sect[j]=0;
for (i=0;i<LIE;i++)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
sect[j]*=10;
sect[j]+=A[b++]-'0';
}
}
}
void plus(hp<LIM,MAX> &A,int offset=0)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
int sc=scnt > A.scnt + offset ? scnt : A.scnt + offset;
int i,j,up=0;
for (i=0;i<sc;i++)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
j=i - offset;
if (j<0) continue;
if (i>=scnt) sect[i]=0;
if (j>=A.scnt) A.sect[j]=0;
sect[i]+=A.sect[j] + up;
up=sect[i] / LIM;
sect[i]%=LIM;
}
scnt=sc;
if (up) sect[scnt++]=up;
}
void minus(hp<LIM,MAX> &A)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
int sc=scnt;
int i,lend=0;
for (i=0;i<sc;i++)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (i>=A.scnt) A.sect[i]=0;
sect[i]-=A.sect[i] + lend;
lend=0;
if (sect[i]<0)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
lend=1;
sect[i]+=LIM;
}
}
scnt=sc;
if (lend) scnt--;
for (;scnt>1 && sect[scnt-1]==0;scnt--);
}
void multiply(int p)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (p==0)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
scnt=1;
sect[0]=0;
return;
}
int sc=scnt;
int i,up=0;
for (i=0;i<sc;i++)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (i>=scnt) sect[i]=0;
sect[i]=sect[i] * p + up;
up=sect[i] / LIM;
sect[i]%=LIM;
}
scnt=sc;
if (up) sect[scnt++]=up;
}
void multiply(hp<LIM,MAX> &A)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
hp<LIM,MAX> T,C;
int sc=A.scnt;
int i;
for (i=0;i<sc;i++)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
T=(*this);
T.multiply(A.sect[i]);
C.plus(T,i);
}
scnt=sc;
(*this)=C;
}
int compare(hp<LIM,MAX> &A)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (scnt > A.scnt)
return 1;
if (scnt < A.scnt)
return -1;
for (int i=scnt-1;i>=0;i--)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (sect[i] > A.sect[i])
return 1;
if (sect[i] < A.sect[i])
return -1;
}
return 0;
}
//operators
bool operator == (hp<LIM,MAX> &A)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
int t=compare(A);
return t==0;
}
bool operator < (hp<LIM,MAX> &A)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
int t=compare(A);
return t==-1;
}
bool operator > (hp<LIM,MAX> &A)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
int t=compare(A);
return t==1;
}
bool operator <= (hp<LIM,MAX> &A)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
int t=compare(A);
return t==-1 || t==0;
}
bool operator >= (hp<LIM,MAX> &A)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
int t=compare(A);
return t==1 || t==0;
}
void operator =(hp<LIM,MAX> A)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
copy(A);
}
void operator =(int A)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
copy(A);
}
void operator +=(hp<LIM,MAX> &A)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
plus(A);
}
void operator -=(hp<LIM,MAX> &A)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
minus(A);
}
void operator *=(hp<LIM,MAX> &A)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
multiply(A);
}
void operator *=(int A)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
multiply(A);
}
hp<LIM,MAX> operator +(hp<LIM,MAX> &A)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
hp<LIM,MAX> C(*this);
C.plus(A);
return C;
}
hp<LIM,MAX> operator -(hp<LIM,MAX> &A)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
hp<LIM,MAX> C(*this);
C.minus(A);
return C;
}
hp<LIM,MAX> operator *(int A)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
hp<LIM,MAX> C(*this);
C.multiply(A);
return C;
}
hp<LIM,MAX> operator *(hp<LIM,MAX> &A)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
hp<LIM,MAX> C(*this);
C.multiply(A);
return C;
}
};
posted on 2011-04-12 19:42
小阮 阅读(224)
评论(0) 编辑 收藏 引用 所属分类:
数据结构和算法