Code


struct BigInteger
{
    int len, negative;
    int val[1005];
    /*      转换      */
    BigInteger(int x = 0)
    {
        I_TO_B(x);
    }
    BigInteger(char *st)
    {
        C_TO_B(st);
    }
    void C_TO_B(char *st)
    {
    	negative = 0;
        len = strlen(st);
        memset(val, 0, sizeof(val));
        rep(i, 0, len - 1) val[i] = st[len - i - 1] - '0';
        if (st[0] == '-') val[--len] = 0, negative = 1;
        while(len > 0 && val[len - 1] == 0) len--;
    }
    void I_TO_B(int x)
    {
    	negative = 0;
        if (x < 0) x = -x, negative = 1;
        len = 0;
        memset(val, 0, sizeof(val));
        while(x)
        {
            val[len++] = x % 10;
            x /= 10;
        }
    }
    /*      输入输出      */
    friend istream &operator >>(istream &in, BigInteger &t)
    {
        char st[1005];
        in >> st;
        t.C_TO_B(st);
        return in;
    }
    friend ostream &operator <<(ostream &out, BigInteger &t)
    {
        if (t.negative) putchar('-');
        per(i, t.len - 1, 0)
            out << t.val[i];
        return out;
    }
    void print()
    {
        if (negative) putchar('-');
        per(i, len - 1, 0)
            putchar(val[i] + '0');
        putchar('\n');
    }
    /*      其他      */
    friend void swap(BigInteger &x, BigInteger &y)
    {
        BigInteger t = x;
        x = y;
        y = t;
    }
    friend BigInteger operator -(BigInteger x)
    {
        BigInteger t = x;
        t.negative = 1 - t.negative;
        return t;
    }
    friend BigInteger abs(BigInteger x)
    {
        BigInteger t = x;
        t.negative = 0;
        return t;
    }
    /*      等于      or      不等于      */
    friend bool operator ==(BigInteger x, BigInteger y)
    {
        if (x.negative != y.negative) return 0;
        if (x.len != y.len) return 0;
        per(i, x.len - 1, 0)
            if (x.val[i] != y.val[i]) return 0;
        return 1;
    }
    friend bool operator ==(BigInteger x, int t)
    {
        BigInteger y;
        y.I_TO_B(t);
        return x == y;
    }
    friend bool operator ==(int t, BigInteger y)
    {
        BigInteger x;
        x.I_TO_B(t);
        return x == y;
    }
    friend bool operator !=(BigInteger x, BigInteger y)
    {
        return !(x == y);
    }
    friend bool operator !=(BigInteger x, int t)
    {
        BigInteger y;
        y.I_TO_B(t);
        return !(x == y);
    }
    friend bool operator !=(const int t, const BigInteger y)
    {
        BigInteger x;
        x.I_TO_B(t);
        return !(x == y);
    }
    /*      小于      or      大于      */
    friend bool operator <(BigInteger x, BigInteger y)
    {
        if (x.negative && !y.negative) return 1;
        if (!x.negative && y.negative) return 0;
        if (x.negative && y.negative) swap(x, y);
        if (x.len < y.len) return 1;
        if (x.len > y.len) return 0;
        per(i, x.len - 1, 0)
            if (x.val[i] < y.val[i]) return 1;
            else if (x.val[i] > y.val[i]) return 0;
        return 0;
    }
    friend bool operator <(BigInteger x, int t)
    {
        BigInteger y;
        y.I_TO_B(t);
        return x < y;
    }
    friend bool operator <(int t, BigInteger y)
    {
        BigInteger x;
        x.I_TO_B(t);
        return x < y;
    }
    friend bool operator >(BigInteger x, BigInteger y)
    {
        return y < x;
    }
    friend bool operator >(BigInteger x, int y)
    {
        return y < x;
    }
    friend bool operator >(int x, BigInteger y)
    {
        return  y < x;
    }
    /*      小于等于      or      大于等于      */
    friend bool operator <=(BigInteger x, BigInteger y)
    {
        return x < y || x == y;
    }
    friend bool operator <=(BigInteger x, int t)
    {
        BigInteger y;
        y.I_TO_B(t);
        return x < y || x == y;
    }
    friend bool operator <=(int t, BigInteger y)
    {
        BigInteger x;
        x.I_TO_B(t);
        return x < y || x == y;
    }
    friend bool operator >=(BigInteger x, BigInteger y)
    {
        return y <= x;
    }
    friend bool operator >=(BigInteger x, int y)
    {
        return y <= x;
    }
    friend bool operator >=(int x, BigInteger y)
    {
        return  y <= x;
    }
    /*      加法      */
    friend BigInteger operator +(BigInteger x, BigInteger y)
    {
        if (x.negative && y.negative) return -(abs(x) + abs(y));
        if (x.negative && !y.negative) return y - abs(x);
        if (!x.negative && y.negative) return x - abs(y);
        int len = x.len > y.len ? x.len : y.len;
        BigInteger ans;
        rep(i, 0, len - 1)
        {
            ans.val[i] += x.val[i] + y.val[i];
            ans.val[i + 1] += ans.val[i] / 10;
            ans.val[i] %= 10;
        }
        if (ans.val[len] != 0) len++;
        ans.len = len;
        return ans;
    }
    friend BigInteger operator +(BigInteger x, int t)
    {
        BigInteger y;
        y.I_TO_B(t);
        return x + y;
    }
    friend void operator +=(BigInteger &x, BigInteger y)
    {
        x = x + y;
    }
    friend void operator +=(BigInteger &x, int t)
    {
        x = x + t;
    }
    friend void operator ++(BigInteger &x)
    {
    	x += 1;
	}
    /*      减法      */
    friend BigInteger operator -(BigInteger x, BigInteger y)
    {
        if (x.negative && y.negative) return abs(y) - abs(x);
        if (x.negative && !y.negative) return -(abs(x) + y);
        if (!x.negative && y.negative) return x + abs(y);
        BigInteger ans;
        if (x < y)
        {
            swap(x, y);
            ans.negative = 1;
        }
        int len = max(x.len, y.len);
        rep(i, 0, len - 1)
        {
            ans.val[i] += x.val[i] - y.val[i];
            if (ans.val[i] < 0)
            {
                ans.val[i] += 10;
                ans.val[i + 1]--;
            }
        }
        while(len > 1 && ans.val[len - 1] == 0) len--;
        ans.len = len;
        return ans;
    }
    friend BigInteger operator -(int t, BigInteger y)
    {
        BigInteger x;
        x.I_TO_B(t);
        return x - y;
    }
    friend BigInteger operator -(BigInteger x, int t)
    {
        BigInteger y;
        y.I_TO_B(t);
        return x - y;
    }
    friend void operator -=(BigInteger &x, BigInteger y)
    {
        x = x - y;
    }
    friend void operator -=(BigInteger &x, int y)
    {
        x = x - y;
    }
    friend void operator --(BigInteger &x)
    {
    	x -= 1;
	}
    /*      乘法      */

