重载运算符

2018-01-04 13:11:53


#include<iostream>
#include<cstdio>
#include<cstring>
#define N 2002
using namespace std;
struct Bignum{
    int len,s[N];
    bool f;//1正,0负 
    Bignum() {
        len = 1;
        memset(s,0,sizeof(s));
        f = 1;
    }
    // =
    Bignum operator = (const char * num) {
        len = strlen(num);
        if(num[0] == '-') {
            f = 0;
            for(int i=1;i<len;i++)
              s[len-i] = s[i] - 48;
            len--;
        }
        else {
            for(int i=0;i<len;i++)
              s[len-i] = num[i] - 48;
        }
        return * this;
    }
    Bignum operator = (const int num) {
        char a[N];
        sprintf(a,"%d",num);
        * this = a;
        return * this;
    }
    // 初始化
    Bignum(const char * num) {
        * this = num;
    }
    Bignum(const int num) {
        * this = num;
    }
    // 逻辑运算符
    bool operator < (const Bignum & x) const {
        if(len != x.len) return len < x.len;
        for(int i=len;i>0;i--)
          if(s[i] != x.s[i])
            return s[i] < x.s[i];
        return false;
    }
    bool operator > (const Bignum & x) const {return x < * this;}
    bool operator <= (const Bignum & x) const {return !(x < * this);}
    bool operator >= (const Bignum & x) const {return !(* this < x);}
    bool operator == (const Bignum & x) const {return !(* this < x or x < * this);}
    bool operator != (const Bignum & x) const {return * this < x or x < * this;}
    // + and +=
    Bignum operator + (const Bignum & x) {
        Bignum ans,xx = x;
        if(f == 1 and xx.f == 0) {//正数加负数等于正数减去正数 
            f = 1;
            xx.f = 1;
            ans = * this - xx;
            return ans;
        }
        if(f == 0 and xx.f == 0) {//负数加负数等于负的两数绝对值之和 
            f = 1;
            xx.f = 1;
            ans = * this + xx;
            return ans;
        }
        if(f == 0 and xx.f == 1) {//负数加正数等于后面的数减去前面的数的绝对值 
            f = 1;
            xx.f = 1;
            ans = xx - * this;
            return ans;
        }
        ans.len = max(len,x.len) + 1;
        int k = 0;
        for(int i=1;i<=ans.len;i++) {
            ans.s[i] = s[i] + x.s[i] + k;
            k = ans.s[i] / 10;
            ans.s[i] %= 10;
        }
        if(ans.s[ans.len] == 0)
          ans.len--;
        return ans;
    }
    Bignum operator += (const Bignum & x) {
        * this = * this + x;
        return * this;
    }
    // - and -=
    Bignum operator - (const Bignum & x) {
        Bignum ans,xx = x;
        ans.len = 1;
        if(f == 1 and xx.f == 1) {//两个正数的差 
            if(* this < x) {
                ans.f = 0;
                while(ans.len <= len or ans.len <= xx.len) {
                    if(xx.s[ans.len] < s[ans.len]) {
                        xx.s[ans.len] += 10;
                        xx.s[ans.len+1]--;
                    }
                    ans.s[ans.len] = xx.s[ans.len] - s[ans.len];
                    ans.len++;
                }
            }
            if(* this == x)
              return ans;
            if(* this > x) {
                ans.f = 1;
                while(ans.len <= len or ans.len <= xx.len) {
                    if(s[ans.len] < xx.s[ans.len]) {
                        s[ans.len] += 10;
                        s[ans.len+1]--;
                    }
                    ans.s[ans.len] = s[ans.len] - xx.s[ans.len];
                    ans.len++;
                }
            }
            while(ans.len > 0 and ans.s[ans.len] == 0)
              ans.len--;
            return ans;
        }
        else if(f == 1 and xx.f == 0) {//被减数为正,减数为负,相当于两数绝对值之和 
            ans.f = 1;
            xx.f = 1;
            ans = * this + xx;
            return ans;
        }
        else if(f == 0 and xx.f == 1) {//被减数为负,减数为正,相当于负的两数绝对值之和 
            ans.f = 0;
            f = 1;
            ans = * this + xx;
            return ans;
        }
        else if(f == 0 and xx.f == 0) {//被减数为负,减数为负,相当于负的减数减去被减数 
            f = 1;
            xx.f = 1;
            ans = xx - * this;
            return ans;
        }
    }
    Bignum operator -= (const Bignum & x) {
        * this = * this - x;
        return * this;
    }
    // * and *=
    Bignum operator * (const Bignum & x) {
        Bignum ans,xx = x;
        if((f == 1 and xx.f == 1) or (f == 0 and xx.f == 0))//同号为正 
          ans.f = 1;
        else ans.f = 0;//异号为负 
        for(int i=1;i<=len;i++) {
            int x = 0;
            for(int j=1;j<=xx.len;j++) {
                ans.s[i+j-1] += s[i] * xx.s[j] + x;
                x = ans.s[i+j-1] / 10;
                ans.s[i+j-1] %= 10;
            }
            ans.s[i+xx.len] = x;
        }
        ans.len = len + xx.len;
        while(ans.s[ans.len] == 0 and ans.len > 1) 
          ans.len--;
        return ans;
    }
    Bignum operator *= (const Bignum & x) {
        * this = * this * x;
        return * this;
    }
    // / and /=
    Bignum numcpy(Bignum & x,Bignum & y,int det) {
        for(int i=1;i<=x.len;i++)
          y.s[det+i-1] = x.s[i];
        y.len = x.len + det - 1;
    }
    Bignum operator / (const Bignum & x) {
        Bignum ans,temp,xx = x;
        if((* this == temp) or (* this < xx))//不够除或是被除数为0,直接返回0 
          return temp;
        else {
            if((f == 1 and xx.f == 1) or (f == 0 and xx.f == 0))//同号为正 
                ans.f = 1;
            else ans.f = 0;//异号为负 
            xx.f = 1;//极为重要,被这个卡了一下午,下面的减法是最简单的减法,所以要把两个数都变成正数 
            f = 1;//同上
            ans.len = len - xx.len + 1;
            for(int i=ans.len;i>0;i--) {
                memset(temp.s,0,sizeof(temp.s));
                numcpy(xx,temp,i);
                while(* this >= temp) {
                    * this -= temp;
                    ans.s[i]++;
                }
            }
            while(ans.len > 0 and ans.s[ans.len] == 0)
              ans.len--;
            return ans;
        }
    }
    Bignum operator /= (const Bignum & x) {
        * this = * this / x;
        return * this;
    }
    // over
};
ostream & operator << (ostream & out,const Bignum & x) {
    if(x.f == 0) cout<<"-";
    for(int i=x.len;i>0;i--)
      cout<<x.s[i];
    return out;
}
istream & operator >> (istream & in,Bignum & x) {
    char num[N];
    in>>num;
    x = num;
    return in;
}
int main()
{
    Bignum x,y;
    cin>>x>>y;
    x *= y;
    cout<<x;
    return 0;
}