资源简介
进行大数的四则运算以及mod运算,大数的生成,和素性检测
代码片段和文件信息
// BigInt.cpp : 定义控制台应用程序的入口点。
//
#include
#include “CBigInt.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_nSign=A.m_nSign;
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;
}
m_nSign=1;
for(int i=m_nLength;i return *this;
}
CBigInt CBigInt::Add(CBigInt& A)
{
if(m_nSign==A.m_nSign)
{
unsigned __int64 sum=0sum2=0carry=0;
if(m_nLength for(int i=0; i {
sum=0; sum2=0;
sum=m_ulValue[i];
sum2=A.m_ulValue[i];
sum=sum+sum2+carry;
m_ulValue[i]=(unsigned long)sum;
carry=unsigned long(sum>>32);
}
if((m_nLength0))
{
m_ulValue[m_nLength]=(unsigned long)carry;
m_nLength++;
}
return *this;//引用时应用Mov
}
else{A.m_nSign=-A.m_nSign;return Sub(A);}
}
CBigInt CBigInt::Add(unsigned long A)
{
CBigInt *Y;
Y=new CBigInt;
Y->m_nSign=1;
Y->m_ulValue[0]=A;
Mov(Add(*Y));
delete Y;
return *this;
}
CBigInt CBigInt::Sub(CBigInt& A)
{
if(m_nSign==A.m_nSign)
{
int cmp=Cmp(A);
if(cmp==0) {Mov(0); return *this;}
int len=0;
unsigned __int64 num=0num2=0num3=0;
unsigned long *s*dcarry=0;
if(cmp>0){s=m_ulValue;d=A.m_ulValue;len=m_nLength;}
if(cmp<0){s=A.m_ulValue;d=m_ulValue;len=A.m_nLength;m_nSign=-m_nSign;}
for(int i=0;i {
num=0;num2=0;num3=0;
num=s[i]num2=carrynum3=d[i];
if(num>=num2+num3)
{
m_ulValue[i]=s[i]-carry-d[i];
carry=0;
}
else
{
num=0x100000000+num;
m_ulValue[i]=(unsigned long)(num-num2-num3);
carry=1;
}
}
while((m_ulValue[len-1]==0)&&(len>1)) len--;
m_nLength=len;
return *this;
}
else {A.m_nSign=-A.m_nSign; return Add(A);}
}
CBigInt CBigInt::Sub(unsigned long A)
{
CBigInt *Y;
Y=new CBigInt;
Y->m_nSign=1;
Y->m_ulValue[0]=A;
Mov(Sub(*Y));
delete Y;
return *this;
}
CBigInt CBigInt::Mul(CBigInt& A)
{
CBigInt *X*Y*carry;///// Y存积的低位carry存积的高位
X=new CBigInt;
Y=new CBigInt;
carry=new CBigInt;
unsigned __int64 mul=0mul2=0;
for(int i=0;i {
int ij=0;
for(int j=0;j {
mul=0; mul2=0;
ij=i+j;
mul=m_ulValue[j];
mul2=A.m_ulValue[i];
mul=mul*mul2;
Y->m_ulValue[ij]=(unsig
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 7605 2009-03-03 22:17 CBigInt.cpp
文件 1058 2009-03-03 22:12 CBigInt.h
----------- --------- ---------- ----- ----
8663 2
- 上一篇:lcd12864的VHDL程序
- 下一篇:联想F40/F41bios3.13版
评论
共有 条评论