九周任务3

#include<iostream>            
using namespace std;     
    
int gcd(int x,int y) ;      
    
class CFraction          
    
{          
    
private:          
    
    int nume; // 分子            
    
    int deno; // 分母            
    
public:          
    
    CFraction(int nu=0,int de=1); //构造函数,初始化用            
    //void simplify();  
    //void output(int style=0); 
    void Set(int nu=0,int de=1); //置值,改变值时用        
    //计算运算符重载
    CFraction operator+(CFraction &t);       
    CFraction operator-(CFraction &t);      
    CFraction operator*(CFraction &t);       
    CFraction operator/(CFraction &t);         
    friend CFraction operator-(CFraction &t);//取反        
    //比较运算符重载
    bool operator>(CFraction &t);        
    bool operator<(CFraction &t);       
    bool operator>=(CFraction &t);        
    bool operator<=(CFraction &t);       
    bool operator==(CFraction &t);       
    bool operator!=(CFraction &t);        
    void Simplify(int n);            //化简(使分子分母没有公因子) 
	//输入输出运算符重载
    friend istream & operator >>(istream &in,CFraction &t);        
    friend ostream & operator <<(ostream &out,CFraction &t);    
    
};          
    
    
CFraction::CFraction(int nu,int de)          
{          
    nume = nu;          
    deno = de;          
    
}          
// void CFraction::simplify() //化简分数    
//{   
 //   int n=gcd(deno, nume);    
 //   deno/=n;     // 化简      
//    nume/=n;    
//}    

//void CFraction::output(int style)  
//{  
 //   int n;  
  //  switch(style)  
  //  {  
  //  case 0:  
   //     cout<<nume<<'/'<<deno<<endl;break;  
  //  case 1:  
   //     n=gcd(nume,deno);  
   //     cout<<nume/n<<'/'<<deno/n<<endl;break;  
  //  case 2:  
   //     n=gcd(nume,deno);  
   //     nume=nume/n;  
   //     deno=deno/n;  
  //      cout<<nume/deno<<'('<<nume%deno<<'/'<<deno<<')'<<endl;break;  
   // default:cout<<nume<<'/'<<deno<<endl;  
   // }  
//}  

void CFraction::Set(int nu,int de) //置值,改变值时用            
{          
    if(de !=0)        
    {        
        nume = nu;        
        deno = de;        
    }        
    else        
    {        
        cout<<"分母不能为零"<<endl;        
        exit(0);        
    }        
    
    nume = nu;           
    deno = de;          
    
}          
    
void CFraction::Simplify(int n)        //化简(使分子分母没有公因子)            
{          
    n = gcd(nume,deno);        
    
    nume = nume/n;        
    deno = deno/n;        
    
}          
    
int gcd(int x,int y)  //求公约数的函数        
{        
    int r;        
    while( y!= 0)        
    {        
        r = x%y;        
        x = y;        
        y = r;        
    }        
    return x;        
}        
    
    
CFraction CFraction:: operator+(CFraction &t)      
{      
    CFraction c;
	c.deno=deno*t.deno;
	c.nume=nume*t.deno+t.nume*deno;
    return c;        
}     
    
CFraction CFraction:: operator-(CFraction &t)      
{      
    CFraction c;        
    c.deno=deno*t.deno;
	c.nume=nume*t.deno-t.nume*deno;
    return c;        
}     
    
CFraction  CFraction:: operator*(CFraction &t)      
{      
    CFraction cf;      
    cf.deno = deno*t.deno;      
    cf.nume = nume*t.nume;      
    return cf;      
}     
    
CFraction  CFraction:: operator/(CFraction &t)      
{      
    CFraction cf;      
    cf.deno = deno*t.nume;      
    cf.nume = nume*t.deno;      
    return cf;      
}     
    
CFraction operator-(CFraction &t)//取反          
{      
    CFraction cf;      
    cf.deno  = -t.deno;      
    cf.nume  = t.nume;      
    return(cf);      
}      
    
bool CFraction::operator>(CFraction &t)      
{      
    if(nume*t.deno>t.nume*deno)      
        return true;      
    else      
        return false;      
}     
    
bool CFraction::operator<(CFraction &t)      
{      
    if(nume*t.deno<t.nume*deno)      
        return true;      
    else      
        return false;      
}     
    
bool CFraction::operator>=(CFraction &t)      
{      
    if(nume*t.deno>t.nume*deno||nume*t.deno==t.nume*deno)      
        return true;      
    else      
        return false;      
}    
    
bool CFraction::operator<=(CFraction &t)       
{      
    if(nume*t.deno<t.nume*deno||nume*t.deno==t.nume*deno)      
        return true;      
    else      
        return false;      
}     
    
bool CFraction::operator==(CFraction &t)      
{      
    if(nume*t.deno==t.nume*deno)      
        return true;      
    else      
        return false;      
}     
    
bool CFraction::operator!=(CFraction &t)      
{      
    if(!operator==(t))      
        return true;      
    else      
        return false;      
}     
    
istream & operator >>(istream &in,CFraction &t)    
{    
    char a;    
    while(1)    
    {    
        cout<<"请输入分数(形式 x/x):"<<endl;    
        cin>>t.nume>>a>>t.deno;    
        if(a != '/'||t.deno ==0)//注意符号不对和分母为0的情况      
        {    
            cout<<"格式不正确,请重新输入:"<<endl;    
            continue;    
        }    
        else break;    
    }    
    return cin;    
}    
    
ostream & operator <<(ostream &out,CFraction &t)//在输出时进行化简      
{    
    if(t.nume ==0)    
    {    
        cout<<"0"<<endl;    
    }    
    else    
    {    
        int m = gcd(t.nume,t.deno);        
        cout<<(t.nume/m)<<'/'<<(t.deno/m)<<endl;        
    }    
    return out;    
}    
    
    
int main ()          
{      
	// CFraction c1(9,4),c2(7,6),c3;
    CFraction c1,c2,c3; 
	cout<<"请输入第一个分数。"<<endl;
    cin>>c1;    
    cout<<"c1 = "<<c1; 
	cout<<"请输入第二个分数。"<<endl;
    cin>>c2;     
     
    c3=c1*c2;  
    cout<<"c1*c2=";  
    //c3.output(); 
	cout<<c3;
    //cout<<"约分后:";  
    //c3.output(1);  
    //cout<<"带分数形式:";  
    //c3.output(2);  
    c3=c1-c2;  
    cout<<"c1-c2=";  
    //c3.output(1);
	cout<<c3;
    c3=c1+c2;  
    cout<<"c1+c2=";  
    //c3.output(1);
	cout<<c3;
    c3=c1/c2;  
    cout<<"c1/c2=";  
    //c3.output(1);
	cout<<c3;
    c3=-c1;  
	cout<<" -c1;为:";
    //c3.output(1);  
	cout<<c3;
    if (c1>c2) cout<<"c1>c2"<<endl;  
    if (c1<c2) cout<<"c1<c2"<<endl;  
    if (c1==c2) cout<<"c1=c2"<<endl;   
    if (c1!=c2) cout<<"c1≠c2"<<endl;  
    if (c1>=c2) cout<<"c1≥c2"<<endl;  
    if (c1<=c2) cout<<"c1≤c2"<<endl;  
    system("pause");  
    return 0;  
  
  
}  

         


运行结果:
 
 
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值