#include<iostream.h>
#include<stdio.h>
#include<math.h>
struct Dnode{
int data;
Dnode *prv;
Dnode *next;
};
class Bigint
{
public:
Dnode *head,
*p,
*q,
*r;
public:
void input();
void shaw();
void clear();
void insert(int n);
void output(Bigint &b);
Bigint(){
head=new Dnode;
head->prv=NULL;
head->next=NULL;
head->data=NULL;
}
Bigint(Bigint &a);
~Bigint(){
r=this->head;
while(r!=head){
p=r;
delete p;
r=r->next;
}
}
friend Bigint operator +(Bigint& a,Bigint& b);
friend Bigint operator *(Bigint& a,Bigint& b);
Bigint operator = (const Bigint &BigNum);
};
Bigint::Bigint(Bigint &a){ //拷贝构造
this->head=new Dnode;
p=head;
q=a.head;
r=a.head->next;
this->head->next=NULL;
this->head->prv=NULL;
while(r!=a.head){
Dnode *b=new Dnode;
b->next=head;
b->data=r->data;
p->next=b;
b->prv=p;
head->prv=b;
r=r->next;
p=b;
}
}
Bigint Bigint::operator = (const Bigint &a)//重载=
{
this->head->next=this->head->prv=NULL;
if(this==&a)
return *this;
Dnode *m;
m=a.head->prv;
while(m!=a.head)
{
this->insert(m->data);
m=m->prv;
}
return *this;
}
void Bigint::insert(int n){//插入运算,只在头结点后插入
Dnode *x=new Dnode;
x->data=n;
if(head->next==NULL){
head->next=x;
x->next=head;
x->prv=head;
head->prv=x;
}
else{
r=head->next;
x->next=r;
x->prv=head;
r->prv=x;
head->next=x;
}
}
void Bigint::input(){//字符串输入
char c;
int i=0,sum=0;
p=head;
fflush(stdin); //清空缓存
while( (c=getchar())!=10 ){
Dnode *m=new Dnode;
m->data=int(c-48);//转换整数0~9
p->next=m;//插入到结点中保留,一个结点只存一位
head->prv=m;
m->prv=p;
m->next=head;
p=m;
}
}
void Bigint::shaw(){//显示
p=head;
while(p->next!=head){
p=p->next;
cout<<p->data;
}
cout<<endl;
}
Bigint operator +(Bigint& a,Bigint& b){//重载+
int sum=0,y=0;//sum是本位和,y是进位
Bigint temp;
Dnode *m=new Dnode;
Dnode *n=new Dnode;
m=a.head->prv;
n=b.head->prv;
while(m!=a.head&&n!=b.head)//当加数、被加数都不为空时
{
sum=n->data+m->data+y;
y=sum/10;
sum=sum%10;
temp.insert(sum);
m=m->prv;
n=n->prv;
}
while(n!=b.head)//加数不为空时
{
sum=n->data+y;
y=sum/10;
sum=sum%10;
temp.insert(sum);
n=n->prv;
}
while(m!=a.head)//被加数不为空时
{
sum=m->data+y;
y=sum/10;
sum=sum%10;
temp.insert(sum);
m=m->prv;
}
if(y==1)//判断最后一次是否加进位
temp.insert(y);
return temp;
}
void Bigint::clear(){//头节点置空
this->head->prv=NULL;
this->head->next=NULL;
}
Bigint operator *(Bigint& a,Bigint& b){//重载乘
int sum=0,y=0,q=0,t=0;//sum为本位积,y为进位、q为记数,乘了几次
Bigint temp,c;
Dnode *m=new Dnode;
Dnode *n=new Dnode;
Dnode *w=new Dnode;
m=a.head->prv;
n=b.head->prv;
c.insert(0);//先初始化
while(n!=b.head){//当乘数不为空,循环乘被乘数
if(q==0)//如果是第一次乘时
{
while(m!=a.head)//当被乘数不为空
{
sum=n->data*m->data+y;
y=sum/10;
sum=sum%10;
temp.insert(sum);
m=m->prv;
}
if(y!=0){//判断最后一次是否有进位
temp.insert(y);
y=0;
}
}
else{//不是第一次乘
t=q;
while(t-1!=0)//初始化c,进行对应的以为,即在末尾插入0
{
t–;
c.insert(0);
}
while(m!=a.head)//当乘数不为0时
{
sum=n->data*m->data+y;
y=sum/10;
sum=sum%10;
c.insert(sum);
m=m->prv;
}
if(y!=0){
c.insert(y);
y=0;
}
temp=temp+c;//将本次乘法结果与上次相加,结果存在temp中。
c.clear();//清空c
c.insert(0);//再初始化c
}
q++;//记录乘几次
n=n->prv;//乘数后移
m=m->prv;//被乘数复位
}
return temp;
}
void main(){
Bigint a,b,d;
int q;
cout<<“请输入要选择的操做,0为退出,1为加法,2为乘法:”<<endl;
cin>>q;
while(q!=0)
{switch (q){
case 1:
{
printf(“请输入a:”);
a.input();
printf(“请输入b:”);
b.input();
cout<<“a=”;
a.shaw();
cout<<“b=”;
b.shaw();
cout<<“a+b=”;
d=a+b;
d.shaw();
cout<<“计算成功,请重新选择操作”<<endl;
cin>>q;
}break;
case 2:
{
printf(“请输入a:”);
a.input();
printf(“请输入b:”);
b.input();
cout<<“a=”;
a.shaw();
cout<<“b=”;
b.shaw();
cout<<“a*b=”;
d=a*b;
d.shaw();
cout<<“计算成功,请重新选择操作”<<endl;
cin>>q;
} break;
case 0:
break;
default:{
cout<<“输入错误,请重新选择操作:”<<endl;
cin>>q;
}
}
}
}