continue;
}
}
}
}
/*头文件,包括长整数数据结构的定义,成员函数的定义*/
/***********************************************************/
#include
#include
#include
usingnamespacestd;
structDblNode{
intdata;
DblNode*prior;
DblNode*next;
};
boolIsNum(chara){//判断字符a是否是便是数字
ints=a-'0';
if(s>=0&&s<10)returntrue;
elsereturnfalse;
}
boolIsInt(stringa){//判断字符串a是否表达一串数字
boolJud=1;
inti=1;
chars=a[0];
if(a=="+"||a=="-")returnfalse;
if(s=='+'||s=='-'){}
elseif(s>='1'&&s<='9'){}
elseif(a[0]=='0'&&a[1]=='\0')returntrue;
elsereturnfalse;
while(a[i]!
='\0')
{
Jud=IsNum(a[i]);
if(Jud==0)returnfalse;
i++;
}
returntrue;
}
intJudSign(strings){//返回数字的符号
if(s[0]=='-')return-1;
elseif(s[0]=='0'&&s[1]=='\0')return0;
elsereturn1;
}
intCtoI(chara){
inti=a-'0';
returni;
}
classDblList{//定义一个双向链表类,存储任意长度的数字
private:
//并可以进行标准化输出和加法,乘法。
DblNode*head,*tail;
DblNode*current;
intsign;
public:
DblList();//构造函数
~DblList();//析构函数
boolCreatList(string);//生成一个双向链表
intGetCount();//获取整数的长度
voidInsert(DblNode*);//从表尾插入一个结点
voidInsertFront(DblNode*);//从表头插入一个结点
voidClear();//清除该链表
voidoperator+(DblList&);//实现两个任意整数的加法
voidoperator*(DblList&);//实现两个任意整数的乘法
DblList&operator=(DblList&);//重载赋值运算符
intCompare(DblList&);//两个整数的绝对值比较
voidDisplay();//任意长度整数的标准化输出
};
DblList:
:
DblList(){
head=newDblNode();//构造函数
head->next=NULL;
head->prior=NULL;
tail=head;
current=NULL;
sign=0;
}
DblList:
:
~DblList(){//析构函数
while(head->next!
=NULL)
{
current=head->next;
head->next=current->next;
deletecurrent;
}
current=NULL;
sign=0;
deletehead;
head=NULL;
tail=NULL;
}
intDblList:
:
GetCount(){//返回该数字的长度(不包括符号位)
current=head->next;
intcount=0;
while(current)
{
count++;
current=current->next;
}
current=NULL;
returncount;
}
voidDblList:
:
Insert(DblNode*p){//从链表尾部插入一个结点
tail->next=p;
p->prior=tail;
tail=p;
}
voidDblList:
:
InsertFront(DblNode*q){//从链表头部插入一个结点
if(head->next==NULL)
{
head->next=q;
q->prior=head;
tail=q;
}
else{
q->next=head->next;
head->next->prior=q;
head->next=q;
q->prior=head;
}
}
boolDblList:
:
CreatList(strings){//输入的任意长度的表示数字的字符串
boolj=IsInt(s);//以此生成双向链表
if(!
j)returnj;
else{
inti=0;
sign=JudSign(s);
if(s[0]=='+'||s[0]=='-')i++;
while(s[i]!
='\0')
{
intia=CtoI(s[i]);
current=newDblNode();
current->data=ia;
current->next=NULL;
current->prior=NULL;
Insert(current);
i++;
current=NULL;
}
returntrue;
}
}
voidDblList:
:
Clear(){
while(head->next)
{
current=head->next;
head->next=current->next;
deletecurrent;
}
tail=head;
sign=0;
current=NULL;
}
intDblList:
:
Compare(DblList&s){//任意两个长度数字绝对值比较
inta=GetCount();
intb=s.GetCount();
if(a>b)return1;
elseif(a
else
{
current=head->next;
s.current=s.head->next;
while(current!
=NULL)
{
intre=current->data-s.current->data;
if(re>0)return1;
elseif(re<0)return-1;
else
{
current=current->next;
s.current=s.current->next;
}
}
current=NULL;
s.current=NULL;
return0;
}
}
DblList&DblList:
:
operator=(DblList&s){
Clear();
sign=s.sign;
s.current=s.head->next;
while(s.current!
=NULL)
{
current=newDblNode();
current->data=s.current->data;
Insert(current);
s.current=s.current->next;
}
s.current=NULL;
current=NULL;
return*this;
}
voidDblList:
:
operator+(DblList&s){//实现加法(包括减法)
DblListtemp;
intda;
intf=0;
intsi=Compare(s);
if(si==0&&(sign+s.sign==0))temp.sign=0;
else
{
if(si==0)temp.sign=sign;
elseif(si>0)temp.sign=sign;
elsetemp.sign=s.sign;
current=tail;
s.current=s.tail;
while
(1)
{
if(current==head&&s.current==s.head)
{
if(f)
{
da=f;
temp.current=newDblNode();
temp.current->data=f;
temp.InsertFront(temp.current);
}
if(!
f)break;
f=0;
}
elseif(current!
=head&&s.current==s.head)
{
temp.current=newDblNode();
temp.current->data=current->data+f;
temp.InsertFront(temp.current);
current=current->prior;
f=0;
}
elseif(current==head&&s.current!
=s.head)
{
temp.current=newDblNode();
temp.current->data=s.current->data+f;
temp.InsertFront(temp.current);
s.current=s.current->prior;
f=0;
}
else{
da=current->data*sign+s.current->data*s.sign+f;
if(da*temp.sign>=10)
{
da=da-10*temp.sign;
f=temp.sign;
}
elseif(da*temp.sign<0)
{
da=da+10*temp.sign;
f=-temp.sign;
}
elsef=0;
temp.current=newDblNode();
temp.current->next=NULL;
temp.current->data=abs(da);
temp.InsertFront(temp.current);
current=current->prior;
s.current=s.current->prior;
}
}
current=NULL;
s.current=NULL;
}
temp.current=temp.head->next;
if(temp.current!
=NULL)
while(temp.current->data==0)
{
temp.head->next=temp.current->next;
deletetemp.current;
temp.current=temp.head->next;
}
temp.current=NULL;
cout<<"num1+num2=";
temp.Display();
}
voidDblList:
:
operator*(DblList&s){//实现乘法
intcf=0;
intans;
inti,j;
intcount=0;
DblListtemp;
temp.sign=sign*s.sign;
inta1=GetCount();
inta2=s.GetCount();
inta=a1+a2;
for(i=0;i{
temp.current=newDblNode();
temp.current->data=0;
temp.current->next=NULL;
temp.current->prior=NULL;
temp.InsertFront(temp.current);
}
s.current=s.tail;
while(s.current!
=s.head){
current=tail;
temp.current=temp.tail;
for(i=0;iprior;
for(j=0;jans=s.current->data*current->data+temp.current->data+cf;
temp.current->data=ans%10;
cf=ans/10;
current=current->prior;
temp.current=temp.current->prior;
}
if(cf!
=0)
{
temp.current->data=temp.current->data+cf;
cf=0;
}
s.current=s.current->prior;
temp.current=temp.tail;
count++;
}
if(temp.head->next->data==0)
{
temp.current=temp.head->next;
temp.head->next=temp.current->next;
deletetemp.current;
temp.current=NULL;
}
cout<<"num1*num2=";
temp.Display();
}
voidDblList:
:
Display(){//任意长数字的标准化输出
intcount=GetCount();
if(sign==0)
{
cout<<"0"<return;
}
elseif(sign==-1)cout<<"-";
current=head->next;
while(current!
=NULL)
{
if(count>0){
cout<data;
count--;
if(count%3==0&&count!
=0)cout<<",";
current=current->next;
}
}
current=NULL;
cout<cout<<"--------------------------------------------------------------"<}