社区
C语言
帖子详情
构造一种新的数据类型
to2008
2001-01-31 09:37:00
高教版《面向对象程序设计基础(C++语言描述)》有一道习题:请构造一种新的数据类型MEGA_INTEGER,它可以使用无穷位有效整数,并可作加、减、乘、除、输入、输出等运算。
请各高手帮忙给个答案。
...全文
257
13
打赏
收藏
构造一种新的数据类型
高教版《面向对象程序设计基础(C++语言描述)》有一道习题:请构造一种新的数据类型MEGA_INTEGER,它可以使用无穷位有效整数,并可作加、减、乘、除、输入、输出等运算。 请各高手帮忙给个答案。
复制链接
扫一扫
分享
转发到动态
举报
写回复
配置赞助广告
用AI写文章
13 条
回复
切换为时间正序
请发表友善的回复…
发表回复
打赏红包
to2008
2001-02-25
打赏
举报
回复
还有别的源程序吗?
to2008
2001-02-07
打赏
举报
回复
确实是无穷位,当然是受内存限制的无穷位。
jiang_jixuan
2001-02-07
打赏
举报
回复
用内存分配,来模拟变长的数组来实现其功能!
jiang_jixuan
2001-02-07
打赏
举报
回复
用内存分配,来模拟变长的数组来实现其功能!
hornedreaper
2001-02-07
打赏
举报
回复
以下是我在vc下写的原代码,我懒得写注释,解释以下。
class MEGA_INTEGER实现该数据类型.
用bytecount存放数据的长度,按byte计算,而且用bytecount的正负来表示MEGA_INTEGER的正负,当bytecount为0,该整数也为0(此时datepoint为null).
用datepoint动态分配的void指针.
实现了构造函数,拷贝构造函数,解析函数和赋值=,二元加+,乘*,输出<<,输入>>5个运算符(-和/懒得写)
*和+都实现了不同符号的MEGA_INTEGER之间的运算,而且长度不限,动态调整该数的长度。
<<和>>没有考虑超过int型的输入和输出。
希望高手指正!
#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;
}
to2008
2001-02-06
打赏
举报
回复
数组如何存无穷位?
有答案的各位高人,请赐源程序。
mathematica
2001-02-06
打赏
举报
回复
朋友,我早几天帮别人写了一下这个东西的算法,以后整理出来后再告诉你。
mathematica_5@263.net
dongyingtao
2001-02-06
打赏
举报
回复
这个数据结构实际上是表达了一种变长的行为,用数组(动态)、链表等都可以
不过,我建议还是用STL的vector比较好,它可以满足你的要求,效率也不错
easyr
2001-02-06
打赏
举报
回复
我好象 听说过 用指针解决具体 怎么样 不太清楚!~ 不好意思啊 帮不了你
我不懂电脑
2001-02-06
打赏
举报
回复
AutoAsm() 对,我的精练也是用数组,补充一下:可以用指针做数组,再使用动态分配内存法,动态改变数组大小,适应数据范围。但还是要受到计算机内存大小的限制。
ed9er
2001-02-06
打赏
举报
回复
应该是任意(any given)位,而不是无穷(infinite)位吧
这些垃圾作者语文很差的
AutoAsm
2001-02-02
打赏
举报
回复
既然是无穷位的,那int这一派肯定就不行,根据我的经验是用数组来做。数组的每个 元素存一位数,如果位数不够了还可以再分配
SunnyWay
2001-02-01
打赏
举报
回复
先去找那本书的 参考答案, 呵呵
看看出题人是走哪个思路的。
【C语言】
构造
数据类型
(自定义
数据类型
)相关知识点
C语言中的
构造
数据类型
有:数组类型、结构体类型、共用体类型和枚举类型。
java学习—
构造
器(
构造
方法)
java中的
构造
方法是
一种
特殊的方法,用于初始化对象。java
构造
函数在对象创建时被调用。它
构造
值,即提供对象的数据。 创建java
构造
函数的规则
构造
函数基本上定义了两个规则。分别如下:
构造
函数名必须与其类名称相同
构造
函数必须没有显式返回类型 Java
构造
函数类型 有两种类型的
构造
函数 默认
构造
函数(无参数
构造
函数) 参数化
构造
函数 1.Java默认
构造
函数 没...
「自定义类型」C语言中的
构造
数据类型
如结构,联合,枚举
❤️🔥❤️🔥C语言中的
构造
数据类型
如结构,联合,枚举
数据类型
与
数据类型
分类
数据类型
就是数据的属性和操作,决定了数据在程序中的存在和使用方式。int类型属性:代表一个整数值,该类型值共享整数的属性。 int类型:操作-可以改变int类型值的符号,两个int类型值可以相加、相减、相乘、相处、求模。注意:char也属于整数类型,因为它实际上存储的是整数,而不是字符(转换关系见ASCII码表)。
数据类型
包括:基本
数据类型
、派生
数据类型
和抽象
数据类型
; 基本
数据类型
包括整数类型和浮点数类型; 派生
数据类型
包括布尔类型、指针和聚合类型。聚合类型又包括数组、结构体、枚举和联合。 其关系如下图所
二叉树的抽象
数据类型
二叉树的抽象
数据类型
一. 理论分析 结点是二叉树的基础,通常主要用结点保存与应用有关的信息. 作为二叉树的表示,还需要记录二叉树的结构信息,至少需要保证能检查结点的父子关系,例如,能从一个结点找到其左/右子结点 下面 一个基本的二叉树抽象
数据类型
的定义: ADT BinTree: # 一个二叉树抽象
数据类型
BinTree(self, data, left, right) #
构造
操作,创建一个
新
二叉树 is_empty(self) # 判断self是否为一个空二叉树
C语言
70,037
社区成员
243,243
社区内容
发帖
与我相关
我的任务
C语言
C语言相关问题讨论
复制链接
扫一扫
分享
社区描述
C语言相关问题讨论
社区管理员
加入社区
获取链接或二维码
近7日
近30日
至今
加载中
查看更多榜单
社区公告
暂无公告
试试用AI创作助手写篇文章吧
+ 用AI写文章