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

      

  • 相关阅读:
    拨号进入防盗界面
    手机开机或启动广播接收者
    time、datetime
    py 包和模块,软件开发目录规范
    递归函数
    匿名函数,内置函数
    三元表达式,列表生成式,生成器生成式
    迭代器,生成器
    XPath
    闭包,装饰器
  • 原文地址:https://www.cnblogs.com/uid001/p/11544492.html
Copyright © 2011-2022 走看看