社区
C语言
帖子详情
构造一种新的数据类型
to2008
2001-01-31 09:37:00
高教版《面向对象程序设计基础(C++语言描述)》有一道习题:请构造一种新的数据类型MEGA_INTEGER,它可以使用无穷位有效整数,并可作加、减、乘、除、输入、输出等运算。
请各高手帮忙给个答案。
...全文
244
13
打赏
收藏
构造一种新的数据类型
高教版《面向对象程序设计基础(C++语言描述)》有一道习题:请构造一种新的数据类型MEGA_INTEGER,它可以使用无穷位有效整数,并可作加、减、乘、除、输入、输出等运算。 请各高手帮忙给个答案。
复制链接
扫一扫
分享
转发到动态
举报
AI
作业
写回复
配置赞助广告
用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
打赏
举报
回复
先去找那本书的 参考答案, 呵呵
看看出题人是走哪个思路的。
基本
数据类型
与
构造
数据类型
PS:本文以C++为例介绍
数据类型
。
数据类型
从广义上根据量的值是否可变,可分为常量和变量两种
数据类型
。 根据数据复杂程度,划分为基本
数据类型
和
构造
数据类型
这两大类。 基本
数据类型
包括:整型(short,int,long)、实型(又称浮点型)(float和double)、字符型char、布尔型bool等。
构造
数据类型
包括:数组类型、枚举类型enum、指针类型...
常用数据结构(
构造
类型)
一、结构体 将一些有关变量组织起来定义成一个结构(structure),用来表示一个有机整体或
一种
新
的类型,这样程序可以像处理内部的基本数据一样来对结构进行各种操作。 1.1结构体类型 ????① 概念 结构体是
一种
构造
类型,由若干成员组成。其中每一个成员可以是一个基本
数据类型
或者是一个
构造
类型。 一般形式: struct Product { char name[10]; char color[10]; int price; }; ????② 结构体变
C语言
数据类型
(2)——
构造
数据类型
数据类型
数组名[常量表达式];“
数据类型
”: 是数组元素的
数据类型
。“数组名”: 遵循C语言标识符规则。“常量表达式”:表示数组中有多少个元素,即数组的长度。它可以是整型常量、整型常量表达式或符号常量。定义了一个一维数组a,int表示数组中a中的每一个元素都是整型的,数组名为a,此数组有5个元素。
【C语言】
构造
数据类型
(自定义
数据类型
)相关知识点
C语言中的
构造
数据类型
有:数组类型、结构体类型、共用体类型和枚举类型。
「自定义类型」C语言中的
构造
数据类型
如结构,联合,枚举
❤️🔥❤️🔥C语言中的
构造
数据类型
如结构,联合,枚举
C语言
70,020
社区成员
243,265
社区内容
发帖
与我相关
我的任务
C语言
C语言相关问题讨论
复制链接
扫一扫
分享
社区描述
C语言相关问题讨论
社区管理员
加入社区
获取链接或二维码
近7日
近30日
至今
加载中
查看更多榜单
社区公告
暂无公告
试试用AI创作助手写篇文章吧
+ 用AI写文章