模板_高精度

已重载运算符

#include<cstdio>
#include<cstring>
#include<vector>
#include<iostream>
using namespace std;
struct Big {
    static const int BASE = 100000000; // change
    static const int WIDTH = 8; // change
    /*
      WIDTH可随需求不同而变动:
       ①:没有乘除mod时,WIDTH为8时效率最高,在8以上可能出错。
       ②:WIDTH增大时,加减乘都会变快,但除和mod会变慢,所以没有除和mod时,尽量把WIDTH开大,有乘法时最大为4。
       ③:如果乘数都很小像2,3,4之类的数,WIDTH也可以开到8。 
       ④:如果运算符要求很全,那么WIDTH为3比较合适。
      如果要改WIDTH的话,需要改三处地方,已在代码中标记上“change”。 
    */
    vector<int> s;
    Big(long long num = 0){*this=num;}
    Big operator = (long long num) {
        s.clear();
        do {
            s.push_back(num%BASE);
            num/=BASE;
        }while(num>0);
        return *this;
    }
    Big 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;
    }
    Big operator + (const Big& b) const {
        Big 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;
    }
    Big operator - (const Big& b) const {  
        Big c;
        c.s.clear();
        int i,g,n=s.size(),m=b.s.size();
        for(i=0,g=0;i<n;i++) {
            int x=s[i]-g;  
            if(i<m) x-=b.s[i];  
            if(x>=0) g=0;  
            else {  
                g=1;  
                x+=BASE;
            }
            c.s.push_back(x);  
        }
        i=c.s.size()-1;
        while(c.s[i]==0&&i) c.s.pop_back(),i--;
        return c; 
    }
    Big operator * (const Big &b) const {
        Big c;
        int i,j,n=s.size(),m=b.s.size(),size=m+n;
        c.s.resize(size,0);
        for(i=0;i<n;i++)
            for(j=0;j<m;j++)
                c.s[i+j] += s[i] * b.s[j];
        for(i=0;i<size;i++) {
            c.s[i+1]+=c.s[i]/BASE;
            c.s[i]%=BASE;
        }
        i=size-1;
        while(c.s[i]==0&&i) c.s.pop_back(),i--;
        return c;
    }
    Big operator / (const Big& b) const {
        Big c,f=0;
        int n=s.size(),i;
        c.s.resize(n,0);
        for(i=n-1;i>=0;i--) {
            f=f*BASE;
            f.s[0]=s[i];
            while(f>=b) {
                f-=b;
                c.s[i]++;
            }
        }
        i=n-1;
        while(c.s[i]==0&&i) c.s.pop_back(),i--;
        return c;
    }
    Big operator % (const Big &b) const {
        Big r=*this/b;
        r=*this-r*b;
        return r;
    }

    Big operator += (const Big& b){*this=*this+b;return *this;}
    Big operator -= (const Big& b){*this=*this-b;return *this;}
    Big operator *= (const Big& b){*this=*this*b;return *this;}
    Big operator /= (const Big& b){*this=*this/b;return *this;}
    Big operator %= (const Big& b){*this=*this%b;return *this;}

    bool operator < (const Big& b) const {
        int n=s.size(),m=b.s.size();
        if(m>n) return 1;
        if(m<n) return 0;
        for(int i=s.size()-1;i>=0;i--) {
            if(s[i]<b.s[i]) return 1;
            if(s[i]>b.s[i]) return 0;
        }
        return 0;
    }
    bool operator > (const Big& b) const {return b<*this;}
    bool operator >= (const Big& b) const {return !(*this<b);}
    bool operator <= (const Big& b) const {return !(b<*this);}
    bool operator == (const Big& b) const {return !(*this<b)&&!(b<*this);}
    bool operator != (const Big& b) const {return (*this<b)||(b<*this);}
};

ostream& operator << (ostream &out, const Big& x) {
    out<<x.s.back();
    for(int i=x.s.size()-2;i>=0;i--) {
        char buf[10];
        sprintf(buf,"%08d",x.s[i]); // change
        for(int j=0;j<strlen(buf);j++) out<<buf[j];
    }
    return out;
}
istream& operator >> (istream &in,Big& x) {
    string s;
    if(!(in>>s)) return in;
    x=s;
    return in;
}
int main() {
    ios_base::sync_with_stdio(0); // 关闭流同步可以变快些 
    Big a,b;
    cin>>a>>b;
    cout<<a<<endl;
    cout<<b<<endl;
    cout<<a+b<<endl;
    cout<<a-b<<endl;
    cout<<a*b<<endl;
    cout<<a/b<<endl;
    cout<<a%b<<endl;
    if(a<b) cout<<"Yes"<<endl;
    else cout<<"No"<<endl;
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值