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

📄 clongint.cpp

📁 一个用来对非常大的数进行科学计算的程序库
💻 CPP
📖 第 1 页 / 共 2 页
字号:
		csResult += csZero;
		csZero.Empty();
		
		CLongInt liResult(csResult);
		liEnd += liResult;
		csResult.Empty();
	}
			
	delete []piUp;
	
	CString csZero('0', iEndZero);
	liEnd.m_csInt += csZero;
	//  sec  3  2  1
	//  idx 01 23 45
	//  cs1 12|34|56
	//  cs2 00|01|23
	
	liEnd.m_bSign = this -> m_bSign * li.m_bSign;
	
	return liEnd;
}

//////////////////////////////////////////////////////////////
//Friend member
CLongInt operator * (long n, const CLongInt & li)
{
	CLongInt li1 = n;
	return li1 * li;
}

//////////////////////////////////////////////////////////////
// Friend member function
// Description: the sign of dr.liModulus is same to liBei
// -50 % 6 = -2     50 % -6 = 2

DIVIDERESULT Div (const CLongInt & liBei1, const CLongInt & liChu1)
{
	DIVIDERESULT dr;
	
	if (liChu1.m_bSign == 0)
	{
		throw new CLongIntDivException;
		return dr;
	}

	if (liBei1.m_bSign == 0)
		return dr;

	CLongInt liBei = liBei1, liChu = liChu1;
	if (liBei.m_bSign == -1)
		liBei.m_bSign = 1;
	if (liChu.m_bSign == -1)
		liChu.m_bSign = 1;

	CLongInt liQuotient, liModulus;
	int iBeiLen = liBei.m_csInt.GetLength();
	int iChuLen = liChu.m_csInt.GetLength();
	int iQuotientLen = iBeiLen - iChuLen + 1;

	if (iQuotientLen <= 0)
	{
		dr.liQuotient = 0;
		dr.liModulus = liBei1;
		return dr;
	}

	CString csQuotient;
	LPTSTR lpszQuotient = csQuotient.GetBuffer (iQuotientLen);
	CLongInt liSecBei, liSecModulus;
	liSecBei.m_bSign = 1;
	liSecModulus.m_bSign = 1;
	
	for (int i = 0; i <= iQuotientLen - 1; i++)
	{
		if (i == 0)
			liSecBei.m_csInt = liBei.m_csInt.Left(iChuLen);
		else
			liSecBei.m_csInt = liSecModulus.m_csInt + liBei.m_csInt.Mid(i + iChuLen - 1, 1);

		liSecBei.m_csInt.TrimLeft('0');
		
		if (liSecBei.m_csInt.IsEmpty())
			liSecBei = 0;
		else
			liSecBei.m_bSign = 1;

		int iMaxSingleQuotient;
		if (liSecBei.m_csInt.GetLength() <= iChuLen)
			iMaxSingleQuotient = int(liSecBei.GetLeft(3) / liChu.GetLeft(3));
		else
			iMaxSingleQuotient = int(liSecBei.GetLeft(4) / liChu.GetLeft(3));

		for (int j = iMaxSingleQuotient;  j >= 0; j--)
		{
			CLongInt li = liChu * j;
			if (liSecBei >= li) break;
		}

		// iSingleTestQuotient = j;
		lpszQuotient[i] = j + '0';
		
		liSecModulus = liSecBei - liChu * j;
				
		if (i == iQuotientLen - 1)
			dr.liModulus = liSecModulus;
	}

	csQuotient.ReleaseBuffer();
	csQuotient.TrimLeft('0');
	
	if (csQuotient.IsEmpty())
		dr.liQuotient = 0;
	else	
		dr.liQuotient.m_csInt = csQuotient;

	// Set Sign
	dr.liQuotient.m_bSign = liBei1.m_bSign * liChu1.m_bSign;
	dr.liModulus.m_bSign = liBei1.m_bSign;
	if (dr.liQuotient.m_csInt == '0') 
		dr.liQuotient.m_bSign = 0;
	if (dr.liModulus.m_csInt == '0')
		dr.liModulus.m_bSign = 0;

	return dr;
}

//////////////////////////////////////////////////////////////
CLongInt CLongInt::operator += (long n)
{
	*this = *this + n;
	return *this;
}

//////////////////////////////////////////////////////////////
CLongInt CLongInt::operator += (const CLongInt & li)
{
	*this = *this + li;
	return *this;
}

//////////////////////////////////////////////////////////////
CLongInt CLongInt::operator -= (long n)
{
	*this = *this - n;
	return *this;
}

//////////////////////////////////////////////////////////////
CLongInt CLongInt::operator -= (const CLongInt & li)
{
	*this = *this - li;
	return *this;
}

