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

📄 long_number.cpp

📁 长整数的加减乘 重载了运算符
💻 CPP
📖 第 1 页 / 共 2 页
字号:
		if(decimal_long<=temp.decimal_long)
		{
			for(i=0;i<(temp.decimal_long-decimal_long);i++)
			{
				p=new number_node;
				if(!decimal_head)
				{
					decimal_head=decimal_tail=p;
					decimal_head->high_link=decimal_head->low_link=NULL;
					p->node=0;
				}
				else
				{
					p->node=0;
					p->low_link=NULL;
					decimal_tail->low_link=p;
					p->high_link=decimal_tail;
					decimal_tail=p;
				}
			}
			p=decimal_tail;
			decimal_long=temp.decimal_long;
		}
		else
		{
			p=decimal_tail;
			for(i=0;i<(decimal_long-temp.decimal_long);i++,p=p->high_link);
		}
		q=temp.decimal_tail;
		while(p!=NULL)
		{
			tn=p->node+(q->node*temp.sign);
			if(tag)
			{
				tn+=temp.sign;
				tag=0;
			}
			p->node=tn%10;
			if(p->node<0)
				p->node=p->node+10;
			if(p->node!=tn)
				tag=1;
			p=p->high_link;
			q=q->high_link;
		};
	}
	p=integer_tail;
	q=temp.integer_tail;
	while(q!=NULL&&p!=NULL)
	{
		tn=p->node+(q->node*temp.sign);
		if(tag)
		{
			tn+=temp.sign;
			tag=0;
		}
		p->node=tn%10;
		
		if(p->node<0)
			p->node=p->node+10;
		if(p->node!=tn)
			tag=1;
		p=p->high_link;
		q=q->high_link;
	};
	if(!q&&p)
	{
		while(tag)
		{
			if(tag)
			{
				p->node+=temp.sign;
				tn=p->node;
				tag=0;
			}
			p->node=tn%10;
			if(p->node<0)
				p->node=p->node+10;
			if(p->node!=tn)
			{
				tag=1;
				p=p->high_link;
			}
		}
	}
	else if(q==p&&tag)
	{
		q=integer_head;
		integer_head=new number_node;
		integer_head->node=1;
		integer_head->low_link=q;
		q->high_link=integer_head;
		integer_head->high_link=NULL;
		integer_long++;
	}
	else
	{
		while(q)
		{
			p=integer_head;
			integer_head=new number_node;
			if(tag)
				integer_head->node=q->node+1;
			else
				integer_head->node=q->node;
			integer_head->low_link=p;
			integer_head->high_link=NULL;
			q=q->high_link;
		};
	}
	return true;
}
//运算符的重载
long_number long_number::operator +(long_number &temp)
{
	long_number outdata;
	if(sign==temp.sign)
	{
		outdata=*this;
		if(outdata.sign==-1)
		{
			outdata.sign=temp.sign=1;
			outdata.basic_operation(temp);
			outdata.sign=temp.sign=-1;
		}
		else
			outdata.basic_operation(temp);
	}
	else if(sign==-1)
	{
		if(basic_compare(temp)!=1)
		{
			outdata=temp;
			outdata.basic_operation(*this);
		}
		else
		{
			outdata=*this;
			outdata.sign=1;
			temp.sign=-1;
			outdata.basic_operation(temp);
			outdata.sign=-1;
			temp.sign=1;
		}
	}
	else
	{
		if(basic_compare(temp)!=-1)
		{
			outdata=*this;
			outdata.basic_operation(temp);
		}
		else
		{
			outdata=temp;
			outdata.sign=1;
			sign=-1;
			outdata.basic_operation(*this);
			outdata.sign=-1;
			sign=1;
		}
	}
	outdata.number_std();
	return outdata;
}

