Skip to content

Instantly share code, notes, and snippets.

@StarOrpheus
Created February 1, 2017 09:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save StarOrpheus/ce2a6b5c92028673c86207544fe3dcfd to your computer and use it in GitHub Desktop.
Save StarOrpheus/ce2a6b5c92028673c86207544fe3dcfd to your computer and use it in GitHub Desktop.
ll gcd(ll x, ll y)
{
while(x > 0 && y > 0)
{
if (x > y)
x = x % y;
else
y = y % x;
}
return x + y;
}
struct fract
{
ll x, y;
bool operator<(const fract& to) const
{
if (x != to.x)
return x < to.x;
return y < to.y;
}
bool operator==(fract& to)
{
return x == to.x && y == to.y;
}
bool operator==(fract to)
{
return x == to.x && y == to.y;
}
bool operator==(fract to) const
{
return x == to.x && y == to.y;
}
fract operator+(fract to)
{
fract nx = *this, ny = to;
ll comm = y / gcd(y, to.y) * to.y;
nx.x *= comm / nx.y;
ny.x *= comm / ny.y;
return fract(nx.x + ny.x, comm);
}
fract operator/(int x)
{
fract res = *this;
res.y *= x;
ll k = gcd(res.x, res.y);
res.x /= k;
res.y /= k;
return res;
}
fract(ll x, ll y) : x(x), y(y)
{
ll k = gcd(x, y);
x /= k;
y /= k;
}
fract() : x(0), y(1) {}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment