#include
using namespace std;
#include
#define OK 1;
#define FALSE 0;
typedef struct lnode{//结点结构体
int data;
struct lnode *next;
struct lnode *prior;
}lnode,*lnodelist;
int compare(lnodelist &ahead,lnodelist &bhead,int &q)//比较a和b的大小
{
lnode *pa,*pb;
int bcount=0,acount=0;//定义ab的节点长度
pa=ahead->next;pb=bhead->next;
while(pa!=ahead)
{acount++;pa=pa->next;}
while(pb!=bhead)
{bcount++;pb=pb->next;}
if(acount>bcount)q=1;
else if(acount
else//a,b节点个数相同时
{
pa=ahead->prior;//a的尾节点
pb=bhead->prior;
while((pa->data==pb->data)&&(pa->prior!=ahead))
{pa=pa->prior;pb=pb->prior;}//分别比较每个节点的大小
if(pa->data>pb->data)q=1;
else if(pa->datadata)q=-1;
else q=0;
}return 0;
}
int inputa(lnodelist &ahead)//输入长整数a
{
lnode *p;char ch;
int afirst;
cout<<"请输入第一个无符号长整型数,要求每四位用逗号隔开末尾为分号:"<<endl;
p=ahead=new lnode;//头结点
p->data=0;
p->next=p;
p->prior=p;
cin>>afirst>>ch;//输入第一个结点数据
if(afirst<0)
{ahead->data=-1;afirst=-afirst;}
else ahead->data=1;
p=new lnode;
p->data=afirst;
p->next=ahead;
ahead->prior=p;
ahead->next=p;
p->prior=ahead;
while(ch!=';')
{
cin>>afirst>>ch;
p=new lnode;
p->data=afirst;
p->next=ahead->next;
ahead->next->prior=p;
ahead->next=p;
p->prior=ahead;
}
return OK;
}
int inputb(lnodelist &bhead)//输入长整数b
{
lnode *p;char ch;
int bfirst;
cout<<"请输入第二个无符号长整型数,要求每四位用逗号隔开末尾为分号:"<<endl;
p=bhead=new lnode;//头结点
p->data=0;
p->next=p;
cin>>bfirst>>ch;//输入第一个结点数据
if(bfirst<0)
{bhead->data=-1;bfirst=-bfirst;}
else bhead->data=1;
p=new lnode;
p->data=bfirst;
p->next=bhead;
bhead->prior=p;
bhead->next=p;
p->prior=bhead;
while(ch!=';')
{
cin>>bfirst>>ch;
p=new lnode;
p->data=bfirst;
p->next=bhead->next;
bhead->next->prior=p;
bhead->next=p;
p->prior=bhead;
}
return OK;
}
void putoutc(lnode *chead)//输出结果
{
lnode *s,*pr;
pr=chead->next;
if(pr!=chead)
{cout<<pr->data;pr=pr->next;}
while(pr!=chead)
{ if(pr->data==0)
cout<<"0000";
else if(pr->data>0&&pr->data<10)
cout<<"000";
else if(pr->data>=10&&pr->data<100)
cout<<"00";
else if(pr->data>=100&&pr->data<1000)
cout<<"0";
cout<<pr->data;
s=pr;
pr=pr->next;
delete s;
}cout<<endl;
}
int unsigndeadd(lnodelist &ahead,lnodelist &bhead)//无符号长整数的加法
{
int sum,carry=0; //进位
lnode *pa,*pb;
lnode *p,*chead;
pa=ahead->next;
pb=bhead->next;
p=chead=new lnode;//头结点
p->data=0;
p->next=p;
p->prior=p;
while(pa!=ahead&&pb!=bhead)
{sum=pa->data+pb->data+carry;
p=new lnode;
p->data=sum000;
carry = sum/10000;
p->next=chead->next;
chead->next->prior=p;
chead->next=p;
p->prior=chead;
pa=pa->next;
pb=pb->next;
}
if(pa!=ahead)//a还没有处理完,把a剩下的数字加到和上
{while(pa!=ahead)
{
sum=pa->data+carry;
p=new lnode;
p->data=sum000;
p->next=chead->next;
chead->next->prior=p;
chead->next=p;
p->prior=chead;
carry = sum/10000;
pa=pa->next;
}
}
if(pb!=bhead)//b还没有处理完,把b剩下的数字加到和上
{while(pb!=bhead)
{
sum=pb->data+carry;
p=new lnode;
p->data=sum000;
p->next=chead->next;
chead->next->prior=p;
chead->next=p;
p->prior=chead;
carry = sum/10000;
pb=pb->next;
}
}
if(carry)//如果最后一位有进位,就申请一个结点存储
{p=new lnode;
p->data=carry;
p->next=chead->next;
chead->next->prior=p;
chead->next=p;
p->prior=chead;
}
putoutc(chead);
return OK;
}
int unsigndesub(lnodelist &ahead,lnodelist &bhead)//无符号长整数的减法a比b大。
{
int diffe,borrow=0; //借位
lnode *pa,*pb,*chead;
lnode *p;
pa=ahead->next;
pb=bhead->next;
p=chead=new lnode;//头结点
p->data=0;
p->next=p;
p->prior=p;
while(pa!=ahead&&pb!=bhead)
{
diffe=pa->data-borrow-pb->data;
if (diffe<0){borrow=1;diffe+=10000;}else borrow=0;
p=new lnode;//存储差
p->data=diffe;
p->next=chead->next;
chead->next->prior=p;
chead->next=p;
p->prior=chead;
pa = pa->next;
pb = pb->next;
}
if(pa!=ahead)
{while(pa!=ahead)
{diffe=pa->data-borrow;
if (diffe<0){borrow=1;diffe+=10000;}else borrow=0;
p=new lnode;//存储差
p->data=diffe;
p->next=chead->next;
chead->next->prior=p;
chead->next=p;
p->prior=chead;
pa=pa->next;
}
}
while (p->data==0&&p->next!=chead)
{
lnode *s;
s=p;p=p->next;
chead->next=p;
p->prior=chead;
delete s;
}
putoutc(chead);
return OK;
}
int add(lnodelist &ahead,lnodelist &bhead)
{
int e,acount=0,bcount=0,q;
e=ahead->data*bhead->data;
cout<<"输出运算结果为:"<<endl;
if(e==1)
{
if(ahead->data==-1)
{
cout<<"-";unsigndeadd(ahead,bhead);
}
else unsigndeadd(ahead,bhead);
}
else
{
compare(ahead,bhead,q);
if(q==1)
{
if(ahead->data==-1)
{
cout<<"-";unsigndesub(ahead,bhead);
}
else unsigndesub(ahead,bhead);
}
else
{
if(ahead->data==1)
{
cout<<"-";unsigndesub(bhead,ahead);
}
else unsigndesub(bhead,ahead);
}
}return 0;
}
int sub(lnodelist &ahead,lnodelist &bhead)
{
int e,q;
e=ahead->data*bhead->data;
cout<<"输出运算结果为:"<<endl;
if(e==-1)
{
if(ahead->data==-1)
{
cout<<"-";unsigndeadd(ahead,bhead);
}else{unsigndeadd(ahead,bhead);}
}
else
{
compare(ahead,bhead,q);
if(q==1)
{
if(ahead->data==-1)
{
cout<<"-";unsigndesub(ahead,bhead);
}
else unsigndesub(ahead,bhead);
}
else
{
if(ahead->data==1)
{
cout<<"-";unsigndesub(bhead,ahead);
}
else unsigndesub(bhead,ahead);
}
}return 0;
}
int mul(lnodelist &ahead,lnodelist &bhead)
{
long multi,carry; //进位
int e;
lnode *pa,*pb,*pc,*cnow;
lnode *p,*chead;
e=ahead->data*bhead->data;
cout<<"输出运算结果为:"<<endl;
if(e==-1){cout<<"-";}
pa=ahead->next;
pb=bhead->next;
p=chead=new lnode;//头结点
p->data=0;
p->next=p;
p->prior=p;
pc=cnow=chead;
while(pb!=bhead)
{
carry=0;
while(pa!=ahead)
{
multi=pa->data*pb->data+carry;
carry = multi/10000;
if(pc->prior==chead)
{
p=new lnode;
p->data=multi000;
p->next=chead->next;
chead->next->prior=p;
chead->next=p;
p->prior=chead;
}
else
{
multi=pc->prior->data+multi000;
pc->prior->data=multi000;
carry=carry+multi/10000;
}
pc=pc->prior;
pa=pa->next;
}
if(carry!=0)
{
p=new lnode;
p->data=carry;
p->next=chead->next;
chead->next->prior=p;
chead->next=p;
p->prior=chead;
}
cnow=cnow->prior;
pc=cnow;
pa=ahead->next;
pb=pb->next;
}
putoutc(chead);
return OK;
}
int divput(lnode *chead)
{
lnode *s,*pr;
pr=chead->prior;
if(pr!=chead)
{ cout<<pr->data;pr=pr->prior;}
while(pr!=chead)
{
if(pr->data==0)
cout<<"0000";
else if(pr->data>0&&pr->data<10)
cout<<"000";
else if(pr->data>=10&&pr->data<100)
cout<<"00";
else if(pr->data>=100&&pr->data<1000)
cout<<"0";
cout<<pr->data;
s=pr;
pr=pr->prior;
delete s;
}
cout<<endl;
return 0;
}
int div(lnodelist &ahead,lnodelist &bhead)
{
cout<<"输出运算结果为:"<<endl;
lnode *pa,*pb,*s,*p;
int borrow=0,diffe,count=0,q,acount=0,bcount=0,e; //借位 e=ahead->data*bhead->data;
cout<<"输出运算结果为:"<<endl;
cout<<"商为"; if(e==-1){cout<<"-";}
while(borrow==0)
{
compare(ahead,bhead,q);
if(q==0){borrow=1;cout<<++count<<",余数为0"<<endl;}
else if(q==-1){borrow=1;cout<<count<<",余数为";divput(ahead);}
else
{
count=count+1;
pa=ahead->next;
pb=bhead->next;
while(pa!=ahead&&pb!=bhead)
{
diffe=pa->data-borrow-pb->data;
if(diffe<0){borrow=1;diffe=diffe+10000;}
else borrow=0;
pa->data=diffe;
pa = pa->next;
pb = pb->next;
}
if(pa!=ahead)
{
while(pa!=ahead)
{
diffe=pa->data-borrow;
if (diffe<0){borrow=1;diffe+=10000;}else borrow=0;
pa->data=diffe;
pa=pa->next;
}
}
p=pa->prior;
while(p->data==0&&p->prior!=ahead)
{
s=p;p=p->prior;
ahead->prior=p;
p->next=ahead;
delete s;
}
}
}return 0;
}
int xuan(int &c)
{
cout<<"*************请选择要进行的操作:***********"<<endl;
cout<<"1.加法运算 2.减法运算 3.乘法运算 4.除法运算"<<endl;
cin>>c;
return 0;
}
int main()
{
int c,h;char b;
lnodelist ahead,bhead;
inputa(ahead);
inputb(bhead);
do
{
xuan(c);
switch(c)
{
case 1:add(ahead,bhead);break;
case 2:sub(ahead,bhead);break;
case 3:mul(ahead,bhead);break;
case 4:div(ahead,bhead);break;
}
cout<<"***是否还要进行其他运算操作,是选择Y,否选择N;***"<<endl;
cin>>b;
if(b=='Y')h=1;
else h=0;
}while(h);
return 0;
}