SocietyNiu 的博客

SocietyNiu 的博客

高精模板

posted on 2018-02-05 21:21:00 | under 模板 |

高精模板

#include<iostream>
#include<string>
#include<cstring>
#include<algorithm>
using namespace std;
const int MAX_WS=200+2;
struct BigNum{
    int ws;
    int d[MAX_WS];
    //构造函数,自动执行,完成初始化
    BigNum(){
        clear();
    }
    BigNum(long long num){
        clear();
        ws=0;
        while(num){                             //123
            d[++ws]=num%10;
            num/=10;
        }
    }
    BigNum(const string& s){
        clear();
        ws=s.length();
        for(int i=1; i<=ws; i++)
            d[i]=s[ws-i]-'0';
    }
    void clear(){
        memset(d, 0, sizeof(d));
        ws=1;
    }
    BigNum operator + (const BigNum& B){//重新定义BigNum下的“+”
        BigNum C;
        C.clear();
        int i=1, jw=0;
        C.ws=max(this->ws, B.ws);
        while(i<=C.ws){
            C.d[i]=this->d[i]+B.d[i]+jw;
            jw=C.d[i]/10;
            C.d[i]%=10;
            i++;
        }
        C.d[i]=jw;
        if(jw) C.ws++;
        return C;
    }
    BigNum operator += (const BigNum& B){
        *this=*this+B;
        return *this;
    }
    BigNum operator -  (const BigNum& B){
        BigNum C;
        C.clear();
        int i=1;
        C.ws=max(this->ws, B.ws);
        while(i<=C.ws){
            C.d[i]=C.d[i]+this->d[i]-B.d[i];
            if(C.d[i]<0){
                C.d[i+1]--;
                C.d[i]+=10;
            }
            i++;
        }
        while(C.d[C.ws]==0 && C.ws>1) C.ws--;
        return C;
    }
    BigNum operator -= (const BigNum& B){
        *this=*this-B;
        return *this;
    }
    BigNum operator *  (const BigNum& B){
        BigNum C;
        C.clear();
        C.ws=this->ws+B.ws;
        for(int i=1; i<=this->ws; i++){
            int jw=0;
            for(int j=1; j<=B.ws; j++){
                C.d[i+j-1]=C.d[i+j-1]+this->d[i]*B.d[j]+jw;
                jw=C.d[i+j-1]/10;
                C.d[i+j-1]%=10;
            }
            C.d[i+B.ws]=jw;
        }
        while(C.d[C.ws]==0 && C.ws>1) C.ws--;
        return C;
    }
    BigNum operator *= (const BigNum& B){
        return *this=*this*B;
    }
    bool     operator >= (const BigNum& B){
        if(this->ws < B.ws) return false;
        if(this->ws > B.ws) return true;
        for(int i=B.ws; i>=1; i--){
            if(this->d[i]>B.d[i])   return true;
            if(this->d[i]<B.d[i])   return false;
        }
        return true;
    }
    bool     operator <  (const BigNum& B){
        return !(*this>=B);
    }
    BigNum operator /  (const BigNum& B){
        BigNum C;
        C.clear();
        if(*this<B)     return C;
        C.ws=this->ws-B.ws+1;                   //可能的最大值, 有可能是A.ws-B.ws
        BigNum bjs=*this;
        for(int i=C.ws; i>=1; i--){
            //构造减数 
            BigNum js=B;
            BigNum t(10);
            for(int j=i-1; j>=1; j--)       js*=t;
            //完成构造减数    
            while(bjs>=js){
                bjs-=js;
                C.d[i]++;
            }
        }
        if(C.d[C.ws]==0) C.ws--;
        return C;
    }
    BigNum operator %  (const BigNum& B){
        BigNum D;
        D.clear();
        D=*this-((*this/B)*B);
        return D;
    }
    friend ostream& operator << (ostream& os, const BigNum& A);
    friend istream& operator >> (istream& is, BigNum& A);
};
ostream& operator << (ostream& os, const BigNum& A){
    for(int i=A.ws; i>=1; i--)  os<<A.d[i];
    return os;
}
istream& operator >> (istream& is, BigNum& A){
    string s;
    is>>s;
    A.clear();
    A.ws=s.length();
    for(int i=1; i<=A.ws; i++)  A.d[i]=s[A.ws-i]-'0';
    return is;
}
int main(){
    BigNum A,B;
    cin>>A>>B;
    cout<<A+B;
    return 0;
}

使用这种方法,主函数特别简单易懂。但函数麻烦,不建议竞赛使用