//bigint.h #ifndef __M_BIGINT_H #define __M_BIGINT_H using namespace std; #define MAXN 1024 class BigInt { public: BigInt(); BigInt(long int); BigInt(string ); BigInt(size_t *,int ,bool ); virtual ~BigInt(); BigInt operator +(BigInt &); BigInt operator -(BigInt &); BigInt operator *(BigInt &); BigInt operator /(BigInt &); BigInt operator %(BigInt &); BigInt operator -(); bool operator >(BigInt &); bool operator <(BigInt &); bool operator >=(BigInt &); bool operator <=(BigInt &); bool operator ==(BigInt &); bool operator !=(BigInt &); BigInt operator +=(BigInt &); BigInt operator -=(BigInt &); BigInt operator *=(BigInt &); BigInt operator /=(BigInt &); BigInt operator %=(BigInt &); BigInt operator ++(); BigInt operator --(); int size(); BigInt abs(const BigInt &a); friend std::ostream& operator <<(std::ostream &,BigInt &); friend std::istream& operator >>(std::istream &,BigInt &); protected: BigInt dis_zero(); friend BigInt abs_max(const BigInt a,const BigInt b); friend BigInt abs_min(const BigInt a,const BigInt b); friend int at(const BigInt ,int ); friend BigInt sub_num(const BigInt ,int ); friend BigInt push(const BigInt,int); private: size_t bit[MAXN]; size_t flag; size_t len; }; #endif //bigint.cpp #include <iostream> #include <string> #include <assert.h> #include "bigint.h" using namespace std; BigInt::BigInt(string a) { assert(a.size()<MAXN/2); memset(bit,0,sizeof(size_t)*MAXN); if (a[0]=='-') flag=1; else flag=0; len=a.size()-flag; for(int i=flag;i<a.size();++i) { if ((int)a[i]>=48&&(int)a[i]<=57) bit[a.size()-i-1]=(int)a[i]-48; else { cout<<"Initialization error!"<<endl; BigInt t; *this=t; } } this->dis_zero(); } BigInt::BigInt(long int a) { memset(bit,0,sizeof(size_t)*MAXN); if (a<0) { flag=1; a=a*(-1); } else if (a==0) { len=1; flag=0; } else { flag=0; len=0; } while (a) { int t=a%10; bit[len++]=t; a/=10; } } BigInt::BigInt(size_t *a,int n,bool b) { assert(n<MAXN/2); memset(bit,0,sizeof(size_t)*MAXN); len=n; if (b) flag=0; else flag=1; for (int i=n-1;i>=0;--i) { if (a[i]>=0&&a[i]<=9) bit[n-i-1]=a[i]; else { cout<<"Initialization error!"<<endl; BigInt t; *this=t; } } this->dis_zero(); } BigInt::BigInt() { memset(bit,0,sizeof(size_t)*MAXN); len=0; flag=0; } ostream& operator <<(ostream &out,BigInt &a) { if (a.flag) out<<'-'; for (int i=a.len-1;i>=0;--i) { out<<a.bit[i]; } return out; } istream& operator >>(istream &in,BigInt &a) { string t; in>>t; a=BigInt(t); return in; } int BigInt::size() { return len; } BigInt::~BigInt() { //cout<<"~"<<endl; } BigInt abs_max(const BigInt a,const BigInt b) { if (a.len>b.len) return a; else if(a.len<b.len) return b; else { int i=a.len-1; while(a.bit[i]==b.bit[i]&&i-->0); if(a.bit[i]>b.bit[i]) return a; else return b; } } BigInt abs_min(const BigInt a, const BigInt b) { if (a.len<b.len) return a; else if(a.len>b.len) return b; else { int i=a.len-1; while(a.bit[i]==b.bit[i]&&i-->0); if(a.bit[i]<b.bit[i]) return a; else return b; } } BigInt BigInt::abs(const BigInt &a) { BigInt t(a); if (t.flag) return -t; else return t; } BigInt BigInt::dis_zero() { if ((len==1&&bit[0]==0)||len==0) return *this; int temp=len-1; for (int i=temp;i>=0;--i) { if (bit[i]==0) --len; else break; } return *this; } BigInt sub_num(const BigInt a,int n) { if (n<=0) return BigInt(""); BigInt t(a); int temp=t.len-n; for (int i=0;i<temp;++i) { for (int j=0;j<t.len;++j) { t.bit[j]=t.bit[j+1]; } --t.len; } return t; } int at(const BigInt a,int n) { if (n>a.len||n<=0) return -1; else return a.bit[a.len-n]; } BigInt push(const BigInt a,int n) { assert(a.len<=MAXN); BigInt t(a); for (int i=t.len-1;i>=0;--i) { t.bit[i+1]=t.bit[i]; } t.bit[0]=n; ++t.len; return t; } BigInt BigInt::operator -() { flag=!flag; return *this; } bool BigInt::operator ==(BigInt &a) { if (flag==a.flag&&len==a.len) { for (int i=0;i<len;++i) { if (bit[i]!=a.bit[i]) return false; } return true; } return false; } bool BigInt::operator >(BigInt &a) { if (flag<a.flag) return true; else if (flag>a.flag) return false; else if (flag) { if (abs(*this)==abs_max(abs(*this),abs(a))&&*this!=a) return false; else if (*this!=a) return true; } else { if (abs(*this)==abs_max(abs(*this),abs(a))&&*this!=a) return true; else if (*this!=a) return false; } return false; } bool BigInt::operator <(BigInt &a) { if (flag<a.flag) return false; else if (flag>a.flag) return true; else if (flag) { if (abs(*this)==abs_max(abs(*this),abs(a))&&*this!=a) return true; else if (*this!=a) return false; } else { if (abs(*this)==abs_max(abs(*this),abs(a))&&*this!=a) return false; else if (*this!=a) return true; } return false; } bool BigInt::operator <=(BigInt &a) { if (*this<a||*this==a) return true; else return false; } bool BigInt::operator >=(BigInt &a) { if (*this>a||*this==a) return true; else return false; } bool BigInt::operator !=(BigInt &a) { if (*this==a) return false; else return true; } BigInt BigInt::operator +=(BigInt &a) { return *this=*this+a; } BigInt BigInt::operator -=(BigInt &a) { return *this=*this-a; } BigInt BigInt::operator *=(BigInt &a) { return *this=*this*a; } BigInt BigInt::operator /=(BigInt &a) { return *this=*this/a; } BigInt BigInt::operator %=(BigInt &a) { return *this=*this%a; } BigInt BigInt::operator ++() { return *this+BigInt(1); } BigInt BigInt::operator --() { return *this-BigInt(1); } BigInt BigInt::operator +(BigInt &a) { BigInt result; if (flag!=a.flag) { BigInt t(a); return result=*this-(-t); } int i,j,temp,f=0; for (i=0,j=0;i<len||j<a.len;++i,++j) { temp=bit[i]+a.bit[j]+f; result.bit[result.len++]=temp%10; f=temp/10; } if (f) result.bit[result.len++]=f; result.flag=flag; return result; } BigInt BigInt::operator -(BigInt &a) { if (*this==a) return BigInt(0); BigInt result; if (flag!=a.flag) { BigInt t(a); return result=*this+(-t); } int i,j,temp,f=0; BigInt t1=abs_max(*this,a); BigInt t2=abs_min(*this,a); for (i=0,j=0;i<t1.len||j<t2.len;++i,++j) { temp=t1.bit[i]-t2.bit[j]-f; if (temp<0) result.bit[result.len++]=10+temp; else result.bit[result.len++]=temp; f=temp<0?1:0; } result.dis_zero(); if (*this>a) result.flag=0; else result.flag=1; return result; } BigInt BigInt::operator *(BigInt &a) { BigInt result(0); if (*this==result||a==result) return result; int i,j,temp,f; for (i=0;i<a.len;++i) { BigInt t; t.len=i; f=0; for (j=0;j<len;++j) { temp=a.bit[i]*bit[j]+f; t.bit[t.len++]=temp%10; f=temp/10; } if (f) t.bit[t.len++]=f; result+=t; } result.flag=flag^a.flag; return result; } // BigInt BigInt::operator /(BigInt &a) // { // if (a==BigInt("0")|| // *this==BigInt("0")|| // (a==abs_max(*this,a)&& // *this!=a&&(*this+a)!=BigInt("0"))) return BigInt("0"); // for (BigInt result("1");abs(a*result)<abs(*this);result=++result); // if (abs(a*result)!=abs(*this)) result=--result; // result.flag=flag^a.flag; // return result; // } BigInt BigInt::operator /(BigInt &a) { if (a==BigInt(0)|| *this==BigInt(0)|| (a==abs_max(*this,a)&& *this!=a&&(*this+a)!=BigInt(0))) return BigInt(0); BigInt result; BigInt t=sub_num(abs(*this),a.len); for (int n=a.len;n<=len;++n) { for (int p=n;t<abs(a)&&p<=len;++p) { if (p==a.len) ++p; t=push(t,at(*this,p)); t.dis_zero(); if (t<abs(a)) result=push(result,0); else break; } if ((n=p)>len) break; for (int i=1;abs(a*BigInt(i))<abs(t);++i); if (abs(a*BigInt(i))!=abs(t)) --i; result=push(result,i); t=abs(t)-abs(a*BigInt(i)); } result.dis_zero(); result.flag=flag^a.flag; return result; } BigInt BigInt::operator %(BigInt &a) { return *this-(*this/a)*a; } //main.cpp #include <iostream> #include <string> #include "bigint.h" using namespace std; int main(int argc, char **argv) { BigInt a,b; cout<<"a="; cin>>a; cout<<"b="; cin>>b; cout<<"a+b="<<a+b<<endl; cout<<"a-b="<<a-b<<endl; cout<<"a*b="<<a*b<<endl; cout<<"a/b="<<a/b<<endl; cout<<"a%b="<<a%b<<endl; return EXIT_SUCCESS; }