码迷,mamicode.com
首页 > 其他好文 > 详细

手写分数类

时间:2019-09-18 19:27:45      阅读:100      评论:0      收藏:0      [点我收藏+]

标签:row   sign   this   cpp   his   struct   size   else   void   

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;
	}
};

  

手写分数类

标签:row   sign   this   cpp   his   struct   size   else   void   

原文地址:https://www.cnblogs.com/uid001/p/11544492.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!