?? 長整數加法.cpp
字號:
#include<iostream.h>
class listnode//定義結點類
{
public:
listnode *right;
listnode *left;
int data;
listnode(int &inteval,listnode *a=NULL,listnode *b=NULL)
{
data=inteval;right=a;left=b;
}
};
class linklist//定義鏈表類(雙向鏈表)
{
public:
listnode *head;
listnode *tail;
listnode *currptr;
linklist(){head=tail=currptr=NULL;}
void insert(int &temp);//尾插入
void print();//輸出
void insert2(int &temp2);//頭插入
};
void linklist::insert2(int &temp2)//定義頭插入函數
{
listnode *tempnode;
tempnode=new listnode(temp2,NULL,NULL);
if(head==NULL)
{
head=tail=tempnode;
}
else
{
tempnode->right=head;
head->left=tempnode;
head=tempnode;
}
}
void linklist::insert(int &temp)//定義尾插入函數
{
listnode *tempnode;
tempnode=new listnode(temp,NULL,NULL);
if(head==NULL)
{
head=tail=tempnode;
}
else
{
tempnode->left=tail;
tail->right=tempnode;
tail=tempnode;
}
}
void linklist::print()//定義輸出函數
{
currptr=head;
while(currptr->right!=NULL)
{
cout<<currptr->data;
currptr=currptr->right;
}
cout<<currptr->data;
}
void main()
{
bool aflag=1;///////標志第一個數的符號
bool bflag=1;/////////標志第二個數的符號
bool flag;////////標志絕對值大小:1,第一個數大;0,第二個數大
linklist first;
int i=0,j=0,k,sum=0,sum2=0;/////////i,j用來標志兩個數的長度
char a[10000];
cout<<"請輸入第一個長整數:"<<endl;
cin>>a;
if(a[0]=='-')
{
aflag=0;
i++;
}
while(a[i]!='\0')
{
k=a[i]-48;/////////將字符轉換為整數
first.insert(k);
i++;
}
if(a[0]=='-')
i--;
k=0;
first.insert2(k);
linklist second;
j=0;
char b[10000];
cout<<"請輸入第二個長整數:"<<endl;
cin>>b;
if(b[0]=='-')
{
bflag=0;
j++;
}
while(b[j]!='\0')
{
k=b[j]-48;
second.insert(k);
j++;
}
if(b[0]=='-')
j--;
k=0;
second.insert2(k);/////////輸入結束
linklist third;
if(aflag+bflag==1)//////////減法開始
{
first.currptr=first.head;
second.currptr=second.head;
if(i>j)//////第一個大
flag=1;
else if(i<j)//////第二個大
flag=0;
else//判斷大小
{
while(first.currptr!=NULL&&second.currptr!=NULL)
{
if(first.currptr->data>second.currptr->data)//////第一個數大
{
flag=1;
break;
}
else if(first.currptr->data<second.currptr->data)/////第二個數大
{
flag=0;
break;
}
else
{
first.currptr=first.currptr->right;
second.currptr=second.currptr->right;
}
}
}
if(flag==1)////////第一個減第二個
{
while(first.head!=NULL&&second.head!=NULL)
{
sum=sum2=first.tail->data-second.tail->data;
if(sum<0)
sum+=10;
third.insert2(sum);
if(first.head==first.tail)
first.head=NULL;
else
{
first.tail=first.tail->left;
if(sum2<0)
first.tail->data--;
}
if(second.head==second.tail)
second.head=NULL;
else
second.tail=second.tail->left;
}
while(first.head!=NULL)
{
sum=first.tail->data;
if(first.tail->data<0)
{
first.tail->data+=10;
}
third.insert2(first.tail->data);
if(first.head==first.tail)
first.head=NULL;
else
{
first.tail=first.tail->left;
if(sum<0)
{
first.tail->data--;
}
}
}
}
else///////////第二個減第一個
{
while(first.head!=NULL&&second.head!=NULL)
{
sum=sum2=second.tail->data-first.tail->data;
if(sum<0)
sum+=10;
third.insert2(sum);
if(second.head==second.tail)
second.head=NULL;
else
{
second.tail=second.tail->left;
if(sum2<0)
second.tail->data--;
}
if(first.head==first.tail)
first.head=NULL;
else
first.tail=first.tail->left;
}
while(second.head!=NULL)
{
sum=second.tail->data;
if(second.tail->data<0)
{
second.tail->data+=10;
}
third.insert2(second.tail->data);
if(second.head==second.tail)
second.head=NULL;
else
{
second.tail=second.tail->left;
if(sum<0)
second.tail->data--;
}
}
}
}
else////////////////////兩個都為正或都為負時
{
while(first.head!=NULL&&second.head!=NULL)/////////兩個都不為空
{
sum2=sum=first.tail->data+second.tail->data;
if(sum>=10)
sum=sum-10;
third.insert2(sum);
if(first.head==first.tail)
first.head=NULL;
else
{
first.tail=first.tail->left;
if(sum2>=10)
first.tail->data+=1;
}
if(second.head==second.tail)
second.head=NULL;
else
second.tail=second.tail->left;
}
while(second.head!=NULL)///////第二個不為空
{
if(sum2>=10)
second.tail->data+=1;
if(second.tail->data>=10)
{
second.tail->data-=10;
second.tail->left->data+=1;
}
third.insert2(second.tail->data);
if(second.tail==second.head)
second.head=NULL;
else
second.tail=second.tail->left;
sum2=0;
}
while(first.head!=NULL)///////第一個不為空
{
if(first.tail->data>=10)
{
first.tail->data-=10;
first.tail->left->data+=1;
}
third.insert2(first.tail->data);
if(first.tail==first.head)
first.head=NULL;
else
first.tail=first.tail->left;
}
}
while(third.head->data==0&&third.head!=third.tail)//////消零
{
third.head=third.head->right;
}
cout<<"運算結果是:"<<endl;
if(aflag==0&&bflag==0)//////兩個都為負
cout<<"-";
if(aflag==0&&flag==1)///////第一個為負且絕對值大
cout<<"-";
if(bflag==0&&flag==0)///////第二個為負且絕對值大
cout<<"-";
third.print();
cout<<endl;
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -