⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 长整数加法.cpp

📁 用C++做的一个长整数加法
💻 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 + -