基于2进制的大数类
我写的另一个大数类,基于二进制(确切的说在32位机下是2^31次进制),乘法是用移位和加法模拟的(如果不是考虑到64位兼容性的话,用long long可以显著提高乘法效率),除法是移位和减法(状况同乘法),效率上比我原来的那个低了很多(有兴趣的可以看我之前的文章,基于10^9次进制的大数类),尤其是输出函数。但毕竟是是我精心设计的,很多方面感觉还是很有价值的
有需要的朋友可以下载我的源码(基于code::blocks的工程)
下面是其中的一个头文件,包括了大数类的主要声明
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 | #ifndef LARGEINT_H_INCLUDED #define LARGEINT_H_INCLUDED #include "numarray.h" const int bits=8*sizeof(numunit)-1; //每一个存储单位的2进制位数 const int intlim=1000000000;//numunit能够容纳的最大的10的幂 const int intlimbits=9;//上面那个intLim的十进制位数(幂) class largeInt { private: numArray number; //反序保存数剧本身,最低位是number[0] int length; //number的长度 int sign; //符号1表示正,-1表示负数,特殊的,有两个0,保证0的符号是1 public: largeInt() {length=1;sign=1;} largeInt(int in) {length=1;sign=(in>=0)?1:-1;number[0]=(numunit)sign*in;} largeInt(const largeInt &in) {number=in.number;length=in.length;sign=in.sign;} largeInt& operator=(const largeInt &in) {number=in.number;length=in.length;sign=in.sign;return *this;} largeInt operator-() const {largeInt opposite=(*this);opposite.sign=-sign;return opposite;} void opposite() {sign*=-1;} //##I/O################################ largeInt& read(); largeInt& read(const char *); void print() const; private: void printAll(largeInt &num) const; void printDecimalUnit(int unit,int len) const; void printDecimalUnit(int unit) const; public: //##compare############################ bool operator==(const largeInt&) const; bool operator>(const largeInt&) const; bool operator<(const largeInt&) const; bool operator>=(const largeInt &right) const {return !(*this<right);} bool operator<=(const largeInt &right) const {return !(*this>right);} bool operator!=(const largeInt &right) const {return !(*this==right);} //##movement########################### largeInt operator<<(int n) const {largeInt solution=*this;solution<<=n;return solution;} largeInt operator>>(int n) const {largeInt solution=*this;solution>>=n;return solution;} const largeInt& operator<<=(int); const largeInt& operator>>=(int); private: void moveLeft(int); //大移位,移1就是移动bits位 void moveRight(int); public: //##+ - * / %########################## largeInt operator++(int) {*this+=1;return *this-1;}//后缀运算 const largeInt& operator++() {return *this+=1;}//前缀 largeInt operator--(int) {*this-=1;return *this+1;} const largeInt& operator--() {return *this-=1;} //************************************* largeInt operator+(const largeInt &right) const {largeInt solution=*this;solution+=right;return solution;} const largeInt& operator+=(const largeInt&); largeInt operator-(const largeInt &right) const {largeInt solution=*this;solution-=right;return solution;} const largeInt& operator-=(const largeInt&); largeInt operator*(const largeInt&) const; largeInt operator*(int) const; const largeInt operator*=(const largeInt &right) {return *this=*this*right;} const largeInt operator*=(int right) {return *this=*this*right;} largeInt operator/(largeInt&) const; largeInt operator/(int) const; largeInt operator/(const largeInt &divi) const {largeInt divisor=divi;return *this/divisor;} const largeInt& operator/=(largeInt &divisor) {return *this=*this/divisor;} const largeInt& operator/=(const largeInt &divi) {largeInt divisor=divi;return *this/=divisor;} const largeInt& operator/=(int divisor) {return *this=*this/divisor;} largeInt operator%(const largeInt &divisor) const {largeInt solution;solution%=divisor;return solution;} largeInt operator%(largeInt &divisor) const {largeInt solution;solution%=divisor;return solution;} const largeInt& operator%=(largeInt&); const largeInt& operator%=(const largeInt &divisor) {largeInt divi=divisor;return *this%=divi;} const largeInt& operator%=(int); int operator%(int) const; //特殊的取模函数,用于返回int型的余数 //************************************** friend largeInt operator+(int a,const largeInt& b) {return b+a;} friend largeInt operator-(int a,const largeInt& b) {return -(b-a);} friend largeInt operator*(int a,const largeInt& b) {return b*a;} friend largeInt operator/(int a,const largeInt& b) {largeInt x=a;return a/b;} friend largeInt operator%(int a,const largeInt& b) {largeInt x=a;return a%b;} private: }; #endif // LARGEINT_H_INCLUDED |



