struct Frac { ll u, v; Frac() : u(0), v(1) {} Frac(ll x) : u(x), v(1) {} Frac(ll _u, ll _v) { if (!_v) throw; ll g = gcd(abs(_u),abs(_v)); _u /= g, _v /= g; if (_v<0) _u = -_u; u = _u, v = _v; } Frac &operator = (int x) { u = x, v = 1; return *this; } Frac &operator = (ll x) { u = x, v = 1; return *this; } bool operator < (const Frac & b) const {return u*b.v<v*b.u;} bool operator > (const Frac & b) const {return b<*this;} bool operator <= (const Frac & b) const {return !(b<*this);} bool operator >= (const Frac & b) const {return !(*this<b);} bool operator == (const Frac & b) const {return !(*this<b)&&!(b<*this);} bool operator != (const Frac & b) const {return *this<b||b<*this;} Frac operator + (const Frac & b) { ll g = gcd(v,b.v); return Frac(b.v/g*u+v/g*b.u,v/g*b.v); } Frac &operator += (const Frac & b) { return *this=*this+b; } Frac operator - (const Frac & b) { ll g = gcd(v,b.v); return Frac(b.v/g*u-v/g*b.u,v/g*b.v); } Frac &operator -= (const Frac & b) { return *this=*this-b; } Frac operator * (const Frac & b) { return Frac(u*b.u,v*b.v); } Frac &operator *= (const Frac & b) { return *this=*this*b; } Frac operator / (const Frac & b) { return *this*Frac(b.v,b.u); } Frac &operator /= (const Frac & b) { return *this=*this/b; } friend Frac operator - (Frac b) { b.u = -b.u; return b; } friend Frac operator + (Frac b) { return b; } friend ostream& operator << (ostream &stream,const Frac& b) { if (b.v==1) stream<<b.u; else stream<<b.u<<‘/‘<<b.v; return stream; } void read(const string &s) { int sign = 1, pos = 0; while (pos<s.size()&&(s[pos]==‘-‘||s[pos]==‘+‘)) { if (s[pos]==‘-‘) sign = -sign; ++pos; } u = 0; while (pos<s.size()&&‘0‘<=s[pos]&&s[pos]<=‘9‘) { u = u*10+s[pos]-‘0‘; ++pos; } u *= sign, v = 1; if (pos==s.size()) return; ++pos, v = 0; while (pos<s.size()&&‘0‘<=s[pos]&&s[pos]<=‘9‘) { v = v*10+s[pos]-‘0‘; ++pos; } *this = Frac(u,v); } friend istream &operator >> (istream &stream,Frac &b) { string s; stream >> s; b.read(s); return stream; } };
原文地址:https://www.cnblogs.com/uid001/p/11544492.html
时间: 2024-10-14 15:34:26