第7周-项目3-分数类中的运算符重载-分数的加减乘除

问题及代码:

/*   
*Copyright (c)2015,烟台大学计算机与控制工程学院   
*All rights reserved.   
*文件名称:Fraction.cpp   
*作    者:单昕昕   
*完成日期:2015年4月27日   
*版 本 号:v1.0   
*问题描述:实现分数类中的运算符重载,在分数类中可以完成分数的加减乘除(运算后再化简)、比较(6种关系)的运算。可以在第4周分数类代码的基础上开始工作。
*程序输入:无。
*程序输出:分数。
*/ 
#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);    //置值,改变值时用
    void input();               //按照"nu/de"的格式,如"5/2"的形式输入
    void simplify();            //化简(使分子分母没有公因子)
    void display();
    bool operator > (CFraction &c);
    bool operator < (CFraction &c);
    bool operator >= (CFraction &c);
    bool operator <= (CFraction &c);
    bool operator == (CFraction &c);
    bool operator != (CFraction &c);
    CFraction operator + (const CFraction &c2);
    CFraction operator - (const CFraction &c2);
    CFraction operator * (const CFraction &c2);
    CFraction operator / (const CFraction &c2);
};

int gys(int a,int b)
{
    return (a%b!=0?(gys(b,a%b)):b);
}
int gbs(int u,int v)
{
    int h;
    h=gys(u,v);
    return (u*v/h);
}

CFraction::CFraction(int nu,int de)
{
    if(de!=0)
    {
        nume=nu;
        deno=de;
    }
}
void CFraction::set(int nu,int de)//置值,改变值时用
{
    if(de!=0)
    {
        nume=nu;
        deno=de;
    }
}
bool CFraction::operator > (CFraction &c)
{
    int r;
    if (deno!=c.deno) //将分数化成相同分母便于比较
    {
        r=gbs(deno,c.deno);
        if(nume*r/deno>c.nume*r/c.deno)
            return true;
        else
            return false;
    }
    else
    {
        if(nume>c.nume)
            return true;
        else
            return false;
    }
}
bool CFraction::operator < (CFraction &c)
{
    int r;
    if (deno!=c.deno) //将分数化成相同分母便于比较
    {
        r=gbs(deno,c.deno);
        if(nume*r/deno<c.nume*r/c.deno)
            return true;
        else
            return false;
    }
    else
    {
        if(nume<c.nume)
            return true;
        else
            return false;
    }
}
bool CFraction::operator >= (CFraction &c)
{
    return !(*this < c);
}
bool CFraction::operator <= (CFraction &c)
{
    return !(*this > c);
}
bool CFraction::operator == (CFraction &c)
{
    return !((*this > c)||(*this < c));
}
bool CFraction::operator != (CFraction &c)
{
    return ((*this > c)||(*this < c));
}
CFraction CFraction::operator + (const CFraction &c2)
{
    CFraction c;
    int r;
    if (deno!=c.deno) //取分母的最大公倍数
    {
        r=gbs(deno,c2.deno);
        c.nume=(nume*r/deno)+(c2.nume*r/c2.deno);
        c.deno=r;
    }
    else
    {
        c.nume=nume+c2.nume;
        c.deno=deno;
    }
    return c;
}
CFraction CFraction::operator - (const CFraction &c2)
{
    CFraction c;
    int r;
    if (deno!=c.deno) //取分母的最大公倍数
    {
        r=gbs(deno,c2.deno);
        c.nume=(nume*r/deno)-(c2.nume*r/c2.deno);
        c.deno=r;
    }
    else
    {
        c.nume=nume-c2.nume;
        c.deno=deno;
    }
    return c;
}
CFraction CFraction::operator * (const CFraction &c2)
{
    CFraction c;
    c.nume=nume*c2.nume;
    c.deno=deno*c2.deno;
    return c;
}
CFraction CFraction::operator / (const CFraction &c2)
{
    CFraction c;
    c.nume=nume*c2.deno;
    c.deno=deno*c2.nume;
    return c;
}
void CFraction::simplify()//化简(使分子分母没有公因子)
{
    int r;
    r=gys(nume,deno);
    nume/=r;
    deno/=r;
}
void CFraction::display()
{
    simplify();
    if(deno!=1)
        cout<<nume<<"/"<<deno;
    else
        cout<<nume;
}

int main()
{
    CFraction x(-2,3),y(6,10),s;
    cout<<"两个分数是:"<<endl;
    x.display();
    cout<<'\t';
    y.display();
    cout<<endl;
    cout<<"下面比较两个分数的大小:"<<endl;
    if (x>y)
    {
        x.display();
        cout<<">";
        y.display();
        cout<<endl;
    }
    if (x<y)
    {
        x.display();
        cout<<"<";
        y.display();
        cout<<endl;
    }
    if (x==y)
    {
        x.display();
        cout<<"=";
        y.display();
        cout<<endl;
    }
    if (x!=y)
    {
        x.display();
        cout<<"≠";
        y.display();
        cout<<endl;
    }
    if (x>=y)
    {
        x.display();
        cout<<"≥";
        y.display();
        cout<<endl;
    }
    if (x<=y)
    {
        x.display();
        cout<<"≤";
        y.display();
        cout<<endl;
    }
    cout<<"下面对两个分数进行计算:"<<endl;
    s=x+y;
    cout<<"x+y=";
    s.display();
    cout<<endl;
    s=x-y;
    cout<<"x-y=";
    s.display();
    cout<<endl;
    s=x*y;
    cout<<"x*y=";
    s.display();
    cout<<endl;
    s=x/y;
    cout<<"x/y=";
    s.display();
    cout<<endl;
    return 0;
}


运行结果:


知识点总结:
分数类中的运算符重载实现分数的加减乘除并化简。

学习心得:

分数主要是要将分母化成一样的,这样方便比较和计算,所以我定义了求最大公倍数的普通函数用于计算。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值