?? vlong.cpp
字號:
// vlong.cpp: implementation of the vlong class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "vlong.h"
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
vlong::vlong (unsigned x)
{
pValue = new vlong_value;
negative = 0;
pValue->init(x);
}
vlong::vlong ( const vlong& x ) // copy constructor
{
negative = x.negative;
pValue = x.pValue;
pValue->share += 1;
}
/**/
vlong::~vlong()
{
if ( pValue->share ) pValue->share -=1; else delete pValue;
}
void vlong::docopy()
{
if ( pValue->share )
{
pValue->share -= 1;
vlong_value * nv = new vlong_value;
nv->copy(*pValue);
pValue = nv;
}
}
int vlong::cf( const vlong x ) const
{
int neg = negative && !pValue->is_zero();
if ( neg == (x.negative && !x.pValue->is_zero()) )
return pValue->cf( *x.pValue );
else if ( neg ) return -1;
else return +1;
}
vlong& vlong::operator =(const vlong& x)
{
if ( pValue->share ) pValue->share -=1; else delete pValue;
pValue = x.pValue;
pValue->share += 1;
negative = x.negative;
return *this;
}
/**/
vlong::operator unsigned () // conversion to unsigned
{
return *pValue;
}
vlong& vlong::operator +=(const vlong& x)
{
if ( negative == x.negative )
{
docopy();
pValue->add( *x.pValue );
}
else if ( pValue->cf( *x.pValue ) >= 0 )
{
docopy();
pValue->subtract( *x.pValue );
}
else
{
vlong tmp = *this;
*this = x;
*this += tmp;
}
return *this;
}
vlong& vlong::operator -=(const vlong& x)
{
if ( negative != x.negative )
{
docopy();
pValue->add( *x.pValue );
}
else if ( pValue->cf( *x.pValue ) >= 0 )
{
docopy();
pValue->subtract( *x.pValue );
}
else
{
vlong tmp = *this;
*this = x;
*this -= tmp;
negative = 1 - negative;
}
return *this;
}
/////////////////////////////////////////////////////////////////////////////
vlong operator +( const vlong& x, const vlong& y )
{
vlong result = x;
result += y;
return result;
}
vlong operator -( const vlong& x, const vlong& y )
{
vlong result = x;
result -= y;
return result;
}
vlong operator *( const vlong& x, const vlong& y )
{
vlong result;
result.pValue->mul( *x.pValue, *y.pValue );
result.negative = x.negative ^ y.negative;
return result;
}
vlong operator /( const vlong& x, const vlong& y )
{
vlong result;
vlong_value rem;
result.pValue->divide( *x.pValue, *y.pValue, rem );
result.negative = x.negative ^ y.negative;
return result;
}
vlong operator %( const vlong& x, const vlong& y )
{
vlong result;
vlong_value divide;
divide.divide( *x.pValue, *y.pValue, *result.pValue );
result.negative = x.negative; // not sure about this?
return result;
}
vlong modexp( const vlong & x, const vlong & e, const vlong & m )
{
monty me(m);
return me.exp( x,e );
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -