|
发表于 2004-3-21 23:55:16
|
显示全部楼层
大数运算类(转贴)
[PHP]/****************************************************************
* Cmjint class created by airsupply 2001.12.12
* magiclink.xiloo.com
*
*
*****************************************************************
* Cmjint class is a super limitless int
* you can use it to caculate a huge number
* Exsample:
*
* Cmjint i,x;
* i="-123413141431342341"; //can init as char* as long as you can
* x=123123; //can init as long
* i+=x;
* i*=x;
* i.Print();
* printf("%s",i.Str());
* if (i>x) cout <<i; //as easy as int
***************************************************************/
//---------------------------------------------------------------------------
#ifndef CLONGH
#define CLONGH
//---------------------------------------------------------------------------
#include <iostream>
using std::cout;
using std::ios;
using std: stream;
//---------------------------------------------------------------------------
typedef long typeint;//user type
typedef struct Unitint
{
typeint i;
Unitint * left;
Unitint * right;
Unitint():left(NULL),right(NULL),i(0){}
}* Punitint;
enum emLR{emLeft,emRight};
//---------------------------------------------------------------------------
// class
//---------------------------------------------------------------------------
class Cmjint
{
private:
friend ostream & operator <<(ostream &,const Cmjint &);
static void _Mul8(Cmjint &,const Cmjint &,const typeint &); //need by mul
static void _moveleft_onebit(Cmjint & , short ) ; //need by mul
static bool _getsfromhead(const Cmjint&,char *,short ) ; //need by div
static void _getnumfromright(const Cmjint & ,const short&,short & ) ; //need by div
void Allocate(Punitint & pnew,emLR linkLR);
void Constructor(){bit=plus=1;data=Ldata=Rdata=NULL;str=NULL;} //init private data
void Deletezero();
Punitint data;
Punitint Ldata; //left bounds
Punitint Rdata; //right bounds
short bit;
char * str;
static const short __B;
static const typeint __base;
static const short __h_base; //half of base
static const char _NUM[];
public:
Cmjint() {Constructor();data=new Unitint;Ldata=data;Rdata=data;}
Cmjint(const Cmjint& lint){Constructor();*this=lint;}
Cmjint(const typeint &i) {Constructor();*this=i; }
Cmjint(const char * str) {Constructor();*this=str; }
~Cmjint();
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Cmjint& operator+=( const Cmjint & );
Cmjint& operator-=(const Cmjint & );
Cmjint& operator*=( const Cmjint & );
Cmjint& operator/=( const Cmjint & );
Cmjint& operator%=( const Cmjint & );
Cmjint operator++(int);
Cmjint operator--(int);
Cmjint& operator++();
Cmjint& operator--();
Cmjint operator+(const Cmjint& ) const;
Cmjint operator-(const Cmjint & ) const;
Cmjint operator*(const Cmjint& ) const;
Cmjint operator/(const Cmjint& ) const;
Cmjint operator%(const Cmjint& ) const;
Cmjint operator-();
Cmjint& operator=( const typeint & ) ;
Cmjint& operator=( const Cmjint & );
Cmjint& operator=(const char * );
bool operator>( const Cmjint & ) const;
bool operator>=( const Cmjint & ) const;
bool operator<=( const Cmjint & ) const;
bool operator<( const Cmjint & ) const;
bool operator==( const Cmjint & ) const;
bool operator!=( const Cmjint & ) const;
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
bool plus;
void Print();
int Length()const; //number bits
void Clear(); //reset data to 0
char * c_str();
};
extern Cmjint operator+(const typeint &tthis,const Cmjint &sthis);
extern Cmjint operator-(const typeint &tthis,const Cmjint &sthis);
extern Cmjint operator*(const typeint &tthis,const Cmjint &sthis);
extern Cmjint operator/(const typeint &tthis,const Cmjint &sthis);
extern Cmjint operator%(const typeint &tthis,const Cmjint &sthis);
//~~~~~~~~
extern Cmjint operator+(const char * tthis,const Cmjint &sthis);
extern Cmjint operator-(const char * tthis,const Cmjint &sthis);
extern Cmjint operator*(const char * tthis,const Cmjint &sthis);
extern Cmjint operator/(const char * tthis,const Cmjint &sthis);
extern Cmjint operator%(const char * tthis,const Cmjint &sthis);
#endif[/PHP] |
|