人类的始祖:求救,构造一种新的整数类型MEGA_INTEGER

来源:百度文库 编辑:高校问答 时间:2024/05/05 02:53:04
联合国教科文组织需要一个关于世界人口统计应用的程序,其中需要用到非常大的整数。显然C++语言提供的int类型或long类型无法满足其需要,使用float或double类型其有效数位又受限制。请构造一种新的整数类型MEGA_INTEGER,它可以使用无穷位有效整数(这只是从理论上讲而已,实际上受到硬件资源的限制),并可以做加、减、乘、除、输入、输出、等运算。请用一个演示程序检验你的设计的新类型!

提示:可以用数组,字符串,指针,复合数据类型

请哪位给出c++程序

fengerzh 提供的 MEGA_INTEGER 类是个开始,但它的一些细节楼主必须注意:

1)没有减和除的操作
2)调用 C++ 里不该调用的 C 资源管理函数:malloc( ), realloc( ) ...
3)利用非标准 C++ 数据类型:微软 C++ 的 _int64
4)实际上只是 int 而已:MEGA_INTEGER( 2147483647 ) + 1 即导致溢出

也许还有其它待改进的地方。大家继续参与。。。

这是别人写的,应该可以用:

#include<iostream.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
class MEGA_INTEGER
{
public:
MEGA_INTEGER(const _int64& x=0);
~MEGA_INTEGER()
{
free(datepoint);
}
MEGA_INTEGER(const MEGA_INTEGER& x);
MEGA_INTEGER& operator=(const MEGA_INTEGER& right);
friend ostream& operator<<(ostream& os,const MEGA_INTEGER& r);
friend ostream& operator>>(ostream& os,const MEGA_INTEGER& r);
friend const MEGA_INTEGER operator*(const MEGA_INTEGER& left,const MEGA_INTEGER& right);
friend const MEGA_INTEGER operator+(const MEGA_INTEGER& left,const MEGA_INTEGER& right);
private:
int bytecount;
void *datepoint;
};
MEGA_INTEGER::MEGA_INTEGER(const _int64& x)
{
_int64 y(x);

if(y==0)
{
bytecount=0;
datepoint=NULL;
}
else
{
if(y>>63)
y=-y;
for(int n=7;n>=0;n--)
{
if(y>>(n<<3))
{
bytecount=n+1;
break;
}
}
datepoint=malloc(bytecount);
memcpy(datepoint,&y,bytecount);
if(x>>63)
bytecount=-bytecount;
}
}
MEGA_INTEGER& MEGA_INTEGER::operator=(const MEGA_INTEGER& right)
{
if(this == &right)
return *this;
bytecount=right.bytecount;
free(datepoint);
if(bytecount==0)
datepoint=NULL;
else if(bytecount>>(sizeof(int)<<3-1))
{
datepoint=malloc(-bytecount);
memcpy(datepoint,right.datepoint,-bytecount);
}
else
{
datepoint=malloc(bytecount);
memcpy(datepoint,right.datepoint,bytecount);
}
return *this;
}
MEGA_INTEGER::MEGA_INTEGER(const MEGA_INTEGER& x)
{
bytecount=x.bytecount;
if(bytecount==0)
datepoint=NULL;
else if(bytecount>>(sizeof(int)<<3-1))
{
datepoint=malloc(-bytecount);
memcpy(datepoint,x.datepoint,-bytecount);
}
else
{
datepoint=malloc(bytecount);
memcpy(datepoint,x.datepoint,bytecount);
}
}
ostream& operator<<(ostream& os,const MEGA_INTEGER& r)
{
if(r.bytecount>>(sizeof(int)<<3-1)?-r.bytecount:r.bytecount<=sizeof(int))
{
int x=0;
if(r.bytecount>>(sizeof(int)<<3-1))
{
memcpy(&x,r.datepoint,-r.bytecount);
os << "-";
}
else
memcpy(&x,r.datepoint,r.bytecount);
os << x;
}
else
os << "too long bytes";
return os;
}
istream& operator>>(istream& is,MEGA_INTEGER& r)
{
int x;
is >> x;
MEGA_INTEGER mi(x);
r=mi;
return is;
}
const MEGA_INTEGER operator*(const MEGA_INTEGER& left,const MEGA_INTEGER& right)
{
if(left.bytecount==0)
return left;
if(right.bytecount==0)
return right;
MEGA_INTEGER x;
int leftcount,rightcount,leftsign,rightsign;
leftsign=left.bytecount>>(sizeof(int)<<3-1);
leftcount=leftsign?-left.bytecount:left.bytecount;
rightsign=right.bytecount>>(sizeof(int)<<3-1);
rightcount=rightsign?-right.bytecount:right.bytecount;
x.bytecount=leftcount+rightcount;
x.datepoint=calloc(x.bytecount,sizeof(char));
int y=0,i,j,byteuse=0;
for(i=0;i<leftcount;i++)
{
for(j=0;j<rightcount;j++)
{
int temp;
y>>=8;
y+=(*((unsigned char*)left.datepoint+i))*(*((unsigned char*)right.datepoint+j));
temp=*((unsigned char*)x.datepoint+i+j)+(unsigned char)y;
*((unsigned char*)x.datepoint+i+j)=(unsigned char)temp;
if(temp>>8)
y+=0x100;
}
if(y>>8)
{
y>>=8;
*((unsigned char*)x.datepoint+i+j)=(unsigned char)y;
byteuse=i+j;
}
}
if(byteuse+1<x.bytecount)
realloc(x.datepoint,--x.bytecount);
if(leftsign!=rightsign)
x.bytecount=-x.bytecount;
return x;
}
const MEGA_INTEGER operator+(const MEGA_INTEGER& left,const MEGA_INTEGER& right)
{
if(left.bytecount==0)
return right;
if(right.bytecount==0)
return left;
MEGA_INTEGER x;
int leftcount,rightcount,leftsign,rightsign;
leftsign=left.bytecount>>(sizeof(int)<<3-1);
leftcount=leftsign?-left.bytecount:left.bytecount;
rightsign=right.bytecount>>(sizeof(int)<<3-1);
rightcount=rightsign?-right.bytecount:right.bytecount;
x.bytecount=leftcount>rightcount?leftcount:rightcount;
x.datepoint=calloc(x.bytecount,sizeof(char));
int y,i;
if(leftsign==rightsign)
{
y=0;
for(i=0;i<(leftcount>rightcount?rightcount:leftcount);i++)
{
y>>=8;
y+=*((unsigned char*)left.datepoint+i)+*((unsigned char*)right.datepoint+i);
*((unsigned char*)x.datepoint+i)=(unsigned char)y;
}
if(leftcount>rightcount)
{
for(i=rightcount;i<leftcount;i++)
{
y>>=8;
y+=*((unsigned char*)left.datepoint+i);
*((unsigned char*)x.datepoint+i)=(unsigned char)y;
}
}
else if(leftcount<rightcount)
{
for(i=leftcount;i<rightcount;i++)
{
y>>=8;
y+=*((unsigned char*)right.datepoint+i);
*((unsigned char*)x.datepoint+i)=(unsigned char)y;
}
}
if(y>>8)
{
realloc(x.datepoint,++x.bytecount);
*((unsigned char*)x.datepoint+i)=1;
}
if(leftsign)
x.bytecount=-x.bytecount;
}
else
{
y=0x100;
int zerocount=0;
if(leftcount==rightcount)
{
for(i=leftcount-1;i>=0;i--)
{
if(((unsigned char)*((unsigned char*)left.datepoint+i))>((unsigned char)*((unsigned char*)right.datepoint+i)))
goto left;
else if(((unsigned char)*((unsigned char*)left.datepoint+i))<((unsigned char)*((unsigned char*)right.datepoint+i)))
goto right;
}
zerocount=leftcount;
}
else if(leftcount>rightcount)
{
left:
for(i=0;i<rightcount;i++)
{
y>>=8;
y+=0xff+*((unsigned char*)left.datepoint+i)-*((unsigned char*)right.datepoint+i);
*((unsigned char*)x.datepoint+i)=(unsigned char)y;
if((unsigned char)y==0)
zerocount++;
else
zerocount=0;
}
for(i=rightcount;i<leftcount;i++)
{
y>>=8;
y+=0xff+*((unsigned char*)left.datepoint+i);
*((unsigned char*)x.datepoint+i)=(unsigned char)y;
if((unsigned char)y==0)
zerocount++;
else
zerocount=0;
}
if(zerocount)
{
x.bytecount-=zerocount;
realloc(x.datepoint,x.bytecount);
zerocount=0;
}
if(leftsign)
x.bytecount=-x.bytecount;
}
else if(leftcount<rightcount)
{
right:
for(i=0;i<leftcount;i++)
{
y>>=8;
y+=0xff+*((unsigned char*)right.datepoint+i)-*((unsigned char*)left.datepoint+i);
*((unsigned char*)x.datepoint+i)=(unsigned char)y;
if((unsigned char)y==0)
zerocount++;
else
zerocount=0;
}
for(i=leftcount;i<rightcount;i++)
{
y>>=8;
y+=0xff+*((unsigned char*)right.datepoint+i);
*((unsigned char*)x.datepoint+i)=(unsigned char)y;
if((unsigned char)y==0)
zerocount++;
else
zerocount=0;
}
if(zerocount)
{
x.bytecount-=zerocount;
realloc(x.datepoint,x.bytecount);
zerocount=0;
}
if(rightsign)
x.bytecount=-x.bytecount;
}
if(zerocount)
{
x.bytecount=0;
free(x.datepoint);
x.datepoint=NULL;
}
}
return x;
}

