猫史档案馆


c++大整数运算

用户:少幽科技少幽科技查看:2 回复:3 评论:2 创建时间:2020-06-24T10:09:19


#include<bits/stdc++.h>
using namespace std;
class Bigint{
    public:
        string num;
        Bigint(string n){
            num=zero(n);
        }
        Bigint(){
            num="0";
        }
        Bigint operator-(){
            Bigint rt(num);
            if (rt.num[0]=='-')rt.num.erase(0,1);
            else rt.num.insert(0,1,'-');
            return rt;
        }
        Bigint operator+(Bigint other){
            if(isneg()&&other.isneg()){
                return -(add(-(*this),-other));
            }
            //pos&neg
            else if(isneg()==0&&other.isneg()){
                return minus((*this),-other);
            }
            //neg&pos
            else if(isneg()&&other.isneg()==0){
                return minus(other,-(*this));
            }
            else if(!(isneg()&&other.isneg())){
                return add((*this),other);
            }
            //else if(isneg()&&other.isneg()==-1)return minus(other);
            //else if(isneg()==-1&&other.isneg())return other.minus(*this);
            //else return add(other);
            return Bigint("err");
        }
        Bigint operator-(Bigint other){
            if(isneg()&&other.isneg()){
                return minus(other,-*this);
            }
            if(ispos()&&other.isneg()){
                return add(*this,-other);
            }
            if(isneg()&&other.ispos()){
                return -add(-*this,other);
            }
            else{
                return minus(*this,other);
            }
        }
        Bigint operator*(Bigint other){
            if(isneg()&&other.isneg())return mul(-*this,-other);
            else if(ispos()&&other.ispos())return mul(*this,other);
            else if(isneg()&&other.ispos()) return -mul(-*this,other);
            else if(ispos()&&other.isneg()) return -mul(*this,-other);
            return Bigint("Error");
        }
        Bigint operator/(Bigint other){
            if(isneg()==other.isneg())return div(*this,other);
            else if(isneg()&&other.ispos()) return -div(-*this,other);
            else if(ispos()&&other.isneg()) return -div(*this,-other);
            return Bigint("Error");
        }
        Bigint operator%(Bigint other){//TODO 关于负数的取余运算
            return mod(*this,other);
        }
        void operator--(){
            num=(*this-Bigint("1")).num;
        }
        void operator++(){
            num=(*this+Bigint("1")).num;
        }
        bool operator<(Bigint other){
            return lesser(*this,other,false);
        }
        bool operator<=(Bigint other){
            return lesser(*this,other,true);
        }
        bool operator>(Bigint other){
            return greater(*this,other,false);
        }
        bool operator>=(Bigint other){
            return greater(*this,other,true);
        }
        bool operator==(Bigint other){  
            return num==other.num;
        }       
    private:
        bool isneg(){
            return num[0]=='-';
        }
        bool ispos(){
            return !isneg();
        }
        string zero(string a){//去除前导零
        
            string r="";
            int i,l=a.size();
            for(i=0;a[i]=='0';i++);
            for(;i<l;i++){
                r+=a[i];
            }
            if(r=="")r="0";
            return r;
        }     
        bool lesser(Bigint i1,Bigint i2,bool is_eq){//计算小于,is_eq是否计算等于
            //neg&neg
            if(i1.isneg()&&i2.isneg()){
                return !lesser(-i1,-i2,is_eq);
            }
            else if(i1.isneg()==0&&i2.isneg()){
                return false;
            }
            else if(i1.isneg()&&i2.isneg()==0){
                return true;
            }
            else{
                int l1=i1.num.size(),l2=i2.num.size(),i;
                if(l1<l2)return true;
                else if(l1>l2)return false;
                else{
                    //此时长度相等
                    for(i=0;i<l1;i++){
                        if(i1.num[i]>i2.num[i])return false;
                        else if(i1.num[i]<i2.num[i])return true;
                    }            
                }
                //完全相等
                return is_eq;
            }
        }
        bool greater(Bigint i1,Bigint i2,bool is_eq){//计算大于,is_eq是否计算等于
            //neg&neg
            if(i1.isneg()&&i2.isneg()){
                return !lesser(-i1,-i2,is_eq);
            }
            else if(i1.isneg()==0&&i2.isneg()){
                return true;
            }
            else if(i1.isneg()&&i2.isneg()==0){
                return false;
            }
            else{
                int l1=i1.num.size(),l2=i2.num.size(),i;
                if(l1<l2)return false;
                else if(l1>l2)return true;
                else{
                    //此时长度相等
                    for(i=0;i<l1;i++){
                        if(i1.num[i]>i2.num[i])return true;
                        else if(i1.num[i]<i2.num[i])return false;
                    }            
                }
                //完全相等
                return is_eq;
            }
        }
        Bigint add(Bigint i1,Bigint i2){//+
            string a,b,c="";//c为储存的结果
            int i,j,k;
            a=i1.num,b=i2.num;
            int l1=a.size(),l2=b.size();
            int l3=max(l1,l2),ans,jinwei=0;
            if(l3>l1)a.insert(0,l3-l1,'0');
            if(l3>l2)b.insert(0,l3-l2,'0');//少的位数补0
            //cout<<"---test----"<<endl<<a<<endl<<b<<endl<<"----end----"<<endl;
            //从最低位起计算
            for(i=l3-1;i>=0;i--){
                ans=0;//ans储存答案(0~9)
                ans+=jinwei;//jinwei储存进位(0~1)
                jinwei=0;//重置进位 
                ans+=a[i]-'0';
                ans+=b[i]-'0';
                if(ans>=10){
                    ans-=10;
                    jinwei=1;
                }
                c.insert(0,1,ans+'0');//使用insert在第0个插入1个ans+'0' 
            }
            if(jinwei){
                c.insert(0,1,'1');
            }//最后一次进位
            Bigint rt(c);
            return rt;
        }
        Bigint minus(Bigint i1,Bigint i2){//-
            if(i1<i2){
                return -minus(i2,i1);
            }      
            string a=i1.num,b=i2.num,c="";
            int ans,jiewei=0;//储存减法结果和借位
            int l1=a.size(),l2=b.size();
            int i,j,k;
            int l3=max(l1,l2);
            if(l3>l1)a.insert(0,l3-l1,'0');
            if(l3>l2)b.insert(0,l3-l2,'0');//少的位数补0
            //cout<<a<<b<<endl;
            for(i=l3-1;i>=0;i--){
                ans=0;
                ans-=jiewei;//借位
                jiewei=0;//重置借位
                ans+=a[i]-'0';
                ans-=b[i]-'0';
                //cout<<ans<<endl;
                if(ans<0){
                    ans+=10;
                    jiewei=1;
                }
                c.insert(0,1,ans+'0');
            }
            Bigint rt(c);
            return rt;

        }
        Bigint mul10(int n){//×10^n
            string a=num;
            if(n==0)return Bigint(a);
            a.insert(a.size(),n,'0');
            return Bigint(a);
        }
        Bigint _mul(Bigint i1,int factor){
            string a=i1.num,c="";
            int l=a.size(),jinwei=0,i,ans;
            for(i=l-1;i>=0;i--){
                ans=0;
                ans+=jinwei;
                jinwei=0;
                ans+=(a[i]-'0')*factor;
                jinwei=ans/10;
                ans=ans%10;
                c.insert(0,1,ans+'0');
            }
            c.insert(0,1,jinwei+'0');
            return Bigint(c);
        }
        Bigint mul(Bigint i1,Bigint i2){

            string a=i1.num,b=i2.num,c="";
            Bigint rt("0");
            int l1=a.size(),l2=b.size(),i,j;
            for(i=l2-1,j=0;i>=0;i--,j++){
                rt = rt+(_mul(i1,b[i]-'0').mul10(j));
            }
            return rt;
        }
        Bigint div(Bigint i1,Bigint i2){
            if(i2==Bigint("0")){
                cerr<<"Divided by zero!"<<endl;
                throw 0;
            }
            Bigint rt("0");
            int i,j,k;
            string a=i1.num,b=i2.num;
			int l1=a.size(),l2=b.size();
            for(i=l1;i>=0;i--){
                while(i1>=i2.mul10(i)){
                    i1 = i1-i2.mul10(i);
                    rt = rt + Bigint("1").mul10(i);
                }
            }
            return rt;	
        }
        Bigint mod(Bigint i1,Bigint i2){
             if(i2==Bigint("0")){
                cerr<<"Divided by zero!"<<endl;
                throw 0;
            }
            Bigint rt("0");
            int i,j,k;
            string a=i1.num,b=i2.num;
			int l1=a.size(),l2=b.size();
            for(i=l1;i>=0;i--){
                while(i1>=i2.mul10(i)){
                    i1 = i1-i2.mul10(i);
                    rt = rt + Bigint("1").mul10(i);
                }
            }
            return i1;
        }
};

ostream &operator<<(ostream &os,const Bigint &i){
    os<<i.num;
    return os;
}
istream &operator>>(istream &is,Bigint &i){
    is>>i.num;
    if(is);
    else i=Bigint();
    return is;  
}

int main(){
    string a,b;
    cin>>a>>b;
    cout<<Bigint(a)*Bigint(b);
}


回复

上一页1 页 / 共 1下一页
SGSMix梨SGSMix梨

用.h,is和os的恶心党

点赞0


评论


lsk666666lsk666666

java狗路过……

想起了BigDecimal类……

emotion_编程猫_点赞

点赞1


评论


蒟蒻OIer1048576蒟蒻OIer1048576

让我想起了我在python的无穷精度浮点书

点赞1


评论