资源简介
包含大整数的各种运算函数,如加法、减法、乘法、除法、取余等
代码片段和文件信息
/****************************************************************/
//大数运算库源文件:BigInt.cpp
//作者:afanty@vip.sina.com
//版本:1.0 (2003.4.26)
//说明:适用于MFC
/****************************************************************/
#include “stdafx.h“
#include “BigInt.h“
CBigInt::CBigInt()
{
m_nSign=1;
m_nLength=1;
for(int i=0;i }
CBigInt::~CBigInt()
{
}
int CBigInt::Cmp(CBigInt& A)
{
if(m_nLength>A.m_nLength)return 1;
if(m_nLength for(int i=m_nLength-1;i>=0;i--)
{
if(m_ulValue[i]>A.m_ulValue[i])return 1;
if(m_ulValue[i] }
return 0;
}
CBigInt& CBigInt::Mov(CBigInt& A)
{
m_nLength=A.m_nLength;
for(int i=0;i return *this;
}
CBigInt& CBigInt::Mov(unsigned __int64 A)
{
if(A>0xffffffff)
{
m_nLength=2;
m_ulValue[1]=(unsigned long)(A>>32);
m_ulValue[0]=(unsigned long)A;
}
else
{
m_nLength=1;
m_ulValue[0]=(unsigned long)A;
}
for(int i=m_nLength;i return *this;
}
CBigInt CBigInt::Add(CBigInt& A)
{
CBigInt X;
if(X.m_nSign==A.m_nSign)
{
X.Mov(*this);
int carry=0;
unsigned __int64 sum=0;
if(X.m_nLength for(int i=0;i {
sum=A.m_ulValue[i];
sum=sum+X.m_ulValue[i]+carry;
X.m_ulValue[i]=(unsigned long)sum;
if(sum>0xffffffff)carry=1;
else carry=0;
}
if(X.m_nLength {
X.m_ulValue[X.m_nLength]=carry;
X.m_nLength+=carry;
}
return X;
}
else{X.Mov(A);X.m_nSign=1-X.m_nSign;return Sub(X);}
}
CBigInt CBigInt::Add(long A)
{
CBigInt X;
if((m_nSign*A)>=0)
{
X.Mov(*this);
unsigned __int64 sum;
sum=A+X.m_ulValue[0];
X.m_ulValue[0]=(unsigned long)sum;
if(sum>0xffffffff)
{
int i=1;
while(X.m_ulValue[i]==0xffffffff){X.m_ulValue[i]=0;i++;}
X.m_ulValue[i]++;
if(i }
return X;
}
else return Sub(-A);
}
CBigInt CBigInt::Sub(CBigInt& A)
{
CBigInt X;
if(m_nSign==A.m_nSign)
{
X.Mov(*this);
int cmp=X.Cmp(A);
if(cmp==0){X.Mov(0);return X;}
int lencarry=0;
unsigned __int64 num;
unsigned long *s*d;
if(cmp>0){s=X.m_ulValue;d=A.m_ulValue;len=X.m_nLength;}
if(cmp<0){s=A.m_ulValue;d=X.m_ulValue;len=A.m_nLength;X.m_nSign=1-X.m_nSign;}
for(int i=0;i {
if((s[i]-carry)>=d[i])
{
X.m_ulValue[i]=s[i]-carry-d[i];
carry=0;
}
else
{
num=0x100000000+s[i];
X.m_ulValue[i]=(unsigned long)(num-carry-d[i]);
carry=1;
}
}
while(X.m_ulValue[len-1]==0)len--;
X.m_nLength=len;
return X;
}
else{X.Mov(A);X.m_nSign=1-X.m_nSign;return Add(X);}
}
CBigInt CBigInt::Sub(long A)
{
CBigInt X;
if((m_nSign*A)>=0)
{
X.Mov(*this);
if(X.m_ulValue[0]>=(unsigned long)A){X.m_ulValue[0]-=A;return X;}
if(X.m_nLength==1){X.m_ulValue[0]=A-X
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 1162 2003-04-28 13:36 BigInt.h
文件 8437 2003-04-28 13:36 BigInt.cpp
----------- --------- ---------- ----- ----
9599 2
- 上一篇:STC15系列原理图及封装
- 下一篇:Kd-tree改进后的BBF算法
评论
共有 条评论