巨型整型运算重载--实现部分(.cpp)
#define MAXLEN 1000//定义巨型数据的最大长度 
#include < conio.h > 
//*----------------------------各构造函数的实现[begin]---------------------------*// 
//------------------HugeInt类构造函数(不带参数 使用默认值0初始化)---------------// 
/* 
函数名:HugeInt类构造函数 
传入参数:字符指针 
功能:实现使用默认值0初始化类对象 
*/ 
HugeInt::HugeInt() 
{ 
//初始化一带头结点的空双向循环链表// 
RightEnd=0; 
node *temp=new node; 
temp->left=temp; 
temp->right=temp; 
head=temp; 
head->data=0; 
RightEnd=head; 
n=0; 
} 
//------------------HugeInt类构造函数(参数类型 字符串)----------------------------// 
/* 
函数名:HugeInt类构造函数 
传入参数:字符指针 
功能:实现使用字符串初始化类对象 
*/ 
HugeInt::HugeInt(char *data) 
{ 
//初始化一带头结点的空双向循环链表// 
RightEnd=0; 
node *temp=new node; 
temp->left=temp; 
temp->right=temp; 
head=temp; 
head->data=0; 
RightEnd=head; 
n=0; 
//接收传入的参数 
char *tempdata=new char[strlen(data)+1]; 
strcpy(tempdata,data); 
//如果输入的数据串合法 则将其分段并存储在链表中// 
//说明:比如字符串"000100000632000"插入到链表中各结点为0,1000,63,2000// 
if(IsRight(data)) 
{ //根据符号设置头结点值 负数为1 正数为0// 
if(data[0]=='-') 
{head->data=1;} 
else 
{head->data=0;} 
//取输入数据串的绝对值 
char *newdata=new char[strlen(tempdata)+1]; 
strncpy(newdata,abs(tempdata),strlen(newdata)); 
short int len=strlen(newdata);//len 数据串的位数 不包括符号 
//当数据的位数大于分段的单位4时 以4位一组作为结点循环插入到链表// 
while(len>=4) 
{ 
char temp[5]={0}; 
strncpy(temp,newdata+len-4,4);//从数据串尾部开始 截取4位长 
Insert(atoi(temp));// atoi() 将数据转为整型后插入 每次都是在头结点后插入 
len=len-4; 
}//end while 
//执行上述循环后 将剩下的位数做为一个结点插入 
if(len>0&&len<4) 
{ 
char temp[4]={0}; 
strncpy(temp,newdata,len); 
Insert(atoi(temp)); 
}//end if 
}//end if 
else//数据不合法的情况 默认值为0 
{ 
Insert(0); 
} 
} 
//------------------HugeInt类构造函数(参数类型 长整型)----------------------------// 
/* 
函数名:HugeInt类构造函数 
传入参数:长整型 
功能:实现使用长整型初始化类对象 
*/ 
HugeInt::HugeInt(const long k) 
{ 
//定义一空的双向循环链表// 
RightEnd=0; 
node *temp=new node; 
temp->left=temp; 
temp->right=temp; 
head=temp; 
RightEnd=head; 
head->data=0; 
n=0; 
//根据符号设置头结点值 负数为1 正数为0// 
if(k<0) 
{head->data=1;} 
else 
{head->data=0;} 
//接收传入的参数并转为字符串 
char* str=new char[sizeof(k)+1]; 
itoa(k,str,10); 
//如果输入的数据串合法 则将其分段并存储在链表中// 
if(IsRight(str)) 
{ 
//取数据串的绝对值 
char *newdata=new char[strlen(str)+1]; 
strncpy(newdata,abs(str),strlen(newdata)); 
short int len=strlen(newdata); 
//当数据的位数大于分段的单位4时 以4位一组作为结点循环插入到链表// 
while(len>=4) 
{ char temp[5]={0}; 
strncpy(temp,newdata+len-4,4);//从数据串尾部开始 截取4位 
Insert(atoi(temp));// atoi() 将数据转为整型后插入 每次都是在头结点后插入 
len=len-4; 
}//end while 
//执行上述循环后 将剩下的位数做为一个结点插入 
if(len>0&&len<4) 
{ char temp[4]={0}; 
strncpy(temp,newdata,len);//从数据串头部开始 截取len位 
Insert(atoi(temp)); 
}//end if 
}//end if 
else//数据不合法的情况 默认值为0 
{ 
Insert(0); 
} 
} 
//------------------HugeInt类拷贝构造函数----------------------------// 
/* 
函数名:HugeInt类拷贝构造函数 
传入参数:类对象的引用 
功能:实现使用已知类对象初始化类 
*/ 
HugeInt::HugeInt(HugeInt& h) 
{ //定义一空的双向循环链表// 
n=0; 
RightEnd=0; 
node *temp=new node; 
temp->left=temp; 
temp->right=temp; 
head=temp; 
head->data=h.GetHeadNode()->data;//设置头结点 
RightEnd=head;//设置尾结点 
//获得链表各结点值 
for(temp=h.RightEnd;temp!=h.GetHeadNode();temp=temp->left) 
{ 
Insert(temp->data); 
} 
} 
/*----------------------------HugeInt类析构函数---------------------------*/ 
HugeInt::~HugeInt() 
{ 
DelList();//删除所有结点 
delete head;//删除头结点 
} 
/*----------------------------各构造函数的实现[end]---------------------------*/ 
/*----------------------------各成员函数的实现[begin]---------------------------*/ 
//------------------插入结点(HugeInt类成员函数)----------------------------// 
/* 
函数名:Insert 
参数: const short int x(常短整型) 
返回值:this对象 
功能:实现在双链表头结点(head)后插入新结点 
*/ 
HugeInt& HugeInt::Insert(const short int x) 
{//在头结点后插入新结点 
node *temp=new node; 
temp->data=x; 
temp->left=head; 
temp->right=head->right; 
head->right->left=temp; 
head->right=temp; 
n++; 
//因每次都是在头结点后插入新结点 故只需移动一次尾结点 即当n==1时 
if(n==1) 
{RightEnd=RightEnd->right;} 
return *this; 
} 
//------------------删除所有结点(HugeInt类成员函数)----------------------------// 
void HugeInt::DelList() 
{ 
while(n!=0) 
{node *temp=new node; 
temp=GetEndNode(); 
RightEnd=RightEnd->left; 
temp->right->left=temp->left; 
temp->left->right=temp->right; 
delete temp; 
n--; 
} 
} 
//------------------求数据串绝对值(重载abs函数)---------------------------// 
/* 
函数名:abs 
传入参数:char *data (字符指针) 
返回值:字符指针 
功能:返回数据串的绝对值(如"-563256223354" 返回"563256223354") 
*/ 
char* HugeInt::abs(char *data) 
{ 
char* newdata=new char[strlen(data)+1]; 
if(data[0]=='-') 
{return strncpy(newdata,data+1,strlen(newdata)-1);}//返回不带负号的数据串 
else 
return strncpy(newdata,data,strlen(newdata)); 
} 
//-----------------检验数据合法性(HugeInt类成员函数)----------------------------------// 
/* 
函数名:IsRight 
传入参数:char *data (字符指针) 
返回值:bool型(true 合法.false非法) 
功能:判断输入的数据是否合法 
说明:在本程序中所谓的合法是指输入的字符串中的每个字符只能为数字(0~9) 
允许首字符为"+"与"-"(正负号) 
比如合法的字符串: "+0563256","-55115100016","454512053566"等 
非法的字符串:"--5632266","df5df12515","dfgjkr"等 
*/ 
bool HugeInt::IsRight(char *data) 
{ 
bool flag=false;//定义是否合法标志 做为返回值 
if(strlen(data)>MAXLEN)//字符串长度超过定义的最大长度 
{ cout<<"字符串长度超过定义的最大长度/n"; 
return flag; 
}//end if 
else//字符串长度合法的情况 
{ 
if((data[0]=='-'||data[0]=='+')&&strlen(data)!=1)//判断首字符 
{flag=true;} 
if(strlen(data)==1&&data[0]-48>=0&&data[0]-48<=9)//如果为单字符 
{flag=true;} 
//如果flag==false 则从第一位开始判断 否则 
//从字符串第二位开始 判断字符是否为数字字符// 
short int i=flag; 
while(i < strlen(data)) 
{ 
if(data[i]-48>=0&&data[i]-48<=9) 
{flag=true;} 
else 
{flag=false;break;}//只要发现一个非法字符 则退出循环 
i++; 
}//end while 
if(!flag) 
cout<<"[错误]数据不合法[数据不合法的情况 本程序返回默认值0]/n"; 
return flag; 
}//end else 
} 
//------------------------输出结点(HugeInt类成员函数)------------------------------// 
/* 
函数名:PrintList 
参数:无 
返回值:void 
功能:实现链表的输出 
说明:如数据1265300065632存储在链表中 各结点为1,2653,6,5632 
显然不能简单的将结点直接输出 要补上相应的0 
*/ 
void HugeInt::PrintList()const 
{ node *current=new node; 
if (head->data==1)//数据串为负数的情况 
{ 
if(head->right->data!=0)//如果第一个结点不为0则输出"-"与该结点数据 
{cout<<"-"<< head-> right->data;} 
else//如果第一个结点为0 
{ 
if(head->right->right==head)//如果只有一个结点(也就是0) 
{cout<< head->right->data;}//输出0 
else//不止一个结点 需要输出负号 
cout<<"-"; 
} 
}//数据串为负数的情况结束 
else//数据串为正数的情况 
{ 
//同样也是先判断第一个结点的输出 
if((head->right->data!=0)) 
{cout<<head->right->data;} 
else 
if(head->right->right==head)//如果只有一个结点(也就是0) 
{cout<<head->right->data;}//输出0 
} 
bool f=false;//标识在当前结点前是否已

 
                   
                   
                   
                   本文详细介绍了巨型整数(HugeInt)类的构造函数和运算符重载实现,包括从字符串、长整型初始化,拷贝构造,以及插入、删除节点等操作。还展示了对巨型整数进行比较、除法、赋值等运算符的重载实现,确保了巨型整数的正确运算。
本文详细介绍了巨型整数(HugeInt)类的构造函数和运算符重载实现,包括从字符串、长整型初始化,拷贝构造,以及插入、删除节点等操作。还展示了对巨型整数进行比较、除法、赋值等运算符的重载实现,确保了巨型整数的正确运算。
           最低0.47元/天 解锁文章
最低0.47元/天 解锁文章
                           
                       
       
           
                 
                 
                 
                 
                 
                
               
                 
                 
                 
                 
                
               
                 
                 扫一扫
扫一扫
                     
              
             
                   1591
					1591
					
 被折叠的  条评论
		 为什么被折叠?
被折叠的  条评论
		 为什么被折叠?
		 
		  到【灌水乐园】发言
到【灌水乐园】发言                                
		 
		 
    
   
    
   
             
            


 
            