大数模板

来源:互联网 发布:路由器选择算法 要求 编辑:程序博客网 时间:2024/06/08 10:36

参考刘汝佳算法竞赛入门经典一书;

太菜了,现在写的还不完善;只有一些基础的;

#include<bits/stdc++.h>using namespace std;struct BigInteger{static const int BASE=10000;static const int WIDTH=4;vector<long long>s;BigInteger(long long num=0) { *this=num; }//构造函数BigInteger operator = (long long num)//赋值运算符 {s.clear();do{s.push_back(num%BASE);num/=BASE;}while(num>0);return *this;}BigInteger operator = (const string& str)//赋值运算符 {s.clear();int x,len=(str.length()-1)/WIDTH+1;for(int i=0;i<len;i++){int end=str.length()-i*WIDTH;int start=max(0,end-WIDTH);sscanf(str.substr(start,end-start).c_str(),"%d",&x);s.push_back(x);}return *this;}BigInteger operator + (const BigInteger& b) const// +运算符 {BigInteger c;c.s.clear();for(int i=0,g=0;;i++){if(g==0&&i>=s.size()&&i>=b.s.size()) break;int x=g;if(i<s.size()) x+=s[i];if(i<b.s.size()) x+=b.s[i];c.s.push_back(x%BASE);g=x/BASE;}return c;}BigInteger operator - (const BigInteger& b) const    // -运算符 (a>b){BigInteger c;c.s.clear();for(int i=0,g=0;;i++){if(g==0&&i>=s.size()&&i>=b.s.size()) break;int x=g;g=0;if(i<s.size()) x+=s[i];      if(i<b.s.size()) x-=b.s[i];      if(x<0)        {        x+=BASE;g=-1;      }      if(x!=0) c.s.push_back(x);}return c;}BigInteger operator * (const BigInteger& b) const    // *运算符 {BigInteger c;c.s.clear();int lena=s.size(),lenb=b.s.size(),lenc=lena+lenb;for(int i=0;i<lenc;i++){c.s.push_back(0);}for(int i=0;i<lena;i++){for(int j=0;j<lenb;j++){c.s[i+j]+=s[i]*b.s[j];}}for(int i=0;i<lenc-1;i++)      {        c.s[i+1]+=c.s[i]/BASE;      c.s[i]%=BASE;    }        for(int i=lenc-1;c.s[i]==0;i--)    {    c.s.pop_back();}return c;}BigInteger operator / (const long long& b) const//  /运算符 (大数除小数){BigInteger c;c.s.clear();for(int i=0;i<s.size();i++){c.s.push_back(0);}long long g=0;for(int i=s.size()-1;i>=0;i--)      {           c.s[i]=(s[i]+g*BASE)/b;          g=s[i]+g*BASE-c.s[i]*b;       }    for(int i=s.size()-1;c.s[i]==0;i--)    {    c.s.pop_back();}return c;}BigInteger operator % (const long long& b) const// %运算符 (大数取模小数){long long ans=0,lena=s.size();for(int i=lena-1;i>=0;i--){ans=(ans*BASE+s[i])%b;}return ans;}/*BigInteger operator / (const BigInteger& b)const// /运算符 (大数除大数){}BigInteger operator % (const BigInteger& b)const// %运算符 (大数取模大数){}*/BigInteger operator += (const BigInteger& b)    // +=运算符 {*this=*this+b;return *this;}BigInteger operator -= (const BigInteger& b)    // -=运算符 {*this=*this-b;return *this;}BigInteger operator *= (const BigInteger& b)// *=运算符 {*this=*this*b;return *this;}BigInteger operator /= (const long long& b)// /=运算符 {*this=*this/b;return *this;}BigInteger operator %= (const long long& b)// %=运算符 {*this=*this%b;return *this;}/*BigInteger operator /= (const BigInteger& b)// /=运算符 {*this=*this/b;return *this;}BigInteger operator %= (const BigInteger& b)// %=运算符 {*this=*this%b;return *this;}*/bool operator < (const BigInteger& b) const    //<运算符 {if(s.size()!=b.s.size()) return s.size()<b.s.size();for(int i=s.size()-1;i>=0;i--)if(s[i]!=b.s[i]) return s[i]<b.s[i];return false;}bool operator > (const BigInteger& b) const { return b<*this; }    //>运算符 bool operator <= (const BigInteger& b) const { return !(b<*this); }    //<=运算符 bool operator >= (const BigInteger& b) const { return !(*this<b); }    //>=运算符 bool operator != (const BigInteger& b) const { return b<*this||*this<b; }    //!=运算符 bool operator == (const BigInteger& b) const { return !(b<*this)&&!(*this<b); }    //==运算符 };int tp;ostream& operator << (ostream &out,const BigInteger &x)    //重载<<使得可以直接输入大数 {out<<x.s.back();for(int i= x.s.size()-2;i>=0;i--){char buf[20];sprintf(buf,"%04d",x.s[i]);for(int j=0;j<strlen(buf);j++) out<<buf[j];}return out;}istream& operator >> (istream &in, BigInteger &x)    //重载>>使得可以直接输入大数 {string s;if(!(in >> s)) return in;x=s;return in;}int main(){BigInteger a,b;while(cin>>a>>b) {cout<<a+b<<endl;}    return 0;}


原创粉丝点击