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

📄 intcount.h

📁 大整数计算
💻 H
字号:
#include<fstream.h>
#include<string.h>
class count
{
public:
	int p[30];
	int sign;
	count( char * string);
	count count::operator +( count &B);
	count count::operator -( count &B);
	int count::compare(count &A,count &B);
	count count::operator *(count &B);
private:
	int length;
};
count::count(char * string)
{
	sign=1;
	for(int i=0;i<30;i++)
		p[i]=0;
	length=strlen(string);
	for(int j=30-length,k=0;j<30;j++,k++)
			p[j]=string[k]-'0';
	if(string[0]=='-'){
		p[30-length]=0;
		sign=-1;}
}
count count::operator +(count &B)
{
	int c=0;
	count A("0");
	if(this->sign==1)
	{
		if(B.sign==1)
		{
			for(int i=29;i>=0;i--)
			{
         		A.p[i]=p[i]+B.p[i]+c;
        		if(A.p[i]>9)
				{
					c=1;
            		A.p[i]=A.p[i]%10;
				}
				else
					c=0;
			}
		}
		else
		{
			B.sign=1;
			if(count::compare(*this,B)==1)
			{
        		for(int i=29;i>=0;i--)
				{
              		 A.p[i]=p[i]-B.p[i]+c;
					 if(A.p[i]<0)
					 {
						 c=-1;
						 A.p[i]=A.p[i]+10;
					 }
					 else
						 c=0;
				}
	        	A.sign=1;
			}
			else
			{
				for(int i=29;i>=0;i--)
				{
					A.p[i]=-p[i]+B.p[i]+c; 
					if(A.p[i]<0)
					{
						c=-1;
						A.p[i]=A.p[i]+10;
					}
					else
						c=0;
				}
				A.sign=-1;
			}
		}
	}
	if(this->sign==-1)
	{
		if(B.sign==1)
			A=B+*this;
		else
		{
			this->sign=1;
			B.sign=1;
			A=*this+B;
			A.sign=-1;
			this->sign=-1;
			B.sign=-1;
		}
	}
	return A;
};
count count::operator -(count &B)
{
	int c=0;
	count A("0");
	B.sign=B.sign*(-1);
	A=*this+B;
	B.sign=B.sign*(-1);
	return A;
};
count count::operator *(count &B)
{
	count A("0"),E("0");
	if(this->length+B.length>30)
	{
		cout<<"error";
		return E;
	}
	else
	{
    	int i=0,j=0,c=0,d=0;
    	for(i=29;i>=30-this->length;i--)
    		for(j=29;j>=30-B.length;j--)
    			A.p[i+j-29]+=this->p[i]*B.p[j];
    	for(i=29;i>=0;i--)
		{
			A.p[i]+=c;
			c=A.p[i]/10;
			A.p[i]=A.p[i]%10;
		}
		A.sign=this->sign*B.sign;
		if(c>0)
		{
			cout<<"error";
			return E;
		}
		else
			return A;
	}
}
int count::compare(count &A,count &B)
{
	if(A.sign>B.sign) return 1;
	else
		if(A.sign>B.sign) return 0;
		else
		{
			int k=0;
			while(A.p[k]-B.p[k]==0)
				k++;
			if(A.sign==1)
			{
				if(A.p[k]-B.p[k]>0)
					return 1;
				else return 0;
			}
			else
			{
				if(A.p[k]-B.p[k]>0)
					return 0;
				else return 1;
			}
		}
}

⌨️ 快捷键说明

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