第九周(3)

/* (程序头部注释开始)
* 程序的版权和版本声明部分
* Copyright (c) 2011, 烟台大学计算机学院学生 
* All rights reserved.
* 文件名称:                              
* 作    者:            苗影                  
* 完成日期:     2012    年    4   月    16    日
* 版 本 号:          

* 对任务及求解方法的描述部分
* 输入描述: 
* 问题描述: 
* 程序输出: 
* 程序头部的注释结束
*/

#include<iostream>   
using namespace std;  
class CFraction  
{  
private:  
    int nume;  // 分子   
    int deno;  // 分母   
public:  
     CFraction(int nu=0,int de=1):nume(nu),deno(de){}    
    void simplify();    
    friend ostream& operator << (ostream&,CFraction&);//声明重载运算符“<<”函数
    CFraction operator+( CFraction &c);  //两个分数相加,结果要化简      
    CFraction operator-( CFraction &c);  //两个分数相减,结果要化简      
    CFraction operator*( CFraction &c);  //两个分数相乘,结果要化简      
    CFraction operator/( CFraction &c);  //两个分数相除,结果要化简      
    CFraction operator+();  //取正一目运算      
    CFraction operator-();  //取反一目运算      
    bool operator>(CFraction &c);    
    bool operator<( CFraction &c);    
    bool operator==( CFraction &c);    
    bool operator!=( CFraction &c);    
    bool operator>=( CFraction &c);    
    bool operator<=( CFraction &c);    
  
  
};  
  ostream& operator << (ostream& output,CFraction& c)//定义重载运算符“<<”函数  
{  
	output << "(" <<c. nume<< "/" << c.deno<<")";  
    return output;  
} 
  
 void CFraction::simplify()  
{  
    int h,m,i,j;  
    for(i=2;i<=6;i++)  
    {  
    {  
        h=nume%i;  
        m=deno%i;  
    }  
      
    while(h==0&&m==0)  
      
    {   j=i;  
        nume=nume/j;  
        deno=deno/j;  
        h=nume%i;  
        m=deno%i;  
    }  
    }  
cout<<nume<<"/"<<deno<<endl;  
 }   
CFraction CFraction::operator+( CFraction &c)    
{    
    CFraction t;    
    t.nume=nume*c.deno+c.nume*deno;    
    t.deno=deno*c.deno;     
    return t;    
}    
CFraction CFraction:: operator-( CFraction &c)    
{    
    CFraction t;    
    t.nume=nume*c.deno-c.nume*deno;    
    t.deno=deno*c.deno;  
    return t;    
}    
CFraction CFraction:: operator/( CFraction &c)    
{    
    CFraction t;    
    t.nume=nume*c.nume;    
    t.deno=deno*c.deno;     
    return t;    
}    
CFraction CFraction:: operator*( CFraction &c)    
{  
   CFraction t;    
    t.nume=nume*c.deno;    
    t.deno=deno*c.nume;    
    return t;    
}  
      
CFraction CFraction:: operator+()    
{   CFraction t;   
   nume=nume;  
   deno=deno;  
   return t;  
}  
CFraction CFraction:: operator-()    
{  
   CFraction t;   
   t.nume=-nume;  
   t.deno=deno;  
return t;  
}  
bool CFraction::operator>( CFraction &c)    
{  
  
  int t,g,h;    
    t=nume*c.deno;          
    g=c.nume*deno;     
    h=deno*c.deno;   
    if(t>g&&h>0||t<g&&h<0)  
    return true;  
    else return false;  
}  
bool CFraction::operator<( CFraction &c)    
{  
    int t,g,h;    
    t=nume*c.deno;          
    g=c.nume*deno;     
    h=deno*c.deno;   
    if(t<g&&h>0||t>g&&h<0)  
    return true;  
    else return false;  
}  
bool CFraction::operator==( CFraction &c)   
{  
    int t,g,h;    
    t=nume*c.deno;          
    g=c.nume*deno;     
    h=deno*c.deno;   
   if(t==g)  
   return true;  
    else return false;  
}  
bool CFraction::operator!=( CFraction &c)   
{  
    int t,g,h;    
    t=nume*c.deno;          
    g=c.nume*deno;     
    h=deno*c.deno;   
   if(t!=g)  
    return true;  
    else return false;  
}  
bool CFraction::operator>=( CFraction &c) //这种方法很方便哦   
{  
   if (*this<c) return false;    
   else  return true;    
}  
bool CFraction::operator<=( CFraction &c)   
{  
   if (*this>c) return false;    
   else   return true;    
}  
void main()  
{  
    CFraction x(3,4),y(2,6),s;    
    cout<<"分数x=3/4    y=2/6"<<endl;    
    s=x+y;    
   cout<<"x+y=";    
    s.simplify();   
    cout<<endl;  
    s=x-y;  
    cout<<"x-y=";  
    s.simplify();  
   cout<<endl;  
    s=x*y;  
    cout<<"x*y=";  
    s.simplify();  
    cout<<endl;  
    s=x/y;  
    cout<<"x/y=";  
    s.simplify();  
   cout<<endl;  
    if(x>y)  
    {  
        cout<<x<<">"<<y;
		cout<<endl;
       
    }  
   if(x<y)  
    {  
    cout<<x<<">"<<y;
	cout<<endl;
  
    }  
    if(x==y)  
    {  
       cout<<x<<"=="<<y;  
       cout<<endl; 
    }  
    if(x!=y)  
    {  
        cout<<x<<"!="<<y;  
		cout<<endl;
    }  
   if(x<=y)  
    {  
        cout<<x<<"<="<<y;
     
    }  
   cout<<endl;
    s=-x;  
    cout<<"(3/4)的相反数是";  
    cout<<s;
  system("pause");  
}



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值