第九周 任务三

#include<iostream>   
using namespace std;  
  
class CFraction  
{  
private:  
    int nume;  // 分子   
    int deno;  // 分母   
public:  
    CFraction(int nu = 1,int de = 2);   //构造函数,初始化用   

    friend istream& operator >> (istream&,CFraction&);//声明重载运算符“>>”函数   
	friend ostream& operator << (ostream&,CFraction&);//声明重载运算符“<<”函数

    void Simplify();                //化简(使分子分母没有公因子)   
    void S_Simplify();              //化简(以4/3为例,输出1(1/3),表示一又三分之一)   
    void output(int style);         //输出:以8/6为例,style为0时,输出8/6;   
                                    //style为1时,输出4/3;   
                                    //style为2时,输出1(1/3),表示一又三分之一;   
                                    //不给出参数和非1、2,认为是方式0   
  
    CFraction operator+(CFraction &CF2); //定义分数加法   
    CFraction operator-(CFraction &CF2); //定义分数减法   
    CFraction operator*(CFraction &CF2); //定义分数乘法   
    CFraction operator/(CFraction &CF2); //定义分数除法   
  
    CFraction operator-();//取反   
  
    //比较大小   
    bool operator > (CFraction &CF2);  
    bool operator < (CFraction &CF2);  
    bool operator >= (CFraction &CF2);  
    bool operator <= (CFraction &CF2);  
    bool operator == (CFraction &CF2);  
    bool operator != (CFraction &CF2);  
};  
  
//构造函数,初始化用   
CFraction::CFraction(int nu,int de)  
{  
    nume = nu;  
    deno = de;  
}  
  
//输出分数   
ostream& operator << (ostream& output,CFraction& CF)
{  
    cout << CF.nume << "/" << CF.deno;
	return output;
}  
  
//按照"nu/de"的格式,如"5/2"的形式输入   
istream& operator >> (istream& input,CFraction& CF)
{  
    char b;  
    cout << "请输入分数(按照“a/b”形式输入):" << endl;  
    input >> CF.nume >> b >> CF.deno;  
	return input;
}  
  
//化简(使分子分母没有公因子)   
void CFraction::Simplify()  
{  
    for(int i = 2;i <= nume && i <= deno;i++)  
    {  
        if(nume % i == 0 && deno % i == 0)  
        {  
            nume = nume/i;  
            deno = deno/i;  
            i--;  
        }  
    }  
  
    cout << (*this) << endl;  
}  
  
//化简(以4/3为例,输出1(1/3),表示一又三分之一)   
void CFraction::S_Simplify()  
{  
    for(int i = 2;i <= nume && i <= deno;i++)  
    {  
        if(nume % i == 0 && deno % i == 0)  
        {  
            nume = nume/i;  
            deno = deno/i;  
            i--;  
        }  
    }  
  
    if(nume >= deno)  
    {  
        int n;  
  
        n = nume / deno;  
        nume = nume % deno;  
        cout << n << "(" << (*this) << ")" << endl;  
    }  
    else  
    {  
        cout << (*this);  
    }  
  
}  
//输出:以8/6为例,style为0时,输出8/6;   
//style为1时,输出4/3;   
//style为2时,输出1(1/3),表示一又三分之一;   
//不给出参数和非1、2,认为是方式0   
void CFraction::output(int style)  
{  
    if(style == 1)  
    {  
        CFraction::Simplify();  
    }  
    else if(style == 2)  
    {  
        CFraction::S_Simplify();  
    }  
    else  
    {  
        cout << (*this) << endl;  
    }  
}  
  
//定义分数加法   
CFraction CFraction::operator+(CFraction &CF2)  
{  
    nume = nume * CF2.deno + CF2.nume * deno;  
    deno = deno * CF2.deno;  
    return (*this);  
}  
  
//定义分数减法   
CFraction CFraction::operator-(CFraction &CF2)  
{  
    nume = nume * CF2.deno - CF2.nume * deno;  
    deno = deno * CF2.deno;  
    return (*this);  
}  
  
//定义分数乘法   
CFraction CFraction::operator*(CFraction &CF2)  
{  
    nume = nume * CF2.nume;  
    deno = deno * CF2.deno;  
    return (*this);  
}  
  
//定义分数除法   
CFraction CFraction::operator/(CFraction &CF2)  
{  
    nume = nume * CF2.deno;  
    deno = deno * CF2.nume;  
    return (*this);  
}  
//取反   
CFraction CFraction::operator-()  
{  
    nume = - nume;  
    deno = deno;  
    return (*this);  
}  
  
//比较大小   
bool CFraction::operator > (CFraction &CF2)     
{    
    nume = nume * CF2.deno;  
    CF2.nume = CF2.nume * deno;  
  
    if( nume  > CF2.nume )    
    {    
        return true;    
    }    
    else    
    {    
        return false;    
    }    
}   
   
bool CFraction::operator < (CFraction &CF2)     
{    
    nume = nume * CF2.deno;  
    CF2.nume = CF2.nume * deno;  
  
    if( nume  < CF2.nume )    
    {    
        return true;    
    }    
    else    
    {    
        return false;    
    }    
}    
  
bool CFraction::operator >= (CFraction &CF2)     
{    
    nume = nume * CF2.deno;  
    CF2.nume = CF2.nume * deno;  
  
    if( nume  >= CF2.nume )   
    {    
        return true;    
    }     
    else    
    {    
        return false;    
    }    
}   
   
bool CFraction::operator <= (CFraction &CF2)     
{    
    nume = nume * CF2.deno;  
    CF2.nume = CF2.nume * deno;  
  
    if( nume  <= CF2.nume )   
    {    
        return true;    
    }    
    else    
    {    
        return false;    
    }    
}    
  
bool CFraction::operator == (CFraction &CF2)     
{    
    nume = nume * CF2.deno;  
    CF2.nume = CF2.nume * deno;  
  
    if( nume  == CF2.nume )   
    {    
        return true;    
    }      
    else    
    {    
        return false;    
    }    
}  
    
bool CFraction::operator != (CFraction &CF2)     
{    
    nume = nume * CF2.deno;  
    CF2.nume = CF2.nume * deno;  
  
    if( nume  != CF2.nume )   
    {    
        return true;    
    }     
    else    
    {    
        return false;    
    }    
}   
   
void main()  
{  
    CFraction CF1, CF2;  
  
    cin >> CF1;           
    CF1.output(1);  
    cin >> CF2;   
    CF2.output(2); 
    cout << endl;  
  
    CFraction CF3(3,4), CF4(1,4),CF5;  
  
    cout << "CF3为:" ;   
    cout << CF3 << endl;  
    cout << "CF4为:" ;   
    cout << CF4 << endl;  
  
    CF5 = CF3;  
    CF5 = CF5 + CF4;  
    cout << "CF3 + CF4 = ";  
    cout << CF5 << endl;  
    cout << "化简后得:";  
    CF5.Simplify();  
  
    CF5 = CF3;  
    CF5 = CF5 - CF4;  
    cout << "CF3 - CF4 = ";  
    cout << CF5 << endl;  
    cout << "化简后得:";  
    CF5.Simplify();  
  
    CF5 = CF3;  
    CF5 = CF5 * CF4;  
    cout << "CF3 * CF4 = ";  
    cout << CF5 << endl;  
    cout << "化简后得:";  
    CF5.Simplify();  
  
    CF5 = CF3;  
    CF5 = CF5 / CF4;  
    cout << "CF3 / CF4 = ";  
    cout << CF5 << endl;  
    cout << "化简后得:";  
    CF5.Simplify(); 
  
    CF5 = CF3;  
    CF5 = - CF5;  
    cout << "-CF3 = ";  
    cout << CF5 << endl;  
  
    cout << "比较两个分数大小:" << endl;        
    if (CF3 > CF4) cout << "CF3 > CF4" << endl;  
    if (CF3 < CF4) cout << "CF3 < CF4" << endl;  
    if (CF3 >= CF4) cout << "CF3 >= CF4" << endl;  
    if (CF3 <= CF4) cout << "CF3 <= CF4" << endl;  
    if (CF3 == CF4) cout << "CF3 == CF4" << endl;  
    if (CF3 != CF4) cout << "CF3 != CF4" << endl;  
      
    system("pause");  
}  
第九周 任务三_第1张图片

你可能感兴趣的:(System,Class,input,任务,output)