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

📄 usuperint.cpp

📁 可以进行无限制的整数计算
💻 CPP
📖 第 1 页 / 共 2 页
字号:
		
		xx[nXX] = new CUSuperInt(x);
		xx[nXX]->Double(nXX);
		
		__asm
		{
to_cmp:
		//比较
			mov eax, type xx;
			mul nXX;
			mov ebx, xx[eax];						
			mov esi, [ebx]xx.pValue;
			mov ecx, [ebx]xx.last;
			mov eax, ecx;
			dec eax;
			shl eax, 2;
			add esi, eax;

			mov edx, this;
			mov edi, [edx]this.pValue;
			mov eax, [edx]this.last;
			dec eax;
			shl eax, 2;
			add edi, eax;

			
			repe cmpsd;
			jbe to_sub;

			//减数变为前一个
			//如果减数指针为空,则新分配
			add nXX, 31;
			and nXX, 0x1f;
			mov eax, type xx;
			mul nXX;
			cmp xx[eax], 0;
			jne to_sub;
		}
		xx[nXX] = new CUSuperInt(x);
		xx[nXX]->Double(nXX);
		__asm
		{
to_sub:	
			mov eax, type xx;
			mul nXX;
			mov ebx, xx[eax];						
			mov esi, [ebx]xx.pValue;
			mov ecx, [ebx]xx.last

			mov edx, this;
			mov edi, [edx]this.pValue;
			mov eax, [edx]this.last;
			sub eax, ecx;
			shl eax, 2;
			add edi, eax;
			push eax;	//保存eax

			xor ebx, ebx;	//作为变址寄存器
			clc;
sub_loop:
			mov eax, [esi][ebx*4];
			sbb [edi][ebx*4], eax;
			inc ebx;
			loop sub_loop;

			//调整last
			mov edi, [edx]this.pValue;
			mov eax, [edx]this.last;
			mov ecx, eax;
			dec eax;
			shl eax, 2;
			add edi, eax;

			xor eax, eax;
			repe scasd;
			inc ecx;
			mov [edx]this.last, ecx;

			//将商的相应位置1
			mov esi, r.pValue;
			pop eax;
			mov ecx, nXX;
			bts dword ptr [esi][eax], ecx;		
		}
	}	

	for (i=0; i<32; i++)
	{
		if (xx[i] != NULL)
		{
			delete xx[i];
		}
	}
	CUSuperInt ret(*this);
	*this = r;
	return ret;
}


DWORD CUSuperInt::operator /= (DWORD dwValue) //返回余数
{
	if (dwValue == 0)
	{
		CMyException* exception = new CMyException("除数为0", My_ErrorNo_ZeroByDiv);
		throw exception;		
	}
	__asm
	{
		mov ebx, this;
		mov esi, [ebx]this.pValue;
		mov ecx, [ebx]this.last;
		mov edx, 0;
		mov ebx, dwValue;
		jmp L2;
L1:				
		mov eax, [esi][ecx*4];
		div ebx;
		mov [esi][ecx*4], eax;
L2:
		loop L1;
		mov eax, [esi]
			div ebx;
		mov [esi], eax;
		
		mov ebx, this;
		mov eax, [ebx]this.last;
		dec eax;
		jz   L3;
		mov esi, [ebx]this.pValue;
		cmp [esi][4*eax], 0;
		jne  L3;
		dec [ebx]this.last;
L3:
		mov eax, edx;
	}
}

bool CUSuperInt::operator == (CUSuperInt& x)
{
	__asm
	{
		mov al, 1;		//return true;
		mov ebx, this;
		mov ecx, [ebx]this.last;
		mov edx, x;
		cmp ecx, [edx]x.last;
		jne  L1;		
		mov esi, [ebx]this.pValue;
		mov edi, [edx]x.pValue;
		cld;
		repe cmpsd;
		jz L2;
L1:
		mov al, 0;
L2:
	}
}

bool CUSuperInt::operator == (DWORD dwValue)
{
	return last == 1 && pValue[0] == dwValue;
}

bool CUSuperInt::operator != (CUSuperInt& x)
{
	return !operator == (x);
}

bool CUSuperInt::operator != (DWORD dwValue) 
{
	return last > 1 || pValue[0] != dwValue;
}

bool CUSuperInt::operator > (CUSuperInt& x)
{
	__asm
	{		
		pushf;
		mov al, 1;		//return true;
		mov ebx, this;
		mov ecx, [ebx]this.last;
		mov edx, x;
		cmp ecx, [edx]x.last;
		ja  L2;
		jb  L1;
		mov esi, [ebx]this.pValue;
		mov edi, [edx]x.pValue;
		mov edx, ecx;
		dec edx;
		shl edx, 2;
		add esi, edx;
		add edi, edx;
		std;
		repe cmpsd;
		ja L2;
L1:
		mov al, 0;
L2:
		popf;
	}
}

bool CUSuperInt::operator > (DWORD dwValue)
{
	return last > 1 || pValue[0] > dwValue;
}

bool CUSuperInt::operator >= (CUSuperInt& x) 
{
	return operator == (x) || operator > (x);
}

bool CUSuperInt::operator >= (DWORD dwValue)
{
	return last > 1 || (last == 1 && pValue[0] >= dwValue);
}

bool CUSuperInt::operator < (CUSuperInt& x) 
{	
	return !operator >= (x);
}

bool CUSuperInt::operator < (DWORD dwValue)
{
	return last == 1 && pValue[0] < dwValue;
}

bool CUSuperInt::operator <= (CUSuperInt& x) 
{
	return !operator > (x);
}

bool CUSuperInt::operator <= (DWORD dwValue)
{
	return last == 1 && pValue[0] <= dwValue;
}

CUSuperInt& CUSuperInt::operator -= (DWORD dwValue)	
{
	if (operator >= (dwValue))
	{
		__asm
		{		
			mov ebx, this;
			mov edi, [ebx]this.pValue;
			mov ecx, [ebx]this.last;		
			
			mov eax, dwValue;
			sub [edi], eax;
			jnc to_return;						

			sub dword ptr[edi+4], 1;
			jnz to_return;
			dec [ebx]this.last;
to_return:		
		}
	}
	else
	{
		*pValue = dwValue-*pValue;
		last = 1;
	}
	return *this;
}

CUSuperInt& CUSuperInt::operator -= (CUSuperInt& x)
{
	if (*this >= x)
	{
		__asm
		{
			mov ebx, this;
			mov edx, x;
			mov edi, [ebx]this.pValue;
			mov esi, [edx]x.pValue;
			mov ecx, [edx]x.last;	
			xor edx, edx;
			clc;
L1:	
			mov eax, [esi][edx*4];
			sbb [edi][edx*4], eax;
			inc edx;
			loop L1;		
			jnc L3;
			
			mov ecx, [ebx]this.last;
			mov ebx, x;		
			sub ecx, [ebx]x.last;			
			xor eax, eax;		
			stc;
L2:
			sbb [edi][edx*4], eax;
			jnc L3;
			inc edx;	
			loop L2;		
L3:
			//调整last
			mov ebx, this;
			mov edi, [ebx]this.pValue;
			mov eax, [ebx]this.last;
			mov ecx, eax;
			dec eax;
			shl eax, 2;
			add edi, eax;		
			xor eax, eax;
			repe scasd;
			inc ecx;
			mov [ebx]this.last, ecx;
		}
	}
	else
	{
		CUSuperInt temp(x);
		temp -= *this;
		*this = temp;
	}
	return *this;	
}

CUSuperInt CUSuperInt::operator - (CUSuperInt& x)
{
	CUSuperInt temp(*this);
	return temp -= x;
}
CUSuperInt CUSuperInt::operator - (DWORD dwValue)
{
	CUSuperInt temp(*this);
	return temp -= dwValue;
}
CUSuperInt&  CUSuperInt::operator --()			//--a
{
	return operator -= (1);
}
CUSuperInt  CUSuperInt::operator --(int)		//a--
{
	CUSuperInt temp(*this);
	operator -= (1);
	return temp;
}

CUSuperInt& CUSuperInt::operator = (char* pszVal)
{
	*pValue = 0;
	last = 1;
	
	if (pszVal[0] == '0' && (pszVal[1] == 'x' || pszVal[1] == 'X'))
	{
		//十六进制		
		int n = strlen(pszVal)-2;
		n = n / 8 + (n%8 == 0 ? 0 : 1);
		Expand(LeastOver(n));
		BYTE* pVal = (BYTE*)pValue;
		char ch;
		bool bLow = true;
		for (n=strlen(pszVal)-1; n>=2; n--)
		{	
			ch = pszVal[n];
			if (ch <= '9' && ch >= '0')
			{
				ch -= '0';
			}
			else if (ch >= 'a' && ch <='f')
			{
				ch -= 'a' - 10;
			}
			else if (ch >= 'A' && ch <='F')
			{
				ch -= 'A' - 10;
			}
			else if (ch == ' ')
			{
				continue;
			}
			else
			{
				*pValue = 0;
				last = 1;
				return *this;				
			}			
			if (bLow)
			{
				bLow = false;
				*pVal = ch;
			}
			else
			{
				bLow = true;
				*pVal++ |= ch << 4;
			}
		}
		if (!bLow)
		{
			pVal++;
		}
		n = ((int)pVal-(int)pValue);
		last = n/4;
		if (n%4 != 0)
		{
			last++;
			for (int i=0; i<4-n%4; i++)
			{
				*pVal++ = 0;
			}
		}
		if (last == 0)
		{
			last = 1;
		}
	}
	else
	{
		//十进制
		int n = strlen(pszVal);
		UINT ch;
		for (int i=0; i<n; i++)
		{
			 ch = pszVal[i];
			if (ch <='9' && ch >= '0')
			{	
				operator *= (10);
				operator += (pszVal[i] - '0');
			}
			else if (ch = ' ')
			{
				continue;
			}
			else
			{
				*pValue = 0;
				last = 1;
				break;
			}
		}
	}
	return *this;
}

void CUSuperInt::ToDecStr(CString &s)
{
	DWORD n = DWORD(last * 32 * log(2) / log(1000000000)+1)*9;
	char* pDec = (char*)s.GetBufferSetLength(n);	
	char* p = pDec;
	CUSuperInt a(*this);
	DWORD remainder;
	do
	{
		remainder = (a/=1000000000);
		__asm
		{
			mov eax, remainder;
			mov ebx, 10;
			mov edi, p;
			mov ecx, 9;
L1:
			cmp eax, 0;
			je L2;
			xor edx, edx;	
			div ebx;
			add dl, '0';
			mov [edi], dl;
			inc edi;
			loop L1;
			jmp L3;
L2:
			mov [edi], '0';
			inc edi;
			loop L2;			
L3:
			mov p, edi;
		}
		if (p+9 > pDec+n)
		{
			int c=1;
		}
	}
	while(a.last > 1 || a.pValue[0] != 0);
	while (p[-1] == '0' && p > pDec+1)
	{
		p--;
	}
	s.ReleaseBuffer(p-pDec);
	s.MakeReverse();
}

void CUSuperInt::ToHexStr(CString &s)
{
	DWORD n = DWORD(last * 9 + 1);
	char* pHex = (char*)s.GetBufferSetLength(n);
	for (DWORD i=last; i>0; i--)
	{
		sprintf(pHex, "%08x ", pValue[i-1]);
		pHex += 9;
	}
	s.ReleaseBuffer();
}

CString CUSuperInt::ToDecStr()
{
	CString s;
	ToDecStr(s);
	return s;
}

CString CUSuperInt::ToHexStr()
{   
	CString s;
	ToHexStr(s);
	return s;
}

CUSuperInt CUSuperInt::operator / (DWORD dwValue) //返回商
{
	CUSuperInt temp(*this);
	temp /= dwValue;
	return temp;
}

CUSuperInt CUSuperInt::operator / (CUSuperInt& x)//返回商
{
	CUSuperInt temp(*this);
	temp /= x;
	return temp;
}
CUSuperInt CUSuperInt::operator % (CUSuperInt& x)
{
	CUSuperInt temp(*this);
	return temp /= x;
}
DWORD CUSuperInt::operator % (DWORD dwValue)
{
	CUSuperInt temp(*this);
	return	temp /= dwValue;
}

⌨️ 快捷键说明

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