第9周任务3

来源:互联网 发布:淘宝收货时间最迟 编辑:程序博客网 时间:2024/05/20 00:48
#include <iostream>   using namespace std; #include "jun.h"class CFraction  {  private:   int nume;  // 分子    int deno;  // 分母   public:   CFraction(){nume=0;deno=0;}   CFraction(int n,int d) :nume(n),deno(d){}   CFraction operator +(CFraction &x);      CFraction operator -(CFraction &x);      CFraction operator *(CFraction &x);      CFraction operator /(CFraction &x);      bool operator >(CFraction &x);    bool operator <(CFraction &x);   //构造函数及运算符重载的函数声明       bool operator =(CFraction &x);      bool operator >=(CFraction &x);      bool operator <=(CFraction &x);   bool operator !=(CFraction &x);//死记住开头的形式,       //CFraction operator-();/////////////////////////////////////////////////    int f(int,int);   int g(int,int);   void display(); friend ostream& operator << (ostream&,CFraction&);friend istream& operator >> (istream&,CFraction&);};  CFraction CFraction::operator +(CFraction &x)  {      CFraction c;     int t=f(deno,x.deno);   c.nume=nume*(t/deno)+(t/x.deno)*x.nume;   c.deno=t;      t=g(c.nume,c.deno);   c.nume=c.nume/t;   c.deno=c.deno/t;   return c;    }  CFraction CFraction::operator -(CFraction &x)  {      CFraction c;     int t=f(deno,x.deno);   c.nume=nume*(t/deno)-(t/x.deno)*x.nume;   c.deno=t;      t=g(c.nume,c.deno);   c.nume=c.nume/t;   c.deno=c.deno/t;   return c;  }        CFraction CFraction::operator *(CFraction &x)  {   CFraction c;   int t;   c.nume=nume*x.nume;   c.deno=deno*x.deno;   t=g(c.nume,c.deno);   c.nume=c.nume/t;   c.deno=c.deno/t;   return c;  }  CFraction CFraction::operator /(CFraction &x)  {      CFraction c;   int t;   c.nume=nume*x.deno;   c.deno=deno*x.nume;      t=g(c.nume,c.deno);   c.nume=c.nume/t;   c.deno=c.deno/t;   return c;  }    bool CFraction::operator >(CFraction &x)  {      //bool flag=true;       CFraction c;   int t=f(deno,x.deno);   c.nume=nume*(t/deno)-(t/x.deno)*x.nume;   if(c.nume>0)   {     return true;   }   else   {    //flag=false;     return false;   }    }  bool CFraction::operator <(CFraction &x)  {   //bool flag=true;    int t=f(deno,x.deno);      CFraction c;   c.nume=nume*(t/deno)-(t/x.deno)*x.nume;   if(c.nume<0)   {    return true;   }   else   {    //flag=false;     return false;   }  }  bool CFraction::operator =(CFraction &x)  {      //bool flag=true;    int t=f(deno,x.deno);      CFraction c;   c.nume=nume*(t/deno)-(t/x.deno)*x.nume;   if(c.nume=0)   {    return true;   }   else   {    //flag=false;     return false;   }  }    bool CFraction::operator >=(CFraction &x)  {      //bool flag=false;    int t=f(deno,x.deno);      CFraction c;   c.nume=nume*(t/deno)-(t/x.deno)*x.nume;   if(c.nume<0)   {    return false;   }   else   {    //flag=true;     return true;   }  }   bool CFraction::operator <=(CFraction &x)  {      //bool flag=false;    int t=f(deno,x.deno);   CFraction c;   c.nume=nume*(t/deno)-(t/x.deno)*x.nume;   if(c.nume>0)   {    return false;   }   else   {    //flag=true;     return true;   }  }  bool CFraction::operator !=(CFraction &x)//注意开头的形式   {   //bool flag=false;    int t=f(deno,x.deno);      CFraction c;   c.nume=nume*(t/deno)-(t/x.deno)*x.nume;   if(c.nume==0)   {    return false;   }   else   {    //flag=true;     return true;   }  }  /*CFraction CFraction:: operator -() {  //*this=this*(-1);  return(0- *this); }*/  int CFraction::f(int x,int y)  {   int t,p,r;   if(x<y)   {    t=x;    x=y;    y=t;   }   p=x*y;   while(y!=0)   {    r=x%y;    x=y;    y=r;   }   return p/x;  }  int CFraction:: g(int x,int y)  {   int t,p,r;   if(x<y)   {    t=x;    x=y;    y=t;   }   p=x*y;   while(y!=0)   {    r=x%y;    x=y;    y=r;   }   return x;  }  void CFraction:: display()  {   cout<<nume<<'/'<<deno;  }  void main()  {   //CFraction x1(3,12),x2(1,3);   CFraction x1,x2; cin>>x1>>x2; cout<<"x1=";   cout<<x1<<endl; x1.display();   cout<<endl;      cout<<"x2="; cout<<x2<<endl; x2.display();   cout<<endl;   cout<<"x1+x2=:";   cout<<(x1+x2); cout<<endl;   cout<<"x1-x2=:";   cout<<(x1-x2);   cout<<endl;   cout<<"x1*x2=:";   cout<<(x1*x2); cout<<endl;   cout<<"x1/x2=:";   cout<<(x1/x2); cout<<endl;   if(x1<x2) cout<<"x1<x2";   cout<<endl;   if(x1!=x2) cout<<"x1!=x2"; }ostream& operator << (ostream& output,CFraction&x){ output<<x.nume<<'/'<<x.deno; return output;}istream& operator >> (istream& input,CFraction&x){ cout<<"请输入数值:";  input>>x.nume>>x.deno; return input;}


 

感悟:经过定义“<<"和”》“之后发现,这样可以使程序更加通俗易懂简单,且不用再定义输出函数。