zoukankan      html  css  js  c++  java
  • [乱搞模板] 闲来无事写的分数类

    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

  • 相关阅读:
    Blank page instead of the SharePoint Central Administration site
    BizTalk 2010 BAM Configure
    Use ODBA with Visio 2007
    Handling SOAP Exceptions in BizTalk Orchestrations
    BizTalk与WebMethods之间的EDI交换
    Append messages in BizTalk
    FTP protocol commands
    Using Dynamic Maps in BizTalk(From CodeProject)
    Synchronous To Asynchronous Flows Without An Orchestration的简单实现
    WSE3 and "Action for ultimate recipient is required but not present in the message."
  • 原文地址:https://www.cnblogs.com/Rain142857/p/11688628.html
Copyright © 2011-2022 走看看