第八周任务4

#include <iostream>   
using namespace std;  

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);
 CFraction operator +(int x);  
 CFraction operator -(int x);  
 CFraction operator *(int x);  
 CFraction operator /(int 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();  
};  
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;  
}  
CFraction CFraction::operator +(int x) 
{
 CFraction c;
 c.nume=nume+deno*x;
 c.deno=deno;
    int t=g(c.nume,c.deno);  
    c.nume=c.nume/t;  
     c.deno=c.deno/t;  
 return c;
}
CFraction CFraction:: operator -(int x)
{ 
 CFraction c;
 c.nume=nume-deno*x;
 c.deno=deno;
    int t=g(c.nume,c.deno);  
    c.nume=c.nume/t;  
     c.deno=c.deno/t;  
 return c;
}


 CFraction CFraction:: operator *(int x)  
 {  
  
  CFraction c;
 c.nume=nume*x;
 c.deno=deno;
    int t=g(c.nume,c.deno);  
    c.nume=c.nume/t;  
     c.deno=c.deno/t;  
 return c;
 }

 CFraction CFraction::operator /(int x)
 {
  CFraction c;
 c.nume=nume;
 c.deno=deno*x;
    int t=g(c.nume,c.deno);  
    c.nume=c.nume/t;  
     c.deno=c.deno/t;  
 return c;
 }
void main()  
{  
 CFraction x1(3,12),x2(1,3); 
 int b=2;
 
 //double c=b+x1;
 //cout<<c<<endl;
 cout<<"x1=";  
 x1.display();  
 cout<<endl;  
    cout<<"x2=";  
 x2.display();  
 cout<<endl;  
(x1+b).display();
cout<<endl;  
(x1-b).display();
cout<<endl;  

(x1*b).display();
cout<<endl;  
(x1/b).display();
cout<<endl;
 cout<<"x1+x2=:";  
 (x1+x2).display();  
 cout<<endl;  
 cout<<"x1-x2=:";  
 (x1-x2).display();  
 cout<<endl;  
 cout<<"x1*x2=:";  
 (x1*x2).display();  
 cout<<endl;  
 cout<<"x1/x2=:";  
 (x1/x2).display();  
 cout<<endl;  
 if(x1<x2) cout<<"x1<x2";  
 cout<<endl;  
 if(x1!=x2) cout<<"x1!=x2";  
}  


感悟:整数和分数的相加其实很简答,只不过改了改参数而已。

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值