//////////////////////////////////////////////////////////////
CLongInt CLongInt::operator *= (long n)
{
	*this = *this * n;
	return *this;
}

//////////////////////////////////////////////////////////////
CLongInt CLongInt::operator *= (const CLongInt & li)
{
	*this = *this * li;
	return *this;
}

//////////////////////////////////////////////////////////////
BOOL CLongInt::operator >  (long n)         const
{
	CLongInt li = n;
	return *this > li;
}

//////////////////////////////////////////////////////////////
BOOL CLongInt::operator <  (long n)         const
{
	CLongInt li = n;
	return *this < li;
}

//////////////////////////////////////////////////////////////
BOOL CLongInt::operator >= (long n)         const
{
	CLongInt li = n;
	return *this >= li;
}

//////////////////////////////////////////////////////////////
BOOL CLongInt::operator <= (long n)         const
{
	CLongInt li = n;
	return *this <= li;
}

//////////////////////////////////////////////////////////////
BOOL CLongInt::operator == (long n)         const
{
	CLongInt li = n;
	return *this == li;
}

//////////////////////////////////////////////////////////////
BOOL CLongInt::operator != (long n)         const
{
	CLongInt li = n;
	return *this != li;
}

//////////////////////////////////////////////////////////////
BOOL CLongInt::operator >  (const CLongInt & li)    const
{
	if (this -> m_bSign < li.m_bSign)    // -+ -0 0+
		return FALSE;
	else if (this -> m_bSign > li.m_bSign)  // +- 0- +0
		return TRUE;
	else if (this -> m_bSign == 0 && li.m_bSign == 0) // 00
		return FALSE;
	
	int nSetZero = (this -> m_csInt).GetLength() - li.m_csInt.GetLength();
	
	if (this -> m_bSign == -1 && li.m_bSign == -1) // --
    {
		if (nSetZero > 0)   // -10  -1
			return FALSE;
		else if (nSetZero < 0)  // -1 -10
			return TRUE;
		else    // -123  -234
			return (this -> m_csInt < li.m_csInt) ? TRUE : FALSE;
    }
	else  // ++
    {
		if (nSetZero > 0)   // 10  1
			return TRUE;
		else if (nSetZero < 0)  // 1 10
			return FALSE;
		else
			// 234  123
			return this -> m_csInt > li.m_csInt ? TRUE : FALSE;
    }
	
	return FALSE;
}

//////////////////////////////////////////////////////////////
BOOL CLongInt::operator <  (const CLongInt & li)    const
{
	if (this -> m_bSign < li.m_bSign)    // -+ -0 0+
		return TRUE;
	else if (this -> m_bSign > li.m_bSign)  // +- 0- +0
		return FALSE;
	else if (this -> m_bSign == 0 && li.m_bSign == 0) // 00
		return FALSE;
	
	int nSetZero = (this -> m_csInt).GetLength() - li.m_csInt.GetLength();
	
	if (this -> m_bSign == -1 && li.m_bSign == -1) // --
    {
		if (nSetZero > 0)   // -10  -1
			return TRUE;
		else if (nSetZero < 0)  // -1 -10
			return FALSE;
		else
			// -234 -123
			return this -> m_csInt > li.m_csInt ? TRUE : FALSE;
    }
	else  // ++
    {
		if (nSetZero > 0)   // 10  1
			return FALSE;
		else if (nSetZero < 0)  // 1 10
			return TRUE;
		else
			// 123  234
			return this -> m_csInt < li.m_csInt ? TRUE : FALSE;
    }
	
	return FALSE;
}

//////////////////////////////////////////////////////////////
BOOL CLongInt::operator >= (const CLongInt & li)    const
{
	if (this -> m_bSign < li.m_bSign)    // -+ -0 0+
		return FALSE;
	else if (this -> m_bSign > li.m_bSign)  // +- 0- +0
		return TRUE;
	else if (this -> m_bSign == 0 && li.m_bSign == 0) // 00
		return TRUE;
	
	int nSetZero = (this -> m_csInt).GetLength() - li.m_csInt.GetLength();
	
	if (this -> m_bSign == -1 && li.m_bSign == -1) // --
    {
		if (nSetZero > 0)   // -10  -1
			return FALSE;
		else if (nSetZero < 0)  // -1 -10
			return TRUE;
		else
			// -123 -234
			return this -> m_csInt <= li.m_csInt ? TRUE : FALSE;
    }
	else  // ++
    {
		if (nSetZero > 0)   // 10  1
			return TRUE;
		else if (nSetZero < 0)  // 1 10
			return FALSE;
		else
			// 234 123
			return this -> m_csInt >= li.m_csInt ? TRUE : FALSE;
    }
	
	return FALSE;
}

