第八周项目3(1)——分数类中的运算符重载

(1)实现分数类中的运算符重载,在分数类中可以完成分数的加减乘除(运算后再化简)、比较(6种关系)的运算。可以在第4周分数类代码的基础上开始工作。 class CFraction  

{  

private:  

    int nume;  // 分子   

    int deno;  // 分母   

public:  

    //构造函数及运算符重载的函数声明   

};  

//重载函数的实现及用于测试的main()函数  

 

代码如下:

 

#include <iostream>
int gcd(int m,int n);
using namespace std;
class CFraction
{
private:
    int nume;  // 分子
    int deno;  // 分母
public:
    //构造函数及运算符重载的函数声明
    CFraction(int nu=0,int de=1):nume(nu),deno(de) {}
    void simplify();
    void display();
    CFraction operator+(const CFraction &c);
    CFraction operator-(const CFraction &c);
    CFraction operator*(const CFraction &c);
    CFraction operator/(const CFraction &c);
    bool operator>(const CFraction &c);
    bool operator<(const CFraction &c);
    bool operator==(const CFraction &c);
    bool operator!=(const CFraction &c);
    bool operator>=(const CFraction &c);
    bool operator<=(const CFraction &c);
};
//重载函数的实现及用于测试的main()函数
int gcd(int m,int n)
{
    int t;
    if(m<n)
    {
        t=m;
        m=n;
        n=t;
    }
    while(t=m%n)
    {
        m=n;
        n=t;
    }
     return n;
}
void CFraction::simplify()
{
    int n=gcd(nume,deno);
    nume/=n;
    deno/=n;
}
void CFraction::display()
{
    cout<<"("<<nume<<"/"<<deno<<")"<<endl;
}
CFraction CFraction::operator+(const CFraction &c)
{
    CFraction z;
    z.nume=nume*c.deno+c.nume*deno;
    z.deno=deno*c.deno;
    z.simplify();
    return z;
}
CFraction CFraction::operator-(const CFraction &c)
{
    CFraction z;
    z.nume=nume*c.deno-c.nume*deno;
    z.deno=deno*c.deno;
    z.simplify();
    return z;
}
CFraction CFraction::operator*(const CFraction &c)
{
    CFraction z;
    z.nume=nume*c.nume;
    z.deno=deno*c.deno;
    z.simplify();
    return z;
}
CFraction CFraction::operator/(const CFraction &c)
{
    CFraction z;
    if(c.nume==0)
        return *this;
    z.nume=nume*c.deno;
    z.deno=deno*c.nume;
    z.simplify();
    return z;
}
bool CFraction::operator>(const CFraction &c)
{
    int t_nume,c_nume,com_deno;
    t_nume=nume*c.deno;
    c_nume=c.nume*deno;
    com_deno=deno*c.deno;
    if((t_nume-c_nume)*com_deno>0) return true;
    return false;
}
bool CFraction::operator<(const CFraction &c)
{
    int t_nume,c_nume,com_deno;
    t_nume=nume*c.deno;
    c_nume=c.nume*deno;
    com_deno=deno*c.deno;
    if((t_nume-c_nume)*com_deno<0) return true;
    return false;
}
bool CFraction::operator==(const CFraction &c)
{
    if(*this<c||*this>c) return false;
    return true;
}
bool CFraction::operator!=(const CFraction &c)
{
    if(*this==c) return false;
    return true;
}
bool CFraction::operator>=(const CFraction &c)
{
    if(*this<c) return false;
    return true;
}
bool CFraction::operator<=(const CFraction &c)
{
    if(*this>c) return false;
    return true;
}

int main()
{
    CFraction x(10,5),y(3,9),s;
    x.display();
    y.display();
    cout<<endl;
    s=x+y;
    cout<<"x+y=";
    s.display();
    s=x-y;
    cout<<"x-y=";
    s.display();
    s=x*y;
    cout<<"x*y=";
    s.display();
    s=x/y;
    cout<<"x/y=";
    s.display();

    if (x>y) cout<<"大于"<<endl;
    if (x<y) cout<<"小于"<<endl;
    if (x==y) cout<<"等于"<<endl;
    if (x>=y) cout<<"大于等于"<<endl;
    if (x<=y) cout<<"小于等于"<<endl;
    if (x!=y) cout<<"不等于"<<endl;
    cout<<endl;

}


总结:没考虑到在除法运算中c.nume==0的结果。不懂比较时度娘是个好帮手

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值