第九周实验报告(3)

时间:2021-11-23 08:06:32
 
#include<iostream.h>     
#include"stdlib.h"      
    
int gcd(int m, int n);      
    
class CFraction    
{    
private:    
    int nume;  // 分子    
    int deno;  // 分母    
public:    
    //构造函数及运算符重载的函数声明    
    CFraction(int nu=0,int de=1);   //构造函数,初始化用      
    void Simplify();                    //化简(使分子分母没有公因子)        
    void output();           //输出:以8/6为例,style为0时,输出8/6;      
    bool operator > (CFraction &t);      
    bool operator < (CFraction &t);      
    bool operator >= (CFraction &t);      
    bool operator <= (CFraction &t);      
    bool operator == (CFraction &t);      
    bool operator != (CFraction &t);      
    CFraction operator+(CFraction &c);       
    CFraction operator-(CFraction &c);    
    CFraction operator*(CFraction &c);      
    CFraction operator/(CFraction &c);    
    CFraction operator-();    
    friend ostream& operator << (ostream&,CFraction &);  
    friend istream& operator >> (istream&,CFraction &);  
        
};    
istream& operator >> (istream& input,CFraction & c)  
{   
    
    
   char c1,c2;  
    cout<<"请您输入一个分数c1:(以a/b的形式输入)";  
   while(1)    
    {    
        input>>c.nume>>c1>>c.deno;      
       if(c1!='/')    
            cout<<"格式输入不正确请重新输入"<<endl;    
       else     
           break;    
    }   
    return input; 

}  
ostream& operator << (ostream& output,CFraction & c)  
{  
    output<<c.nume<<'/'<<c.deno<<endl;   
    return output;  
}  
CFraction::CFraction(int nu,int de)   //构造函数,初始化用       
{    
       if (de!=0)      
       {      
           nume=nu;      
           deno=de;      
       }      
       else      
       {      
           cerr<<"初始化中发生错误,程序退出\n";          
           exit(0);      
       }      
}    
void CFraction::Simplify()                    //化简(使分子分母没有公因子)     
{     
    int n;    
    if(nume < 0)      
    {      
        n = gcd(-nume, deno);      
    }      
    else      
    {      
        n = gcd(nume, deno);      
    }      
      
    nume = nume / n;        
      
    deno = deno / n;    
}    
// 求m,n的最大公约数      
int gcd(int m, int n)      
{      
    int r;      
    if (m<n){r=m;m=n;n=r;}      
    while(r=m%n)  // 求m,n的最大公约数      
    {      
        m=n;      
        n=r;      
    }      
    return n;      
}      
  
bool CFraction::operator > (CFraction &t)    
{    
    CFraction c2,c3;    
    c2.nume =nume*t.deno ;    
    c3.nume =t.nume *deno;    
    if(c2.nume >c3.nume )    
        return true;    
    else    
        return false;    
}    
bool CFraction::operator < (CFraction &t)     
{    
    CFraction c2,c3;    
    c2.nume =nume*t.deno ;    
    c3.nume =t.nume *deno;    
    if(c2.nume <c3.nume )    
        return true;    
    else    
        return false;    
}    
bool CFraction::operator >= (CFraction &t)    
{      
    CFraction c1;      
    c1.nume =nume;    
    c1.deno =deno;    
    if (c1<t)      
        return false;      
    return true;      
}      
    
bool CFraction::operator <= (CFraction &t)    
{      
    CFraction c1;      
    c1.nume =nume;    
    c1.deno =deno;    
    if (c1>t)      
        return false;      
    return true;      
}      
bool CFraction::operator == (CFraction &t)     
{    
    CFraction c1;      
    c1.nume =nume;    
    c1.deno =deno;    
    if (c1<t)      
        return false;      
    if (c1>t)      
        return false;      
    return false;    
}    
    
bool CFraction::operator != (CFraction &t)    
{    
    CFraction c1;      
    c1.nume =nume;    
    c1.deno =deno;    
    if (c1==t)      
        return false;    
    return true;    
}    
CFraction CFraction::operator+(CFraction &c)    
{    
    CFraction c2,c3,c4;    
    c2.nume =nume*c.deno ;    
    c3.nume =c.nume *deno;    
    c2.deno =deno*c.deno ;    
    c3.deno =c.deno *deno;    
    c4.nume=c2.nume +c3.nume ;    
    c4.deno =c2.deno ;    
    c4.Simplify ();    
    return c4;    
}    
CFraction CFraction::operator-(CFraction &c)    
{    
    CFraction c2,c3,c4;    
    c2.nume =nume*c.deno ;    
    c3.nume =c.nume *deno;    
    c2.deno =deno*c.deno ;    
    c3.deno =c.deno *deno;    
    c4.nume=c2.nume -c3.nume ;    
    c4.deno =c2.deno ;    
    c4.Simplify ();    
    return c4;    
}    
CFraction CFraction::operator*(CFraction &c)     
{    
    CFraction c2,c3,c4;    
    c2.nume =nume*c.nume  ;    
    c2.deno =deno*c.deno ;    
    c2.Simplify ();    
    return c2;    
}    
CFraction CFraction::operator/(CFraction &c)    
{    
    CFraction c2,c3;    
    c2.nume =c.deno ;    
    c2.deno =c.nume ;    
    c3.nume =nume*c2.nume ;    
    c3.deno =deno*c2.deno ;    
    c3.Simplify ();    
    return c3;    
}    
CFraction CFraction::operator-()    
{    
    CFraction c2;    
    c2.nume =nume;    
    c2.deno =deno;    
    c2.Simplify ();    
    if(c2.nume<0 || c2.deno<0)    
    {    
        if(c2.nume <0)    
        {    
            c2.nume =-nume;    
        }    
        else    
        {    
            c2.deno =-deno;    
        }    
    }    
    else    
    {    
        c2.nume =-nume;    
        c2.deno =deno;    
    }    
    return c2;    
}    
    
//用于测试的main()函数    
void main()      
{      
    CFraction c1,c2,c;  
    cin>>c1; 
    cin>>c2;  
    cout<<"c1为:";      
    cout<<c1;      
    cout<<"c2为:";      
    cout<<c2;      
    cout<<"下面比较两个时间大小:\n";      
    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;      
    cout<<endl;      
    cout<<"c1+c2的数值为:";      
    c=c1+c2;      
    cout<<c;    
    cout<<endl;    
    cout<<"c1-c2的数值为:";      
    c=c1-c2;      
    cout<<c;      
    cout<<endl;      
    cout<<"c1*c2的数值为:";      
    c=c1*c2;      
    cout<<c;    
    cout<<endl;    
    cout<<"c1/c2的数值为:";      
    c=c1/c2;      
    cout<<c;     
    cout<<endl;    
    cout<<"对c1取反的结果为:";    
    c=-c1;    
    cout<<c;    
    cout<<endl;       
}

  第九周实验报告(3)