//////////////////////////////////////////////////////////////
BOOL CLongInt::operator <= (const CLongInt & li)    const
{
	if (this -> m_bSign < li.m_bSign)    // -+ -0 0+
		return TRUE;
	else if (this -> m_bSign > li.m_bSign)  // +- 0- +0
		return FALSE;
	else if (this -> m_bSign == 0 && li.m_bSign == 0) // 00
		return TRUE;
	
	int nSetZero = (this -> m_csInt).GetLength() - li.m_csInt.GetLength();
	
	if (this -> m_bSign == -1 && li.m_bSign == -1) // --
    {
		if (nSetZero > 0)   // -10  -1
			return TRUE;
		else if (nSetZero < 0)  // -1 -10
			return FALSE;
		else
			// -234 -123
			return this -> m_csInt >= li.m_csInt ? TRUE : FALSE;
    }
	else  // ++
    {
		if (nSetZero > 0)   // 10  1
			return FALSE;
		else if (nSetZero < 0)  // 1 10
			return TRUE;
		else
			// 123  234
			return this -> m_csInt <= li.m_csInt ? TRUE : FALSE;
    }
	
	return FALSE;
}

//////////////////////////////////////////////////////////////
BOOL CLongInt::operator == (const CLongInt & li)    const
{
	if (this -> m_bSign == li.m_bSign
		&& this -> m_csInt == li.m_csInt)
		return TRUE;
	
	return FALSE;
}

//////////////////////////////////////////////////////////////
BOOL CLongInt::operator != (const CLongInt & li)    const
{
	if (this -> m_bSign != li.m_bSign
		|| this -> m_csInt != li.m_csInt)
		return TRUE;
	
	return FALSE;
}

//////////////////////////////////////////////////////////////
BOOL CLongInt::IsNum(const char * lpszNumStr) const
{
	CString cs = lpszNumStr;
	
	cs.TrimLeft();
	cs.TrimRight();
	
	int nLen = cs.GetLength();
	
	if ((cs.SpanIncluding(CLongInt::sCharSet)).GetLength() != nLen)
		return FALSE;
	
	BOOL bIsZheng = 0, bIsFu = 0;
	
	if (cs.Find('+') != -1) bIsZheng = 1;
	if (cs.Find('-') != -1) bIsFu = 1;
	
	if (bIsZheng && bIsFu)
		return FALSE;
	
	else if (bIsZheng == 1 && bIsFu == 0)
    {
		cs = cs.Right(nLen - 1);
		if (cs.Find('+') != -1)
			return FALSE;
    }
	
	else if (bIsZheng == 0 && bIsFu == 1)
    {
		cs = cs.Right(nLen - 1);
		if (cs.Find('-') != -1)
			return FALSE;
    }
	
	return TRUE;
}

//////////////////////////////////////////////////////////////
CString CLongInt::ToRegularNumStr(const char * lpszNumStr) const
{
	if (!IsNum(lpszNumStr))
		throw new CLongIntIsNotNumberException;
	
	CString cs = lpszNumStr, csEnd;
	
	cs.TrimLeft();
	cs.TrimRight();
	
	int nLen = cs.GetLength();
	
	BOOL bIsZheng = 0, bIsFu = 0;
	
	if (cs.Find('+') != -1) bIsZheng = 1;
	if (cs.Find('-') != -1) bIsFu = 1;
	
	if (bIsZheng == 1 && bIsFu == 0)
    {
		cs = cs.Right(nLen - 1);
		if (cs.SpanIncluding("0") == cs)
			csEnd = '0';
		else
		{
			cs.TrimLeft('0');
			csEnd = cs;
		}
    }
	
	else if (bIsZheng == 0 && bIsFu == 1)
    {
		cs = cs.Right(nLen - 1);
		if (cs.SpanIncluding("0") == cs)
			csEnd = '0';
		else
		{
			cs.TrimLeft('0');
			csEnd = '-' + cs;
		}
    }
	else
    {
		if (cs.SpanIncluding("0") == cs)
			csEnd = '0';
		else
		{
			cs.TrimLeft('0');
			csEnd = cs;
		}
    }
	return csEnd;
}

//////////////////////////////////////////////////////////////
CString CLongInt::GetNumString() const
{
	if (m_bSign != -1)
		return m_csInt;
	else 
		return '-' + m_csInt;
}

//////////////////////////////////////////////////////////////
// Private function, serve for Div().
// Description: return the left X num.
// Example: GetFourLeft(~123456~) -> 1234
inline int CLongInt::GetLeft(int x) const
{
	CString cs = this->m_csInt.Left(x);
	return atoi(cs);
}

⌨️ 快捷键说明

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