跳到正文

剪贴板

剪贴板 e0mt3svd

原文
剪贴板信息

剪贴板信息

收录
更新

分数

C++
#define cT const T&
#define cfrac const frac&
template <class T, unsigned char jd> struct frac
{
	T num1,num2 = 1;
	T gcd(cT a, cT b)
	{
		return (b ? gcd(b,a%b) : a);
	}
	void yf()
	{
		T t = gcd(num1,num2);
    	num1 /= t, num2 /= t;
	}
	void print()
	{
		cout << num1;
		if (num2 != 1) cout << "/" << num2 << " ";
	}
	frac() {num1 = 0, num2 = 1;}
	frac(cT num)
	{
		num1 = num,num2 = 1;
	}
	frac(cT num1_, cT num2_)
	{
		num1 = num1_,num2 = num2_;
	}
	frac(const initializer_list<T>& num)
	{
		num1 = *num.begin(), num2 = *next(num.begin(),1);
	}
	operator double()
	{
		return ((double)(num1)) / num2;
	}
	operator bool()
	{
		return num1;
	}
	frac& operator = (cT num)
	{
		num1 = num, num2 = 1;
		return *this;
	}
	frac& operator = (const double& num) //*7 在T = int时可以保留7~8位小数 (*8会溢出)
	{
		num1 = num * (((T)1) << (sizeof(T) * 7)), num2 = (((T)1) << (sizeof(T) * 7));
		yf(); return *this;
	}
	frac& operator = (const initializer_list<T>& num)
	{
		num1 = *num.begin(), num2 = *next(num.begin(),1);
		return *this;
	}
	frac operator + (cfrac ot) const
	{
		return frac(num1*ot.num2+ot.num1*num2,ot.num2*num2);
	}
	frac operator + (cT ot) const
	{
		return frac(num1+ot*num2,num2);
	}
	friend frac operator + (cT ot, cfrac th)
	{
		return th + ot;
	}
	frac operator - (cfrac ot) const
	{
		return frac(num1*ot.num2-ot.num1*num2,ot.num2*num2);
	}
	frac operator - (cT ot) const
	{
		return frac(num1-ot*num2,num2);
	}
	friend frac operator - (cT ot, cfrac th)
	{
		return frac(ot)-th;
	}
	frac operator * (cfrac ot) const
	{
		return frac(num1*ot.num1,num2*ot.num2);
	}
	frac operator * (cT ot) const
	{
		return frac(num1*ot,num2);
	}
	friend frac operator * (cT ot, cfrac th)
	{
		return th * ot;
	}
	frac operator / (cfrac ot) const
	{
		return frac(num1*ot.num2,num2*ot.num1);
	}
	frac operator / (cT ot) const
	{
		return frac(num1,num2*ot);
	}
	friend frac operator / (cT ot, cfrac th)
	{
		return frac(ot)/th;
	}
	friend frac& operator += (frac& th, cT num)
	{
		return th = th + num;
	}
	friend frac& operator += (frac& th, cfrac ot)
	{
		return th = th + ot;
	}
	friend frac& operator -= (frac& th, cT num)
	{
		return th = th - num;
	}
	friend frac& operator -= (frac& th, cfrac ot)
	{
		return th = th - ot;
	}
	friend frac& operator *= (frac& th, cT num)
	{
		return th = th * num;
	}
	friend frac& operator *= (frac& th, cfrac ot)
	{
		return th = th * ot;
	}
	friend frac& operator /= (frac& th, cT num)
	{
		return th = th / num;
	}
	friend frac& operator /= (frac& th, cfrac ot)
	{
		return th = th / ot;
	}
	friend bool operator == (cfrac th, cfrac ot)
	{
		return th.num1 * ot.num2 == th.num2 * ot.num1;
	}
	friend bool operator == (cfrac th, cT ot)
	{
		return th == frac(ot);
	}
	friend bool operator == (cT th, cfrac ot)
	{
		return frac(th) == ot;
	}
};struct frac <long long,6> a(3,8),b(1,5),c;