- 论坛徽章:
- 1
|
- #ifndef BIGINT_H_
- #define BIGINT_H_
- #include <iostream>
- using namespace std;
- class Error
- {
- public:
- Error(char *rea):_rea(rea){}
- char *what(){return _rea;}
- private:
- char *_rea;
- };
- const int Max_Bit_Len= 1024;
- class bigint
- {
- public:
- bigint();
- bigint(int num);
- bigint(const char *num);
- bigint(const bigint& obj); //复制构造函数
- inline bigint operator-()const; //取相反值
- bigint operator()()const; //取绝对值仿函数
- int get_len()const{return len;}; //大数位数
- bigint operator+(const bigint& obj)const;
- bigint operator-(const bigint& obj)const;
- bigint& operator+=(const bigint& obj);
- bigint operator*(const bigint& obj)const;
- bigint operator/(const bigint& obj)const;
- bigint operator%(const bigint& obj)const;
- bool operator>(const bigint& obj)const;
- bool operator<(const bigint& obj)const;
- bool operator>=(const bigint& obj)const;
- bool operator==(const bigint& obj)const;
- friend std::istream& operator>>(std::istream& in,bigint& obj);
- friend std::ostream& operator<<(std::ostream& out,bigint& obj);
- private:
- int len;
- int sign;
- int BitArray[Max_Bit_Len];
- void set_len(); //位数处理
- void shift(int bit); //移位 >=0 右移 否则 左移 用于除法
- };
- #endif
复制代码- #include <cstring>
- #include <cctype>
- #include <string>
- #include "bigint.h"
- bigint::bigint()
- {
- memset(BitArray,0x0,sizeof(BitArray));
- len=1;
- sign=1;
- }
- bigint::bigint(int num)
- {
- if(num < 0)
- {
- sign=-1;
- num=-num;
- }
- else
- sign=1;
- int *ptr=BitArray;
- for(; num!=0; ptr++)
- {
- *ptr=num%10;
- num /= 10;
- }
- len=(int)(ptr-BitArray);
- len=len?len:1;
- memset(ptr,0x0,sizeof(int)*(Max_Bit_Len-len));
- }
- bigint::bigint(const bigint& obj)
- {
- if(this!=&obj)
- {
- len=obj.len;
- sign=obj.sign;
- memcpy(BitArray,obj.BitArray,sizeof(BitArray));
- }
- }
- bigint::bigint(const char *num)
- {
- len=strlen(num);
- sign=1;
- if(*num=='-'||*num=='+')
- {
- if(*num=='-')
- sign=-1;
- num++;
- len--;
- }
- for(int i=0;i<len;i++)
- {
- if(isdigit(*num))
- BitArray[len-i-1]=*num++ -'0';
- else
- throw Error("Input Error!");
- }
- if(len==0)
- len=1;
- memset(BitArray+len,0x0,(Max_Bit_Len-len)*sizeof(int));
- }
- bigint bigint::operator-()const //-a
- {
- bigint res(*this);
- res.sign=-sign;
- return res;
- }
- bigint bigint::operator+(const bigint& obj)const
- {
- bigint res;
- if(sign==obj.sign)
- {
- res.len=0;
- int k=(len>obj.len) ? len : obj.len;
- for(int i=0,x=0; i<k||x; i++)
- {
- if(i<len) x+=BitArray[i];
- if(i<obj.len) x+=obj.BitArray[i];
- //x+=BitArray[i]+obj.BitArray[i];
- res.BitArray[res.len++]=x%10;
- if(res.len >= Max_Bit_Len) throw Error("位数超过最大值");
- x /= 10;
- }
- res.sign=sign;
- }
- else
- res=(*this)-(-obj);
- return res;
- }
- bigint bigint::operator()()const //|a|
- {
- bigint res(*this);
- res.sign=1;
- return res;
- }
- void bigint::set_len()
- {
- while(len>1 && !BitArray[len-1])
- len--;
- }
- bigint bigint::operator-(const bigint& obj)const
- {
- bigint res;
- if(sign==obj.sign)
- {
- const bigint *max,*min;
- if((*this)()>obj())
- {
- max=this; min=&obj; res.sign=sign;
- }
- else
- {
- max=&obj; min=this; res.sign=-sign;
- }
- res.len=max->len;
- for(int i=0;i<max->len;i++)
- {
- res.BitArray[i]+= max->BitArray[i]-min->BitArray[i];
- if(res.BitArray[i] < 0)
- {
- res.BitArray[i] += 10;
- res.BitArray[i+1]--;
- }
- }
- res.set_len();
- }
- else
- res=(*this)+(-obj);
- return res;
- }
- bool bigint::operator>(const bigint& obj)const
- {
- if(sign > obj.sign) return true;
- if(sign < obj.sign) return false;
- if(sign==1)
- {
- if(len!=obj.len) return len > obj.len;
- for(int i=len-1;i>=0;i--)
- {
- if(BitArray[i]!=obj.BitArray[i])
- return BitArray[i] > obj.BitArray[i];
- }
- }
- else
- {
- if(len!=obj.len) return len < obj.len;
- for(int i=len-1;i>=0;i--)
- {
- if(BitArray[i]!=obj.BitArray[i])
- return BitArray[i] < obj.BitArray[i];
- }
- }
- return false; //==
- }
- bool bigint::operator<(const bigint& obj)const
- {
- return obj>*this;
- }
- bool bigint::operator>=(const bigint& obj)const
- {
- return !(obj > *this);
- }
- bool bigint::operator==(const bigint& obj)const
- {
- return (!(obj>*this))&&(!(*this>obj));
- }
- bigint& bigint::operator+=(const bigint& obj)
- {
- *this=*this+obj;
- return *this;
- }
- bigint bigint::operator*(const bigint& obj)const
- {
- bigint res;
- res.len=len+obj.len;
- for(int i=0;i<len;i++)
- {
- for(int j=0;j<obj.len;j++)
- {
- if(i+j >= Max_Bit_Len) throw Error("位数超过最大值");
- res.BitArray[i+j] += BitArray[i]*obj.BitArray[j];
- }
- }
- for(int i=0;i<res.len-1;i++)
- {
- res.BitArray[i+1] += res.BitArray[i]/10;
- res.BitArray[i] %= 10;
- }
- res.set_len();
- res.sign=(sign==obj.sign) ? 1 : -1;
- return res;
- }
- void bigint::shift(int bit)
- {
- if(bit >=0) // >=0 右移
- {
- for(int i=len+bit-1;i>=0;i--)
- BitArray[i]=(i>=bit) ? BitArray[i-bit] : 0;
- }
- else // <0 左移
- {
- for(int i=0;i<len;i++)
- BitArray[i]=(i-bit<len) ? BitArray[i-bit] : 0;
- }
- len += bit;
- }
- bigint bigint::operator/(const bigint& obj)const
- {
- if(obj==bigint(0))
- throw Error("Dirisor can not be zero!");//除数不能为零
- bigint did=(*this)();//取绝对值
- bigint dir=obj();
- if(dir > did) return bigint(0);
- bigint res;
- int m=len-obj.len;
- dir.shift(m); //右移m位
- while(m>=0)
- {
- if(did >= dir) //被除数>=除数
- {
- did =did- dir;
- res.BitArray[m]++;
- }
- else
- {
- m--;
- res.len++; //???
- dir.shift(-1);//左移一位
- }
- }
- res.set_len();
- res.sign=(sign==obj.sign) ? 1 :-1;
- return res;
- }
- bigint bigint::operator%(const bigint& obj)const
- {
- bigint res;
- res=(*this)-((*this)/obj)*obj;
- return res;
- }
- std::istream& operator>>(std::istream& in,bigint& obj)
- {
- string tmp;
- in>>tmp;
- obj=tmp.c_str();
- return in;
- }
- std::ostream& operator<<(std::ostream& out,bigint& obj)
- {
- if(obj.sign==-1)
- out<<'-';
- for(int i=obj.len-1;i>=0;i--)
- out<<(obj.BitArray[i]);
- return out;
- }
复制代码- #include <iostream>
- #include "bigint.h"
- using namespace std;
- bigint test(unsigned);
- int main(void)
- {
- bigint c;
- /*bigint a("-11"),b("-11");
- c=a/b;
- cout<<c<<endl;
- cout<<c.get_len()<<endl;
- */
- c=test(458);
- cout<<c<<endl;
- cout<<c.get_len()<<endl;
- return 0;
- }
- bigint test(unsigned n)
- {
- bigint res(1);
- for(unsigned i=2;i<=n;i++)
- res= res*i;
- return res;
- }
复制代码 |
|