第八周任务三

/* (程序头部注释开始) 
* 程序的版权和版本声明部分 
* Copyright (c) 2011, 烟台大学计算机学院学生  
* All rights reserved. 
* 文件名称:                               
* 作    者:   张艳明            
* 完成日期:   2012   年  4 月 10  日 
* 版 本 号:           
 
* 对任务及求解方法的描述部分 
* 输入描述: 
* 问题描述:实现分数类中的运算符重载,在分数类中可以完成分数的加减乘除(运算后再化简),求反。比较(6中关系)的运算。 
* 程序输出:  
* 程序头部的注释结束 
*/  
#include<iostream>      
  
using namespace std;      
  
class CFraction      
  
{      
  
private:      
  
    int nume; // 分子      
  
    int deno; // 分母      
  
public:      
  
    CFraction(int nu=0,int de=1); //构造函数,初始化用      
  
    void Set(int nu=0,int de=1); //置值,改变值时用  
  
    CFraction operator+(CFraction &t);   
  
    CFraction operator-(CFraction &t);  
  
    CFraction operator*(CFraction &t);  
  
    CFraction operator/(CFraction &t);    
  
    friend CFraction operator-(CFraction &t);//取反  
  
    bool operator>(CFraction &t);   
  
    bool operator<(CFraction &t);  
  
    bool operator>=(CFraction &t);   
  
    bool operator<=(CFraction &t);  
  
    bool operator==(CFraction &t);   
  
    bool operator!=(CFraction &t);    
  
    void Simplify(int n);            //化简(使分子分母没有公因子)      
  
    int gcd(int x,int y);    
  
    void output(int style=0);   //输出:以8/6为例,style为0时,输出8/6;CFraction(int nu=0,int de=1); //构造函数,初始  
  
化用      
  
};      
  
  
CFraction::CFraction(int nu,int de)      
{      
    nume = nu;      
    deno = de;      
  
}      
  
void CFraction::Set(int nu,int de) //置值,改变值时用      
{      
    if(de !=0)    
    {    
        nume = nu;    
        deno = de;    
    }    
    else    
    {    
        cout<<"分母不能为零"<<endl;    
        exit(0);    
    }    
  
    nume = nu;      
  
    deno = de;      
  
}      
  
void CFraction::Simplify(int n)        //化简(使分子分母没有公因子)      
{      
    n = gcd(nume,deno);    
  
    nume = nume/n;    
    deno = deno/n;    
  
}      
  
int CFraction::gcd(int x,int y)  //求公约数的函数  
{    
    int r;    
    while( y!= 0)    
    {    
        r = x%y;    
        x = y;    
        y = r;    
    }    
    return x;    
}    
  
  
void CFraction::output(int style)   //输出:以8/6为例,style为0时,输出8/6;CFraction(int nu=0,int de=1); //构造函数,初始  
  
化用      
  
{      
    switch(style)    
    {    
  
    case 0:    
        {    
            cout<<nume<<'/'<<deno<<endl;    
            break;    
        }    
    case 1:    
        {    
            int m = gcd(nume,deno);    
            cout<<(nume/m)<<'/'<<(deno/m)<<endl;    
            break;    
        }    
    case 2:    
        {    
            int nu,de,g;    
            g = gcd(nume,deno);    
            nu = nume/g;    
            de = deno/g;    
            cout<<(nu/de)<<"("<<(nu%de)<<'/'<<de<<")"<<endl;    
            break;    
        }    
    default:    
        {    
            cout<<nume<<'/'<<deno<<endl;    
        }    
    }    
  
}      
  
CFraction CFraction:: operator+(CFraction &t)  
{  
    CFraction cf;    
    if(deno==t.deno)  //分母相同时比较分子  
    {    
        cf.deno=deno;    
        cf.nume=nume+t.nume;    
    }    
    else  //否则同分比较分子  
    {    
        cf.deno=deno*t.deno;    
        cf.nume=nume*t.deno+deno*t.nume;    
    }    
    return cf;    
}  
CFraction CFraction:: operator-(CFraction &t)  
{  
    CFraction cf;    
    if(deno==t.deno)    
    {    
        cf.deno=deno;    
        cf.nume=nume-t.nume;    
    }    
    else    
    {    
        cf.deno=deno*t.deno;    
        cf.nume=t.deno*nume-t.nume*deno;    
    }    
    return cf;    
}  
CFraction  CFraction:: operator*(CFraction &t)  
{  
    CFraction cf;  
    cf.deno = deno*t.deno;  
    cf.nume = nume*t.nume;  
    return cf;  
}  
CFraction  CFraction:: operator/(CFraction &t)  
{  
    CFraction cf;  
    cf.deno = deno*t.nume;  
    cf.nume = nume*t.deno;  
    return cf;  
}  
CFraction operator-(CFraction &t)//取反    
{  
    CFraction cf;  
    cf.deno  = -t.deno;  
    cf.nume  = t.nume;  
    return(cf);  
}  
bool CFraction::operator>(CFraction &t)  
{  
    if(nume*t.deno>t.nume*deno)  
        return true;  
    else  
        return false;  
}  
bool CFraction::operator<(CFraction &t)  
{  
    if(nume*t.deno<t.nume*deno)  
        return true;  
    else  
        return false;  
}  
bool CFraction::operator>=(CFraction &t)  
{  
    if(nume*t.deno>t.nume*deno||nume*t.deno==t.nume*deno)  
        return true;  
    else  
        return false;  
}  
bool CFraction::operator<=(CFraction &t)   
{  
    if(nume*t.deno<t.nume*deno||nume*t.deno==t.nume*deno)  
        return true;  
    else  
        return false;  
}  
bool CFraction::operator==(CFraction &t)  
{  
    if(nume*t.deno==t.nume*deno)  
        return true;  
    else  
        return false;  
}  
bool CFraction::operator!=(CFraction &t)  
{  
    if(!operator==(t))  
        return true;  
    else  
        return false;  
}  
  
int main ()      
{      
    CFraction cf1(17,5),cf2(9,6),cf3;  
  
    cout<<"cf1 =";  
    cf1.output ();  
  
    cout<<"cf2 =";  
    cf2.output ();  
  
    cf3=cf1*cf2;    
    cout<<"cf1*cf2=";    
    cf3.output();  
  
    cout<<"约分后:";  
    cf3.output(1);  
  
    cout<<"化简后的形式:";   
    cf3.output(2);  
  
    cf3=cf1-cf2;   
    cout<<"cf1-cf2=";  
    cf3.output(1);  
  
    cf3=cf1+cf2;    
    cout<<"cf1+cf2=";   
    cf3.output(1);   
  
    cf3=cf1/cf2;    
    cout<<"cf1/cf2=";    
    cf3.output(1);   
  
    cf3=-cf1;   
    cout<<"-cf1 =";  
    cf3.output(1);  
  
    if (cf1>cf2) cout<<"cf1>cf2"<<endl;   
  
    if (cf1<cf2) cout<<"cf1<cf2"<<endl;   
  
    if (cf1==cf2) cout<<"cf1=cf2"<<endl;   
  
    if (cf1!=cf2) cout<<"cf1≠cf2"<<endl;   
  
    if (cf1>=cf2) cout<<"cf1≥cf2"<<endl;  
  
    if (cf1<=cf2) cout<<"cf1≤cf2"<<endl;    
  
    system("pause");    
  
    return 0;    
  
}      

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值