求救,构造一种新的整数类型MEGA_INTEGER

来源:百度知道 编辑:UC知道 时间:2024/09/22 15:42:11
联合国教科文组织需要一个关于世界人口统计应用的程序,其中需要用到非常大的整数。显然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);
pri