long_number long_number::operator -(long_number &temp)
{
	long_number outdata;
	if(sign!=temp.sign)
	{
		outdata=*this;
		if(sign==-1)
		{
			outdata.sign=1;
			outdata.basic_operation(temp);
			outdata.sign=-1;
		}
		else
		{
			temp.sign=1;
			outdata.basic_operation(temp);
			temp.sign=-1;
		}
	}
	else
	{
		if(sign==-1)
		{
			if(basic_compare(temp)!=1)
			{
				outdata=temp;
				outdata.sign=1;
				outdata.basic_operation(*this);
			}
			else
			{
				outdata=*this;
				outdata.sign=1;
				outdata.basic_operation(temp);
				outdata.sign=-1;
			}
		}
		else
		{
			if(basic_compare(temp)!=-1)
			{
				outdata=*this;
				temp.sign=-1;
				outdata.basic_operation(temp);
				temp.sign=1;
			}
			else
			{
				outdata=temp;
				sign=-1;
				outdata.basic_operation(*this);
				sign=1;
				outdata.sign=-1;
			}
		}
	}
	outdata.number_std();
	return outdata;
}
/*************************老版乘法运算函数
long_number long_number::operator*(long_number &temp)
{
	long_number product;
	number_node *p,*q,*protail,*protemp,*pp,*qq;
	int tn,tag=0,i,tagg=0;

	product.integer_head=product.integer_tail=new number_node;
	product.integer_head->node=0;
	product.integer_head->high_link=product.integer_head->low_link=NULL;
	product.integer_long=1;
	
	
	integer_tail->low_link=decimal_head;
	if(decimal_head)
	{
		decimal_head->high_link=integer_tail;
		pp=decimal_tail;
	}
	else
		pp=integer_tail;
	temp.integer_tail->low_link=temp.decimal_head;
	if(temp.decimal_head)
	{
		temp.decimal_head->high_link=temp.integer_tail;
		qq=temp.decimal_tail;
	}
	else
		qq=temp.integer_tail;

	protail=product.integer_tail;
	for(q=qq;q!=NULL;q=q->high_link)
	{
		for(p=pp,protemp=protail,tag=0;p!=NULL;p=p->high_link)
		{
			tn=p->node*q->node;
			if(tag)
				tn+=tag;
			tag=tn/10;
			tn=tn%10;
			protemp->node+=tn;
			if(protemp->node>9)
			{
				protemp->node-=10;
				tag++;
			}
			if(!protemp->high_link)
			{
				product.integer_head=new number_node;
				product.integer_head->high_link=NULL;
				product.integer_head->low_link=protemp;
				protemp->high_link=product.integer_head;
				product.integer_head->node=0;
				product.integer_long++;
			}
			protemp=protemp->high_link;
		}
		if(tag)
			protemp->node=tag;
		protail=protail->high_link;
	}
	product.decimal_long=decimal_long+temp.decimal_long;
	product.integer_long-=product.decimal_long;
	if(product.decimal_long)
	{
		for(i=0,p=product.integer_tail;i<product.decimal_long;i++,p=p->high_link);
		q=p->low_link;
		q->high_link=p->low_link=NULL;
		product.decimal_tail=product.integer_tail;
		product.integer_tail=p;
		product.decimal_head=q;
	}
	if(decimal_head)
		integer_tail->low_link=decimal_head->high_link=NULL;
	if(temp.decimal_head)
		temp.integer_tail->low_link=temp.decimal_head->high_link=NULL;
	product.sign=sign*temp.sign;
	product.number_std();
	return product;
}

*/
int optemp(number_node *p,number_node *q)
{
	int out=0;
	while(p!=NULL&&q!=NULL)
	{
		out+=p->node*q->node;
		p=p->high_link;
		q=q->low_link;
	}
	return out;
}

long_number long_number::operator*(long_number &temp)
{
	long_number product;
	number_node *p,*q,*protail;
	int tag=0,i,jiwei;

	product.integer_head=product.integer_tail=new number_node;
	product.integer_head->node=0;
	product.integer_head->high_link=product.integer_head->low_link=NULL;
	product.integer_long=0;
	
	
	integer_tail->low_link=decimal_head;
	if(decimal_head)
	{
		decimal_head->high_link=integer_tail;
		integer_tail->low_link=decimal_head;
		p=decimal_tail;
	}
	else
		p=integer_tail;
	temp.integer_tail->low_link=temp.decimal_head;
	if(temp.decimal_head)
	{
		temp.decimal_head->high_link=temp.integer_tail;
		temp.integer_tail->low_link=temp.decimal_head;
		q=temp.decimal_tail;
	}
	else
		q=temp.integer_tail;

	i=0;
	protail=product.integer_tail;
	while(1)
	{
		protail->node=optemp(p,q)+tag;
		product.integer_long++;
		tag=0;
		jiwei=protail->node/10;
		if(jiwei!=0)
		{
			tag=jiwei;
			protail->node=protail->node%10;
		}

		if(q->high_link!=NULL)
			q=q->high_link;
		else if(p->high_link!=NULL)
			p=p->high_link;
		else
		{
			if(tag)
			{
				while(tag)
				{
					protail->high_link=new number_node;
					protail->high_link->low_link=protail;
					protail=protail->high_link;
					protail->node=tag%10;
					tag=tag/10;
					product.integer_head=protail;
				}
			}
			break;
		}
		protail->high_link=new number_node;
		protail->high_link->low_link=protail;
		protail=protail->high_link;
		product.integer_head=protail;
		product.integer_head->high_link=NULL;
	}

	product.decimal_long=decimal_long+temp.decimal_long;
	product.integer_long-=product.decimal_long;
	if(product.decimal_long)
	{
		for(i=0,p=product.integer_tail;i<product.decimal_long;i++,p=p->high_link);
		q=p->low_link;
		q->high_link=p->low_link=NULL;
		product.decimal_tail=product.integer_tail;
		product.integer_tail=p;
		product.decimal_head=q;
	}
	if(decimal_head)
		integer_tail->low_link=decimal_head->high_link=NULL;
	if(temp.decimal_head)
		temp.integer_tail->low_link=temp.decimal_head->high_link=NULL;
	product.sign=sign*temp.sign;
	product.number_std();
	return product;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -