📄 bigint.h
字号:
__asm
{
bsr eax, bit_x;
mov bit_x, eax
}
int nXX; //减数在减数数组中的编号
while (*this >= *xx[0])
{
__asm
{
//计算出高DWORD中二进制位1的位置差
mov ebx, this;
mov esi, [ebx]this.pValue;
mov ecx, [ebx]this.last;
bsr eax, [esi][4*ecx-4];
sub eax, bit_x;
add eax, 32;
and eax, 0x1f;
mov nXX, eax;
//如果减数指针为空,则新分配
mov eax, type xx;
mul nXX;
cmp xx[eax], 0;
jne to_cmp;
}
xx[nXX] = new CBigInt(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 CBigInt(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];
}
}
CBigInt ret(*this);
*this = r;
return ret;
}
DWORD CBigInt::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 CBigInt::operator == (CBigInt& 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 CBigInt::operator == (DWORD dwValue)
{
return last == 1 && pValue[0] == dwValue;
}
bool CBigInt::operator != (CBigInt& x)
{
return !operator == (x);
}
bool CBigInt::operator != (DWORD dwValue)
{
return last > 1 || pValue[0] != dwValue;
}
bool CBigInt::operator > (CBigInt& 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 CBigInt::operator > (DWORD dwValue)
{
return last > 1 || pValue[0] > dwValue;
}
bool CBigInt::operator >= (CBigInt& x)
{
return operator == (x) || operator > (x);
}
bool CBigInt::operator >= (DWORD dwValue)
{
return last > 1 || (last == 1 && pValue[0] >= dwValue);
}
bool CBigInt::operator < (CBigInt& x)
{
return !operator >= (x);
}
bool CBigInt::operator < (DWORD dwValue)
{
return last == 1 && pValue[0] < dwValue;
}
bool CBigInt::operator <= (CBigInt& x)
{
return !operator > (x);
}
bool CBigInt::operator <= (DWORD dwValue)
{
return last == 1 && pValue[0] <= dwValue;
}
CBigInt& CBigInt::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;
}
CBigInt& CBigInt::operator -= (CBigInt& 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
{
CBigInt temp(x);
temp -= *this;
*this = temp;
}
return *this;
}
CBigInt CBigInt::operator - (CBigInt& x)
{
CBigInt temp(*this);
return temp -= x;
}
CBigInt CBigInt::operator - (DWORD dwValue)
{
CBigInt temp(*this);
return temp -= dwValue;
}
CBigInt& CBigInt::operator --() //--a
{
return operator -= (1);
}
CBigInt CBigInt::operator --(int) //a--
{
CBigInt temp(*this);
operator -= (1);
return temp;
}
CBigInt& CBigInt::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 CBigInt::ToDecStr(std::string &s)
{
DWORD n = DWORD(last * 32 * log(2) / log(1000000000)+1)*9;
s.resize(n,0);
char* pDec = (char*)s.begin();
char* p = pDec;
CBigInt 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.resize(p-pDec);
char temp;
for(int m=0;m<s.size()/2;m++)
{
temp=s[m];
s[m]=s[s.size()-1-m];
s[s.size()-1-m]=temp;
}
}
void CBigInt::ToHexStr(std::string &s)
{
DWORD n = DWORD(last * 9 + 1);
s.resize(n,0);
char* pHex =(char *) s.begin();
for (DWORD i=last; i>0; i--)
{
sprintf(pHex, "%08x ", pValue[i-1]);
pHex += 9;
}
s.resize(pHex-(char*)s.begin());
}
std::string CBigInt::ToDecStr()
{
std::string s;
ToDecStr(s);
return s;
}
std::string CBigInt::ToHexStr()
{
std::string s;
ToHexStr(s);
return s;
}
CBigInt CBigInt::operator / (DWORD dwValue) //返回商
{
CBigInt temp(*this);
temp /= dwValue;
return temp;
}
CBigInt CBigInt::operator / (CBigInt& x)//返回商
{
CBigInt temp(*this);
temp /= x;
return temp;
}
CBigInt CBigInt::operator % (CBigInt& x)
{
CBigInt temp(*this);
return temp /= x;
}
DWORD CBigInt::operator % (DWORD dwValue)
{
CBigInt temp(*this);
return temp /= dwValue;
}
#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -