手写分数类
Posted uid001
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了手写分数类相关的知识,希望对你有一定的参考价值。
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; ;
以上是关于手写分数类的主要内容,如果未能解决你的问题,请参考以下文章