其他分享
首页 > 其他分享> > 大数相乘

大数相乘

作者:互联网

#include <iostream>
#include <string>
using namespace std;

/* 请在这里填写答案 */
class BigInteger{
    protected:
    int a[3005];
    int count;
    public:
    BigInteger(){
        for(int i=0;i<3005;i++){
            a[i]=0;
        }
        count=0;
    }
    friend BigInteger operator *(BigInteger &a,BigInteger &b){
        BigInteger c;
        int k=a.count+b.count;
        for(int i=a.count-1;i>=0;i--){
            for(int j=b.count-1;j>=0;j--){
                c.a[i+j+1]+=a.a[i]*b.a[j];
                if(c.a[i+j+1]>9){
                    c.a[i+j]+=c.a[i+j+1]/10;
                    c.a[i+j+1]%=10;
                }
            }
        }
        c.count=k;
        return c;
    }
    friend istream& operator >> (istream &in,BigInteger &s){
        string s1;
        in>>s1;
        for(int i=0;i<s1.size();i++){
            s.a[i]=s1[i]-'0';
            s.count++;
        }
        return in;
    }
    friend ostream& operator << (ostream &o,BigInteger &s){
        int flag=1;
        for(int i=0;i<s.count;i++){
            if(s.a[i]!=0){
                flag=0;
            }
            if(flag==0){
                o<<s.a[i];
            }
        }
        if(flag==1){
            o<<"0";
        }
        return o;
    }
};

int main() 
{
    BigInteger a, b, c;
    cin >> a >> b;
    c = a * b;
    cout << a << "*" << b << "=" << c << endl;
    return 0;
}


标签:count,BigInteger,大数,int,s1,相乘,istream,include
来源: https://www.cnblogs.com/Xuuxxi/p/14929816.html