第九周任务(三)

#include <iostream>  
using namespace std;  
  
class CFraction  
{  
private:  
    int nume;  
    int deno;  
public:  
    CFraction(int nu=0,int de=1):nume(nu),deno(de){}  
    void simplify();   
    void display();  
    CFraction operator +(CFraction &c1);  
    CFraction operator -(CFraction &c1);  
    CFraction operator *(CFraction &c1);  
    CFraction operator /(CFraction &c1);  
    bool operator >(CFraction &c1);  
    bool operator <(CFraction &c1);  
    bool operator ==(CFraction &c1);  
    bool operator !=(CFraction &c1);  
    bool operator >=(CFraction &c1);  
    bool operator <=(CFraction &c1);  
    CFraction operator +();  
    CFraction operator -();  
	friend ostream & operator <<(ostream &,CFraction &);

    friend istream & operator >>(istream &,CFraction &);

};  
void CFraction::simplify()  
{  
    int x;  
    int max;  
    if(nume>deno)  
        x=nume;  
    else   
        x=deno;  
    for(int i=2;i<x;i++)  
        if(nume%i==0&&deno%i==0)  
        {  
            deno=deno/i;  
            nume=nume/i;  
            max=i;  
        }  
}  
  
void CFraction::display()    
{    
    cout<<"("<<nume<<"/"<<deno<<")"<<endl;    
}   
  
CFraction CFraction::operator +(CFraction &c1)  
{  
    CFraction c2;  
  
    c2.deno=deno*c1.deno;  
    c2.nume=nume*c1.deno+c1.nume*deno;  
    c2.simplify();  
  
    return (c2);  
}  
  
CFraction CFraction::operator -(CFraction &c1)  
{  
    CFraction c2;  
  
    c2.deno=deno*c1.deno;  
    c2.nume=nume*c1.deno-c1.nume*deno;  
    c2.simplify();  
  
    return (c2);  
}  
  
CFraction CFraction::operator *(CFraction &c1)  
{  
    CFraction c2;  
  
    c2.deno=deno*c1.deno;  
    c2.nume=nume*c1.nume;  
    c2.simplify();  
  
    return (c2);  
}  
  
CFraction CFraction::operator /(CFraction &c1)  
{  
    CFraction c2;  
  
    c2.deno=deno*c1.nume;  
    c2.nume=nume*c1.deno;  
    c2.simplify();  
  
    return (c2);  
}  
  
bool CFraction::operator >(CFraction &c1)  
{  
    CFraction  c2,c3;  
    c2.nume=nume*c1.deno;  
    c3.nume=c1.nume*deno;  
  
    if(c2.nume>c3.nume)  
        return true;  
    else  
        return false;  
}  
  
bool CFraction::operator <(CFraction &c1)  
{  
    CFraction  c2,c3;  
    c2.nume=nume*c1.deno;  
    c3.nume=c1.nume*deno;  
  
    if(c2.nume<c3.nume)  
        return true;  
    else  
        return false;  
}  
  
bool CFraction::operator ==(CFraction &c1)  
{  
    if( operator >(c1)!=1&& operator <(c1)!=1)  
        return true;  
    else  
        return false;  
}  
  
bool CFraction::operator !=(CFraction &c1)  
{  
    if(operator >(c1)==1|| operator <(c1)==1)  
        return true;  
    else   
        return false;  
}  
  
bool CFraction::operator >=(CFraction &c1)  
{  
    if( operator <(c1)!=1)  
        return true;  
    else   
        return false;  
}  
  
bool CFraction::operator <=(CFraction &c1)  
{  
    if(operator >(c1)!=1)  
        return true;  
    else  
        return false;  
}  
CFraction CFraction::operator +()  
{  
    CFraction c2;  
  
    c2.nume=nume;  
    c2.deno=deno;  
  
    return (c2);  
}  
  
CFraction CFraction::operator -()  
{  
    CFraction c2;  
  
    c2.nume=-nume;  
    c2.deno=deno;  
  
    return (c2);  
} 

 ostream & operator <<(ostream & output,CFraction &c)
 {
	 output<<"("<<c.nume<<"/"<<c.deno<<")"<<endl;

	 return output;
 }

 istream & operator >>(istream & input,CFraction &c)
 {
	 cout<<"请输入分数"<<endl;
	 input>>c.nume>>c.deno;

	 return input;
 }

int main()    
{    
    CFraction x,y,s; 
	cin>>x>>y;
    cout<<"分数x="<<x<<endl;
	cout<<"分数y="<<y<<endl;    
    s=+x+y;    
    cout<<"+x+y="<<s;    
        
    s=x-y;    
    cout<<"x-y="<<s;    
      
    s=x*y;    
    cout<<"x*y="<<s;    
    
    s=x/y;    
    cout<<"x/y="<<s;    
   
    s=-x+y;    
    cout<<"-x+y="<<s;    
    
    cout<<x;   
    if (x>y) cout<<"大于"<<endl;    
    if (x<y) cout<<"小于"<<endl;    
    if (x==y) cout<<"等于"<<endl;     
    cout<<endl;   
    cout<<y;  
    system("pause");    
    return 0;    
}    

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值