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

📄 hugeint.cpp

📁 该代码实现了256位的大数运算
💻 CPP
字号:

#include<ctype.h>
#include<string.h>
#include"hugeint.h"


hugeint::hugeint(int value)
{   if(value>=0)
	  label='+';
     else  
	 {label='-';
	 value=-value;
	 }
	for(int i=0;i<N;i++)
	{
		integer[i]=0;
	}
	for(int j=N-1;(value!=0)&&(j>=0);j--)
	{
		integer[j]=value%10;
		value/=10;
	}
}

hugeint::hugeint(const char *string)
{   if(string[0]=='-')
      label='-';
    else
	  label='+';
	for(int i=0;i<N-1;i++)
		integer[i]=0;
	int length=strlen(string);
	for(int j=N-length,k=0;j<=N-1;j++,k++)
	{
		if(isdigit(string[k]))
			integer[j]=string[k]-'0'; 
	}
}

hugeint hugeint::operator =(const int a)
{
	return (*this)=hugeint(a);
}

hugeint  operator +(const hugeint op1, const hugeint op2)
{
	hugeint temp,temp1=op1,temp2=op2;

	if(op1.label!=op2.label)
		if(temp1.label=='+')
		  
		{   temp2.label='+';
			return temp1-temp2;
		}
		else
		{ temp1.label='+';
		  temp2.label='+';
		  temp=temp2-temp1;
		  //temp.label='-';
		  return temp;
		}
	else
		temp.label=op1.label;
	int carry=0;
	for(int i=N-1;i>=0;i--)
	{
		temp.integer[i]=op1.integer[i]+op2.integer[i]+carry;
		if(temp.integer[i]>9)
		{
			temp.integer[i]%=10;
			carry=1;
		}
		else
			carry=0;
	}
	return temp;
}



hugeint hugeint::operator +(int op2)
{
	return *this+hugeint(op2);
}



hugeint operator+(const hugeint op1,const char *op2)
{
	return op1+hugeint(op2);
}

hugeint operator-(const hugeint op1,const hugeint op2)
{
	hugeint temp,temp1,temp2;
    if(op1.label!=op2.label)
       if(op1.label=='+')
	   {     temp1=op2;
			 temp1.label='+';
			 temp=op1+temp1;
	   }
	   else 
	   {temp1=op1;
	         temp2=op2;
			 temp1.label='+';
			 temp2.label='+';
			 temp=temp1+temp2;
		     temp.label='-';
	   }
			  
else 
  if(op1.label=='+')
 {
	int carry=0;
	for(int i=N-1;i>=0;i--)
	{
		temp.integer[i]=op1.integer[i]-op2.integer[i]-carry;
		if(temp.integer[i]<0)
		{
			temp.integer[i]+=10;
			carry=1;
		}
		else
			carry=0;
	}
	if(carry==1) 
	{ temp.label ='-';
	   carry=0;
		 for(int i=N-1;i>=0;i--)
		{
		     temp.integer[i]=op2.integer[i]-op1.integer[i]-carry;
		     if(temp.integer[i]<0)
			 {
			     temp.integer[i]+=10;
			      carry=1;
			 }
		  
              else carry=0;
		 }
	}
  }
else
{
	temp1=op1;
	temp2=op2;
	temp1.label='+';
    temp2.label='+';
     temp=temp2-temp1;

 return temp;

}
   
	return temp;
}

hugeint hugeint::operator -(int op2)
{
	return *this-hugeint(op2);
}



hugeint operator-(const hugeint op1,const char *op2)
{
	return op1-hugeint(op2);
}

hugeint operator*(const hugeint op1,const hugeint op2)
{
	hugeint temp=0,temp1=0,temp2=0;

	if(op1<=op2)
	{
		temp1=op1;//min
	    temp2=op2;//max
	}
	else
	{
		temp1=op2;//min
		temp2=op1;//max
	}

	
	temp1.label='+';

//	cout<<"ok"<<endl;

    for(hugeint i=0;i<temp1-1;i=i+1)
	{	temp=temp+temp2;
	   // cout<<"ok"<<endl;
	}
	if(op1.label!=op2.label)
	   temp.label='-';
	return temp;
}

