分数运算合集

· · 个人记录

分数运算合集,打了一晚上QwQ

这两天打了高精度运算合集(新手篇),和矩阵运算合集,要累死了,太肝了。

欢迎去我的博客看看

#include <bits/stdc++.h>
#define int long long
using namespace std;
int gcd(int a, int b)
{
    return !b ? a : gcd(b, a % b);
}
struct fraction
{
    int p, q;
    fraction()
    {
        clear();
    }
    void clear()
    {
        p = 0, q = 1;
    }
    void reduct()
    {
        if (q < 0)
            p = -p, q = -q;
        int x = gcd(q, abs(p));
        p /= x, q /= x;
    }
    fraction swap()
    {
        fraction y = *this;
        std::swap(y.p, y.q);
        return y;
    }
    void write()
    {
        cout << p;
        if (q != 1)
            cout << '/' << q;
        cout << endl;
    }
    void operator=(fraction x)
    {
        p = x.p, q = x.q;
    }
    void operator=(int x)
    {
        p = x, q = 1;
    }
    bool operator==(fraction x)
    {
        return p == x.p && q == x.q;
    }
    bool operator!=(fraction x)
    {
        return !operator==(x);
    }
    bool operator<(fraction x)
    {
        return p * x.q < q * x.p;
    }
    bool operator>(fraction x)
    {
        return p * x.q > q * x.p;
    }
    bool operator<=(fraction x)
    {
        return !operator>(x);
    }
    bool operator>=(fraction x)
    {
        return !operator<(x);
    }
    bool operator==(int x)
    {
        fraction y;
        y = x;
        return operator==(y);
    }
    bool operator!=(int x)
    {
        fraction y;
        y = x;
        return operator!=(y);
    }
    bool operator<(int x)
    {
        fraction y;
        y = x;
        return operator<(y);
    }
    bool operator>(int x)
    {
        fraction y;
        y = x;
        return operator>(y);
    }
    bool operator<=(int x)
    {
        fraction y;
        y = x;
        return operator<=(y);
    }
    bool operator>=(int x)
    {
        fraction y;
        y = x;
        return operator>=(y);
    }
    bool operator!()
    {
        return p == 0;
    }
    fraction operator+(fraction x)
    {
        fraction y;
        y.p = p * x.q + q * x.p;
        y.q = q * x.q;
        y.reduct();
        return y;
    }
    fraction operator+(int x)
    {
        fraction y;
        y = x;
        return operator+(y);
    }
    fraction operator-()
    {
        fraction y = *this;
        y.p = -y.p;
        return y;
    }
    fraction operator-(fraction x)
    {
        fraction y = -x;
        return operator+(y);
    }
    fraction operator-(int x)
    {
        fraction y;
        y = x;
        return operator-(y);
    }
    fraction operator*(fraction x)
    {
        fraction y;
        y.p = p * x.p;
        y.q = q * x.q;
        y.reduct();
        return y;
    }
    fraction operator*(int x)
    {
        fraction y;
        y = x;
        return operator*(y);
    }
    fraction operator/(fraction x)
    {
        fraction y = x.swap();
        return operator*(y);
    }
    fraction operator/(int x)
    {
        fraction y;
        y = x;
        return operator/(y);
    }
    void operator+=(fraction x)
    {
        fraction y = *this;
        *this = y + x;
    }
    void operator-=(fraction x)
    {
        fraction y = *this;
        *this = y - x;
    }
    void operator*=(fraction x)
    {
        fraction y = *this;
        *this = y * x;
    }
    void operator/=(fraction x)
    {
        fraction y = *this;
        *this = y / x;
    }
    void operator+=(int x)
    {
        fraction y = *this;
        *this = y + x;
    }
    void operator-=(int x)
    {
        fraction y = *this;
        *this = y - x;
    }
    void operator*=(int x)
    {
        fraction y = *this;
        *this = y * x;
    }
    void operator/=(int x)
    {
        fraction y = *this;
        *this = y / x;
    }
} a;
int n;
signed main()
{
    cin >> n;
    while (n--)
    {
        int op, x;
        cin >> op;
        if (op == 0)
        {
            a.write();
            continue;
        }
        cin >> x;
        if (op == 1)
        {
            a += x;
        }
        else if (op == 2)
        {
            a -= x;
        }
        else if (op == 3)
        {
            a *= x;
        }
        else
        {
            a /= x;
        }
    }
    return 0;
}