    friend BigInteger operator *(BigInteger x, BigInteger y)
    {
        BigInteger ans;
        ans.negative = (x.negative ^ y.negative);
        int len = x.len + y.len;
        rep(i, 0, x.len - 1)
            rep(j, 0, y.len - 1)
                ans.val[i + j] += x.val[i] * y.val[j];
        rep(i, 0, len - 1)
        {
            ans.val[i + 1] += ans.val[i] / 10;
            ans.val[i] %= 10;
        }
        while(len > 1 && ans.val[len - 1] == 0) len--;
        ans.len = len;
        return ans;
    }
    friend BigInteger operator *(BigInteger x, int t)
    {
        BigInteger y;
        y.I_TO_B(t);
        return x * y;
    }
    friend BigInteger operator *(int t, BigInteger y)
    {
        BigInteger x;
        x.I_TO_B(t);
        return x * y;
    }
    friend void operator *=(BigInteger &x, BigInteger y)
    {
        x = x * y;
    }
    friend void operator *=(BigInteger &x, int t)
    {
        x = x * t;
    }
    /*      除法      */
	friend BigInteger operator /(BigInteger x, BigInteger y) 
	{
		BigInteger R, ans;
		if (y.len == 1 && y.val[0] == 0) return R;
		per(i, x.len - 1, 0)
		{
			ans *= 10;
			R = R * 10 + x.val[i];
			while(R >= y)
			{
				++ans;
				R -= y;
			}
		}
		return ans;
	}
	friend BigInteger operator /(BigInteger x, int t)
	{
		BigInteger y;
		y.I_TO_B(t);
		return x / y;
	}
	friend BigInteger operator %(BigInteger x, BigInteger y)
	{
		return x - x / y;
	}
	friend BigInteger operator %(BigInteger x, int t)
	{
		return x - x / t;
	}
	friend void operator /=(BigInteger &x, BigInteger y)
	{
		x = x / y;
	}
	friend void operator /=(BigInteger &x, int t)
	{
		x = x / t;
	}
	friend void operator %=(BigInteger &x, BigInteger y)
	{
		x = x % y;
	}
	friend void operator %=(BigInteger &x, int t)
	{
		x = x % t;
	}
};
posted on 2017-09-28 09:00  WizardCowboy  阅读(133)  评论(0编辑  收藏  举报