• [乱搞模板] 闲来无事写的分数类


    QWQ。。

    自动拥有构造函数,顺序是(分子,分母)。

    调用直接用,支持运算符> < >= <= + - *其他的实在是不想写辽。。

    将就着看吧

    注意:分子分母必须是int型!!

    #include<cstdio>
    using namespace std;
    inline int read()
    {
    	int s=0;bool f=1;char ch=getchar();
    	while(ch<'0'||ch>'9'){if(ch=='-')f=0;ch=getchar();}
    	while(ch>='0'&&ch<='9'){s=(s<<3)+(s<<1)+ch-'0';ch=getchar();}
    	return f?s:-s;
    }
    int maxf(int x,int y){return x>y?x:y;}
    int minf(int x,int y){return x<y?x:y;}
    int gcd(int x,int y)
    {
    	if(x==y)return x;
    	int r;
    	do{r=x%y;x=y;y=r;}while(r);
    	return x;
    }
    int lcm(int x,int y)
    {
    	return x*y/gcd(x,y);
    }
    struct Fraction
    {
    	int up,dn;
    	Fraction(int x,int y)
    	{
    		up=x;dn=y;
    	}
    	friend bool operator < (Fraction a,Fraction b)
    	{
    		int Gl=lcm(a.dn,b.dn);
    		int Al=a.up*(Gl/a.dn),Bl=b.up*(Gl/b.dn);
    		return Al<Bl;
    	}
    	friend bool operator <= (Fraction a,Fraction b)
    	{
    		int Gl=lcm(a.dn,b.dn);
    		int Al=a.up*(Gl/a.dn),Bl=b.up*(Gl/b.dn);
    		return Al<=Bl;
    	}
    	friend bool operator > (Fraction a,Fraction b)
    	{
    		int Gl=lcm(a.dn,b.dn);
    		int Al=a.up*(Gl/a.dn),Bl=b.up*(Gl/b.dn);
    		return Al>Bl;
    	}
    	friend bool operator >= (Fraction a,Fraction b)
    	{
    		int Gl=lcm(a.dn,b.dn);
    		int Al=a.up*(Gl/a.dn),Bl=b.up*(Gl/b.dn);
    		return Al>=Bl;
    	}
    };
    Fraction operator + (Fraction a,Fraction b)
    {
    	Fraction c(0,0);
    	int Gl=lcm(a.dn,b.dn);
    	c.dn=Gl;
    	c.up=a.up*(Gl/a.dn)+b.up*(Gl/b.dn);
    	if(c.up&&c.dn)
    	{
    		Gl=gcd(c.up,c.dn);
    		c.up/=Gl;c.dn/=Gl;
    	}
    	return c;
    }
    Fraction operator - (Fraction a,Fraction b)
    {
    	Fraction c(0,0);
    	int Gl=lcm(a.dn,b.dn);
    	c.dn=Gl;
    	c.up=a.up*(Gl/a.dn)-b.up*(Gl/b.dn);
    	if(c.up>1&&c.dn>1)
    	{
    		Gl=gcd(c.up,c.dn);
    		c.up/=Gl;c.dn/=Gl;
    	}
    	return c;
    }
    Fraction operator * (Fraction a,Fraction b)
    {
    	Fraction c(0,0);
    	c.dn=a.dn*b.dn;
    	c.up=a.up*b.up;
    	if(c.up>1&&c.dn>1)
    	{
    		int Gl=gcd(c.up,c.dn);
    		c.up/=Gl;c.dn/=Gl;
    	}
    	return c;
    }
    Fraction bigger(Fraction a,Fraction b){return a>b?a:b;}
    Fraction smaller(Fraction a,Fraction b){return a<b?a:b;}
    int main()
    {
    	Fraction x(0,0),y(0,0),z(0,0);
    	int i=0;
    	while(1)
    	{
    		printf("-------------------
    New round: [%d]
    ",++i);
    		scanf("%d%d%d%d",&x.up,&x.dn,&y.up,&y.dn);
    		printf("x>y? %d
     x<y? %d
    ",x>y?1:0,x<y?1:0);
    		printf("bigger %d/%d smaller %d/%d
    ",bigger(x,y).up,bigger(x,y).dn,smaller(x,y).up,smaller(x,y).dn);
    		z=x+y;
    		printf("x+y=%d/%d ",z.up,z.dn);
    		z=bigger(x,y)-smaller(x,y);
    		printf("|x-y|=%d/%d
    ",z.up,z.dn);
    		z=x*y;
    		printf("x*y=%d/%d
    ",z.up,z.dn);
    	}
    	return 0;
    }
    

    附:利用原代码main函数中的调试内容进行调试:依次输入 第一个分数的分子 第一个分数的分母 第二个分数的分子 第二个分数的分母

    然后的输出你就看得懂了qwq

  • 相关阅读:
    web 安全问题(二):XSS攻击
    web 安全问题(一):CSRF 攻击
    关于阅读源码
    vue 阅读一【待完结】
    【转】服务器添加新用户用ssh-key 登录,并禁用root用户 密码登录
    sass & compass 实战录
    浏览器兼容性总结
    常用的一个cookie 对象,还有path 兼容性问题
    css margin塌陷问题
    markdown 语法简要备忘
  • 原文地址:https://www.cnblogs.com/Rain142857/p/11688628.html
Copyright © 2020-2023  润新知