第9周任务3

#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;
}


 

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值