zoukankan      html  css  js  c++  java
  • 手写分数类

    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, _v = -_v;
    		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;
    	}
    };
    

      

  • 相关阅读:
    oop klass

    广义表
    Huffman树
    二叉搜索树
    二叉树的前序、中序、后序、层序遍历
    循环链表解决约瑟夫环问题
    搭建局域网SVN代码服务器
    【CheckList】精简用例,提升执行效率,减少漏测(总结篇)
    测试资源不同时,如何有针对性的设计测试用例?
  • 原文地址:https://www.cnblogs.com/uid001/p/11544492.html
Copyright © 2011-2022 走看看