主题:[原创]大数类 hugeint
#include<iostream.h>
class hugeint
{
public:
hugeint(int);
hugeint(long);
~hugeint();
hugeint& operator = (char*);
friend ostream& operator << (ostream&,hugeint&);
protected:
int *data;
long size;
};
#include<string.h>
#include<iomanip.h>
#include "hugeint.h"
hugeint::hugeint(int n)
{
size=(long)n/4;
data=new int[size];
for(long i=0;i<size;++i)data[i]=0;
}
hugeint::hugeint(long n)
{
size=n/4;
data=new int[size];
for(long i=0;i<size;++i)data[i]=0;
}
hugeint::~hugeint()
{
delete[]data;
}
hugeint& hugeint::operator = (char *str)
{
long len=strlen(str),count=0;
if(len>(size<<2))return *this;
char *p=str+len-1;
int t,i;
while(1)
{
if((p-str)<4)break;
t=0;
char *ph=p-3;
for(int i=0;i<4;++i)
{
t*=10;
t+=ph[i]-'0';
}
data[count++]=t;
p-=4;
}
t=0;
for(i=0;i<=p-str;++i)
{
t*=10;
t+=str[i]-'0';
}
data[count]=t;
return *this;
}
ostream& operator <<(ostream& out,hugeint& a)
{
int *p=a.data+a.size-1;
while(!*p)--p;
out<<*p--;
while(p>(a.data-1))
out<<setw(4)<<setfill('0')<<*p--;
return out;
}
#include "hugeint.h"
void main()
{
hugeint a(1000);
a="451234567864512164800012300000000";
cout<<a<<endl;
}
class hugeint
{
public:
hugeint(int);
hugeint(long);
~hugeint();
hugeint& operator = (char*);
friend ostream& operator << (ostream&,hugeint&);
protected:
int *data;
long size;
};
#include<string.h>
#include<iomanip.h>
#include "hugeint.h"
hugeint::hugeint(int n)
{
size=(long)n/4;
data=new int[size];
for(long i=0;i<size;++i)data[i]=0;
}
hugeint::hugeint(long n)
{
size=n/4;
data=new int[size];
for(long i=0;i<size;++i)data[i]=0;
}
hugeint::~hugeint()
{
delete[]data;
}
hugeint& hugeint::operator = (char *str)
{
long len=strlen(str),count=0;
if(len>(size<<2))return *this;
char *p=str+len-1;
int t,i;
while(1)
{
if((p-str)<4)break;
t=0;
char *ph=p-3;
for(int i=0;i<4;++i)
{
t*=10;
t+=ph[i]-'0';
}
data[count++]=t;
p-=4;
}
t=0;
for(i=0;i<=p-str;++i)
{
t*=10;
t+=str[i]-'0';
}
data[count]=t;
return *this;
}
ostream& operator <<(ostream& out,hugeint& a)
{
int *p=a.data+a.size-1;
while(!*p)--p;
out<<*p--;
while(p>(a.data-1))
out<<setw(4)<<setfill('0')<<*p--;
return out;
}
#include "hugeint.h"
void main()
{
hugeint a(1000);
a="451234567864512164800012300000000";
cout<<a<<endl;
}