hugeint hugeint::operator*(int a)
{
	return  (*this)*hugeint(a);

}
	hugeint operator*(const hugeint op1,const char *op2)
	{
		return op1*hugeint(op2);
	}

 
	hugeint operator/(const hugeint op1,const hugeint op2)
	{
       hugeint temp1=op1,temp2=op2,temp=0;
	   hugeint count=0;
	   if(temp1.label==temp2.label)
	   {
		   if(temp1.label=='-')
			   temp1.label=temp2.label='+';		   		   	
			  while(temp2<temp1)
			   { // cout<<" flag1"<<endl;
		        	count=count+1;
			       temp1=temp1-temp2;
			   }
		       return count;
	   }
		else

		{	if(temp1==hugeint(0))
		       return hugeint(0);
			temp1.label='-';
		    temp2.label='+';

		    while(temp1<0)
			{   count=count+1;
				temp1=temp2+temp1;
			}
			count.label='-';
			return count;
		}
	
	}

	hugeint hugeint::operator/(int a)
		{
		    hugeint temp=(*this)/hugeint(a);
			return temp;
		}


	hugeint operator/(const hugeint op1,const char *op2)
	{
		return op1/hugeint(op2);
	}

	hugeint operator%(const hugeint op1,const hugeint op2)
	{
		hugeint temp1=op1,temp2=op2,temp;
	//	while(temp1<0)
	//		temp1=temp1+temp2;
	//	if(temp1.label!=temp2.label)
	//	{temp1.label='-';
	//	temp2.label='+';//cout<<"flag2"<<endl;
	//	 temp=temp1-(temp1/temp2)*temp2; 
	//	}
	//	slse
		  temp=temp1-(temp1/temp2)*temp2;

	//	temp.label='+';
		return temp;
	}

 
        hugeint hugeint::operator%(int a)
		{
			return (*this)%hugeint(a);
		}
	hugeint operator%(const hugeint op1,const char *op2)
	{
		return op1%hugeint(op2);
	}






bool operator==(const hugeint op1,const hugeint op2)
{ 
	int i=N-1;
	
	if(op1.label!=op2.label) return false;
	while(i>=0)
	{
		if(op1.integer[i]!=op2.integer[i])
             return  false;
		i--;
	}
	return true;
}
bool operator==(const hugeint op1,const int op2)
{
	if(op1==hugeint(op2))
		return true;
	else  return false;
}


 bool operator<(const hugeint op1,const hugeint op2)
 {cout<<"flag!!!!!!!!!!!!!!!!"<<endl;
	 hugeint temp,temp1=op1,temp2=op2;
	 int i=0,count1=0,count2=0;
	 
//	 if(temp1==temp2)  return false;
//	 if((temp1-temp2)==0)  return false;
	 cout<<"flag"<<endl;
	 if(op1.label!=op2.label)
		 if(op1.label=='-')
			 return true;
		 else 
			 return false;
	else
	{
	 temp=op1;
     do{
        count1++;
	 }while(temp.integer[i++]==0);
	 temp=op2;
	 i=0;
	 do{
		 count2++;
	 }while(temp.integer[i++]==0);
	 if(temp.label=='+')
	 {//
	   if(count1>count2)		
	   {return true;
	    //cout<<"flag3"<<endl;
	   }
	   else 
		   if(count1<count2)
		   {	  
		      return false;
		   }
	   else  
	   {    
		   i=count1-1;
		  // i=0;
		   do{
			   if(op1.integer[i]<op2.integer[i])
			   {//cout<<"flag3"<<endl;
			   return true;	}
			  else  if(op2.integer[i]<op1.integer[i])
			  {//cout<<"flag4"<<endl;
			  return false;}
		   }while(i++<N-1);
		  // if(i==N) return false;
	   }
        //return false;
	 }
	 else
	 {   i=count1-1;
	 //i=0;
		// cout<<"ok"<<endl;
		 if(count1<count2)
		        return true;
	     else  if(count1>count2)
		        return false;
	     else  
		 {
		   do{
			   if(op1.integer[i]<op2.integer[i])
				   return false;
			  else  if(op2.integer[i]<op1.integer[i])
				   return true;
		   }while(i++<N-1);
          // return false;
		 }
	 }
	}

/*
	 hugeint temp=op1-op2;
	 if(temp.label=='-')
		 return true;
	 else
		 return false;*/

	}

bool operator<(const hugeint op1,const int op2)
{
	return (op1<hugeint(op2));
}

  bool operator<=(const hugeint op1,const hugeint op2)
  {
    /* hugeint temp=op1-op2;
	 if(temp.label=='-')
		 return true;
	 temp=temp-1;
     if(temp.label=='-')
		 return true;
	 else
		 return false;*/
	  if(op1<op2)
		  return true;
	  if(op1==op2)
		  return true;
	  else return false;
 }

 bool operator<=(const hugeint op1,const int op2)
 {
	 return (op1<hugeint(op2));
 }

hugeint sqrt(hugeint op1)
{
	if(op1<hugeint(0))
		cout<<"wrong";
	return hugeint(0);
    hugeint i;
	for( i=0;(i*i)<=op1;i=i+1)
		;
	i=i-1;
	return i;
}


ostream &operator<<(ostream & output,const hugeint & num)
{   
	if(num.label=='-')
		output<<num.label;
	else if(num.label=='+')
		output<<num.label;
	int i=0;
	for(i=0;num.integer[i]==0&&i<=N-1;i++)
		;
	if(i==N)
		output<<0;
	else
		for( ;i<=N-1;i++)
			output<<num.integer[i];
	return output;
}

⌨️ 快捷键说明

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