#include<iostream.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
class MEGA_INTEGER
{
public:
MEGA_INTEGER(const _int64& x=0);
~MEGA_INTEGER()
{
free(datepoint);
}
MEGA_INTEGER(const MEGA_INTEGER& x);
MEGA_INTEGER& operator=(const MEGA_INTEGER& right);
friend ostream& operator<<(ostream& os,const MEGA_INTEGER& r);
friend ostream& operator>>(ostream& os,const MEGA_INTEGER& r);
friend const MEGA_INTEGER operator*(const MEGA_INTEGER& left,const MEGA_INTEGER& right);
friend const MEGA_INTEGER operator+(const MEGA_INTEGER& left,const MEGA_INTEGER& right);
private:
int bytecount;
void *datepoint;
};
MEGA_INTEGER::MEGA_INTEGER(const _int64& x)
{
_int64 y(x);

if(y==0)
{
bytecount=0;
datepoint=NULL;
}
else
{
if(y>>63)
y=-y;
for(int n=7;n>=0;n--)
{
if(y>>(n<<3))
{
bytecount=n+1;
break;
}
}
datepoint=malloc(bytecount);
memcpy(datepoint,&y,bytecount);
if(x>>63)
bytecount=-bytecount;
}
}
MEGA_INTEGER& MEGA_INTEGER::operator=(const MEGA_INTEGER& right)
{
if(this == &right)
return *this;
bytecount=right.bytecount;
free(datepoint);
if(bytecount==0)
datepoint=NULL;
else if(bytecount>>(sizeof(int)<<3-1))
{
datepoint=malloc(-bytecount);
memcpy(datepoint,right.datepoint,-bytecount);
}
else
{
datepoint=malloc(bytecount);
memcpy(datepoint,right.datepoint,bytecount);
}
return *this;
}
MEGA_INTEGER::MEGA_INTEGER(const MEGA_INTEGER& x)
{
bytecount=x.bytecount;
if(bytecount==0)
datepoint=NULL;
else if(bytecount>>(sizeof(int)<<3-1))
{
datepoint=malloc(-bytecount);
memcpy(datepoint,x.datepoint,-bytecount);
}
else
{
datepoint=malloc(bytecount);
memcpy(datepoint,x.datepoint,bytecount);
}
}
ostream& operator<<(ostream& os,const MEGA_INTEGER& r)
{
if(r.bytecount>>(sizeof(int)<<3-1)?-r.bytecount:r.bytecount<=sizeof(int))
{
int x=0;
if(r.bytecount>>(sizeof(int)<<3-1))
{
memcpy(&x,r.datepoint,-r.bytecount);
os << "-";
}
else
memcpy(&x,r.datepoint,r.bytecount);
os << x;
}
else
os << "too long bytes";
return os;
}
istream& operator>>(istream& is,MEGA_INTEGER& r)
{
int x;
is >> x;
MEGA_INTEGER mi(x);
r=mi;
return is;
}
const MEGA_INTEGER operator*(const MEGA_INTEGER& left,const MEGA_INTEGER& right)
{
if(left.bytecount==0)
return left;
if(right.bytecount==0)
return right;
MEGA_INTEGER x;
int leftcount,rightcount,leftsign,rightsign;
leftsign=left.bytecount>>(sizeof(int)<<3-1);
leftcount=leftsign?-left.bytecount:left.bytecount;
rightsign=right.bytecount>>(sizeof(int)<<3-1);
rightcount=rightsign?-right.bytecount:right.bytecount;
x.bytecount=leftcount+rightcount;
x.datepoint=calloc(x.bytecount,sizeof(char));
int y=0,i,j,byteuse=0;
for(i=0;i<leftcount;i++)
{
for(j=0;j<rightcount;j++)
{
int temp;
y>>=8;
y+=(*((unsigned char*)left.datepoint+i))*(*((unsigned char*)right.datepoint+j));
temp=*((unsigned char*)x.datepoint+i+j)+(unsigned char)y;
*((unsigned char*)x.datepoint+i+j)=(unsigned char)temp;
if(temp>>8)
y+=0x100;
}
if(y>>8)
{
y>>=8;
*((unsigned char*)x.datepoint+i+j)=(unsigned char)y;
byteuse=i+j;
}
}
if(byteuse+1<x.bytecount)
realloc(x.datepoint,--x.bytecount);
if(leftsign!=rightsign)
x.bytecount=-x.bytecount;
return x;
}
const MEGA_INTEGER operator+(const MEGA_INTEGER& left,const MEGA_INTEGER& right)
{
if(left.bytecount==0)
return right;
if(right.bytecount==0)
return left;
MEGA_INTEGER x;
int leftcount,rightcount,leftsign,rightsign;
leftsign=left.bytecount>>(sizeof(int)<<3-1);
leftcount=leftsign?-left.bytecount:left.bytecount;
rightsign=right.bytecount>>(sizeof(int)<<3-1);
rightcount=rightsign?-right.bytecount:right.bytecount;
x.bytecount=leftcount>rightcount?leftcount:rightcount;
x.datepoint=calloc(x.bytecount,sizeof(char));
int y,i;
if(leftsign==rightsign)
{
y=0;
for(i=0;i<(leftcount>rightcount?rightcount:leftcount);i++)
{
y>>=8;
y+=*((unsigned char*)left.datepoint+i)+*((unsigned char*)right.datepoint+i);
*((unsigned char*)x.datepoint+i)=(unsigned char)y;
}
if(leftcount>rightcount)
{
for(i=rightcount;i<leftcount;i++)
{
y>>=8;
y+=*((unsigned char*)left.datepoint+i);
*((unsigned char*)x.datepoint+i)=(unsigned char)y;
}
}
else if(leftcount<rightcount)
{
for(i=leftcount;i<rightcount;i++)
{
y>>=8;
y+=*((unsigned char*)right.datepoint+i);
*((unsigned char*)x.datepoint+i)=(unsigned char)y;
}
}
if(y>>8)
{
realloc(x.datepoint,++x.bytecount);
*((unsigned char*)x.datepoint+i)=1;
}
if(leftsign)
x.bytecount=-x.bytecount;
}
else
{
y=0x100;
int zerocount=0;
if(leftcount==rightcount)
{
for(i=leftcount-1;i>=0;i--)
{
if(((unsigned char)*((unsigned char*)left.datepoint+i))>((unsigned char)*((unsigned char*)right.datepoint+i)))
goto left;
else if(((unsigned char)*((unsigned char*)left.datepoint+i))<((unsigned char)*((unsigned char*)right.datepoint+i)))
goto right;
}
zerocount=leftcount;
}
else if(leftcount>rightcount)
{
left:
for(i=0;i<rightcount;i++)
{
y>>=8;
y+=0xff+*((unsigned char*)left.datepoint+i)-*((unsigned char*)right.datepoint+i);
*((unsigned char*)x.datepoint+i)=(unsigned char)y;
if((unsigned char)y==0)
zerocount++;
else
zerocount=0;
}
for(i=rightcount;i<leftcount;i++)
{
y>>=8;
y+=0xff+*((unsigned char*)left.datepoint+i);
*((unsigned char*)x.datepoint+i)=(unsigned char)y;
if((unsigned char)y==0)
zerocount++;
else
zerocount=0;
}
if(zerocount)
{
x.bytecount-=zerocount;
realloc(x.datepoint,x.bytecount);
zerocount=0;
}
if(leftsign)
x.bytecount=-x.bytecount;
}
else if(leftcount<rightcount)
{
right:
for(i=0;i<leftcount;i++)
{
y>>=8;
y+=0xff+*((unsigned char*)right.datepoint+i)-*((unsigned char*)left.datepoint+i);
*((unsigned char*)x.datepoint+i)=(unsigned char)y;
if((unsigned char)y==0)
zerocount++;
else
zerocount=0;
}
for(i=leftcount;i<rightcount;i++)
{
y>>=8;
y+=0xff+*((unsigned char*)right.datepoint+i);
*((unsigned char*)x.datepoint+i)=(unsigned char)y;
if((unsigned char)y==0)
zerocount++;
else
zerocount=0;
}
if(zerocount)
{
x.bytecount-=zerocount;
realloc(x.datepoint,x.bytecount);
zerocount=0;
}
if(rightsign)
x.bytecount=-x.bytecount;
}
if(zerocount)
{
x.bytecount=0;
free(x.datepoint);
x.datepoint=NULL;
}
}
return x;
}

参考资料:http://www.graphics.net.cn/bbs/c_or_cpp/0003/256.asp
回答者:fengerzh - 经理 四级 4-11 13:22

fengerzh 提供的 MEGA_INTEGER 类是个开始,但它的一些细节楼主必须注意:

1)没有减和除的操作
2)调用 C++ 里不该调用的 C 资源管理函数:malloc( ), realloc( ) ...
3)利用非标准 C++ 数据类型:微软 C++ 的 _int64

100亿也不过30位,够了

.............楼主你究竟有没有学过C++的,竟然问这种问题,白痴

人民币200还差不多