第十三周项目1.3—分数中的运算符重载

问题及代码:

/*

*Copyright(c) 2016.烟台大学计算机与控制工程学院

*ALL rights  reserved.

*文件名称:main.cpp

*作者:郝昱猛

*完成日期:2016年6月6日

*问题描述:定义分数的一目运算+和-,分别代表分数取正和求反,将“按位取反运算符”~重载为分数的求倒数运算。
*/
#include <iostream>
#include <Cmath>
using namespace std;
class CFraction
{
private:
    int nume;  // 分子
    int deno;  // 分母
public:
    CFraction(int nu=1,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);//两个分数相除,结果要化简

    CFraction operator+(int a);
    CFraction operator-(int a);
    CFraction operator*(int a);
    CFraction operator/(int a);

    CFraction operator+();//取正一目运算
    CFraction operator-();//取反一目运算
    CFraction operator~();//取倒数一目运算

    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);
};
void CFraction::display()
{
    cout<<nume<<"/"<<deno<<endl;
}
//分数简化
void CFraction::simplify()
{
    int m,n,t;
    n=fabs(deno);
    m=fabs(nume);
    while(t=m%n)// 求m,n的最大公约数
    {
       m=n;
       n=t;
    }
    deno/=n;
    nume/=n;
    if(deno<0)
    {
      deno=-deno;
      nume=-nume;
    }
}
CFraction CFraction::operator+(const CFraction &c)
{
    CFraction t;
    t.nume=nume*c.deno+c.nume*deno;
    t.deno=deno*c.deno;
    t.simplify();
    return t;
}
CFraction CFraction::operator-(const CFraction &c)
{
   CFraction t;
   t.nume=nume*c.deno-c.nume*deno;
   t.deno=deno*c.deno;
   t.simplify();
   return t;
}
CFraction CFraction::operator*(const CFraction &c)
{
   CFraction t;
   t.nume=nume*c.nume;
   t.deno=deno*c.deno;
   t.simplify();
   return t;
}
CFraction CFraction::operator/(const CFraction &c)
{
   CFraction t;
   t.nume=nume*c.deno;
   t.deno=deno*c.nume;
   t.simplify();
   return t;
}

CFraction CFraction::operator+(int a)
{
    CFraction t;
    int c;
    c=a*deno;
    t.deno=deno;
    t.nume=nume+c;
    t.simplify();
    return t;
}
CFraction CFraction::operator-(int a)
{
    CFraction t;
    int c;
    c=a*deno;
    t.deno=deno;
    t.nume=nume-c;
    t.simplify();
    return t;
}
CFraction CFraction::operator*(int a)
{
    CFraction t;
    t.deno=deno;
    t.nume=nume*a;
    t.simplify();
    return t;
}
CFraction CFraction::operator/(int a)
{
    CFraction t;
    t.deno=deno*a;
    t.nume=nume;
    t.simplify();
    return t;
}
CFraction CFraction::operator+()
{
  return *this;
}
CFraction CFraction::operator-()
{
    CFraction t;
    t.nume=-nume;
    t.deno=deno;
    return t;
}
CFraction CFraction::operator~()
{
    CFraction t;
    t.nume=deno;
    t.deno=nume;
    if(t.deno<0)//保证负分数的负号在分子上
    {
        t.deno=-t.deno;
        t.nume=-t.nume;
    }
    return t;
}
bool CFraction::operator>(const CFraction &c)
{
    int n1,d1;
    n1=nume*c.deno-c.nume*deno;
    d1=deno*c.deno;
    if(n1*d1>0)
        return true;
    return false;
}
bool CFraction::operator<(const CFraction &c)
{
    int n1,d1;
    n1=nume*c.deno-c.nume*deno;
    d1=deno*c.deno;
    if(n1*d1<0)
        return true;
    return false;
}
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;
}
 bool CFraction::operator<=(const CFraction &c)
{
    if(*this>c)
        return false;
    return true;
}
int main()
{
    char c;
    int n,d;
    cout<<"输入分数的样例:x/y"<<endl;
    cin>>n>>c>>d;
    CFraction x(n,d);
    x.display();
    cin>>n>>c>>d;
    CFraction y(n,d);
    y.display();
    CFraction s;
    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;
    cout<<"请输入要进行运算的整数d"<<endl;
    cin>>d;
    s=x+d;
    cout<<"x+d=";
    s.display();
    cout<<endl;
    s=x-d;
    cout<<"x-d=";
    s.display();
    cout<<endl;
    s=x*d;
    cout<<"x*d=";
    s.display();
    cout<<endl;
    s=x/d;
    cout<<"x/d=";
    s.display();
    cout<<endl;

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

运行结果:


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值