高精度模板

class BigInt{
public:    
    vector<int> a;
    BigInt operator+(const BigInt& b)
    {
        BigInt r;
        int s=max(a.size(),b.a.size());
        for(int i=0;i<s;i++)
        {
            r.a.push_back(0);
            if(i<a.size())
            {
                r.a[i]+=a[i];
            }
            if(i<b.a.size())
            {
                r.a[i]+=b.a[i];
            }
        }
        int la=0;
        for(int i=0;i<s;i++)
        {
            r.a[i]+=la;
            la=0;
            if(r.a[i]>=10)
            {
                la+=r.a[i]/10;
                r.a[i]%=10;
                if(i+2>s)
                {
                    s=i+2;
                    r.a.push_back(0);
                }
            }
        }
        return r;
    }
    friend ostream &operator<<(ostream &output, const BigInt &x)
    { 
        for(int i=x.a.size()-1;i>=0;i--)
        {
            output<<x.a[i];
        }
        return output;
    }
    friend istream &operator>>(istream  &input, BigInt &x)
    { 
         string g;
         input>>g;
         vector<int>().swap(x.a);
        for(int i=g.size()-1;i>=0;i--)
        {
            x.a.push_back(g[i]-'0');
        }
        if(x.a.size()==0)
        {
            x.a.push_back(0);
        }
         return input; 
    }
};

 

posted @ 2021-03-14 08:43  我要见血小板  阅读(44)  评论(0编辑  收藏  举报