2-8-3 分数类中的运算符重载

问题及代码:

#include <iostream>
#include <cmath>
#include <stdlib.h>
using namespace std;
class CFraction
{
private:
    int nume;  // 分子
    int deno;  // 分母
public:
    CFraction(int nu=0,int de=1):nume(nu),deno(de) {};  //构造函数,初始化用
    void set(int nu=0,int de=1);    //置值,改变值时用
    void input();               //按照"nu/de"的格式,如"5/2"的形式输入
    void simplify();            //化简(使分子分母没有公因子)
    void amplify(int n);        //放大n倍,如2/3放大5倍为10/3
    void output(int style=0);   //输出:以8/6为例,style为0时,原样输出8/6;
    int gcd(int n,int d);
    CFraction operator+(CFraction &c);
    CFraction operator-(CFraction &c);
    CFraction operator*(CFraction &c);
    CFraction operator/(CFraction &c);
    bool operator > (CFraction &c);
    bool operator < (CFraction &c);
    bool operator >= (CFraction &c);
    bool operator <= (CFraction &c);
    bool operator == (CFraction &c);
    bool operator != (CFraction &c);

    //style为1时,输出化简后形式4/3;
    //style为2时,输出1(1/3)形式,表示一又三分之一;
    //style为3时,用小数形式输出,如1.3333;
    //默认方式0
};
void CFraction::set(int nu,int de)
{
    nume=nu;
    deno=de;
};
void CFraction::input()
{
    int a,b;
    char c;
    while(1)
    {
        cin>>a>>c>>b;
        if(c!='/')
            cout<<"输入有误,请重输:";
        else
        {
            nume=a;
            deno=b;
            break;
        }
    }
}
void CFraction::simplify()
{
    int n=gcd(nume,deno);
    nume=nume/n;
    deno=deno/n;
}
void CFraction::amplify(int n)
{
    nume*=n;
}
void CFraction::output(int style)
{
    switch(style)
    {
    case 0:
    {
        cout<<nume<<'/'<<deno<<endl;
        break;
    }
    case 1:
    {
        simplify();
        cout<<nume<<'/'<<deno<<endl;
        break;
    }
    case 2:
    {
        int i;
        i=nume/deno;
        nume=nume%deno;
        if(i==0)
            cout<<nume<<'/'<<deno<<endl;
        else if(nume==0)
            cout<<i<<endl;
        else
            cout<<i<<'('<<nume<<'/'<<deno<<')'<<endl;
        break;
        case 3:
        {
            cout<<double(nume)/double(deno)<<endl;
            break;
        }
    }
    }
}
int CFraction::gcd(int n,int d)
{
    int r;
    for ( ; d!=0 ;) //用for循环构造的辗转相除法
    {
        r = n%d ;
        n = d ;
        d = r ;
    }
    return n;
}
CFraction CFraction::operator+(CFraction &c)
{
    CFraction c1(nume*c.deno+deno*c.nume,deno*c.deno);
    c1.simplify();
    return c1;
}
CFraction CFraction::operator-(CFraction &c)
{
    CFraction c1(nume*c.deno-deno*c.nume,deno*c.deno);
    c1.simplify();
    return c1;
}
CFraction CFraction::operator*(CFraction &c)
{
    CFraction c1(nume*c.nume,deno*c.deno);
    c1.simplify();
    return c1;
}
CFraction CFraction::operator/(CFraction &c)
{
    CFraction c1(nume*c.deno,deno*c.nume);
    c1.simplify();
    return c1;
}
bool CFraction::operator>(CFraction &c)
{
    CFraction c1;
    double x;
    c1=*this-c;
    x=c1.nume*c1.deno;
    return (x>0);

}
bool CFraction::operator<(CFraction &c)
{
    CFraction c1;
    double x;
    c1=*this-c;
    x=c1.nume*c1.deno;
    return (x<0);
}
bool CFraction::operator<=(CFraction &c)
{
    return !(*this>c);
}
bool CFraction::operator>=(CFraction &c)
{
    return !(*this<c);
}
bool CFraction::operator==(CFraction &c)
{
    simplify();
    c.simplify();
    if(nume==c.nume&&deno==c.deno)
        return true;
    else
        return false;
}
bool CFraction::operator!=(CFraction &c)
{
    return !(*this==c);
}
int main()
{
    CFraction c1,c2(4,2),c3(8,6),c4,c5,c6(1,3),c7(1,4),c8(1,2),c9(3,6);
    c1.output();
    c2.output(1);
    c2.set(3,4);
    c2.amplify(3);
    c2.output(2);
    c3.simplify();
    c3.output(3);
    c4.input();
    c4.output();
    c5=c6+c7;
    c5.output();
    c5=c6-c7;
    c5.output();
    c5=c6*c7;
    c5.output();
    c5=c6/c7;
    c5.output();
    cout<<(c6>c7?"c6>c7":"c6<c7")<<endl;
    cout<<(c6<c7?"c6<c7":"c6>c7")<<endl;
    cout<<(c6>=c7?"c6>=c7":"c6<c7")<<endl;
    cout<<(c6<=c7?"c6<=c7":"c6>c7")<<endl;
    cout<<(c8==c9?"c8=c9":"c8!=c9")<<endl;
    cout<<(c6!=c7?"c6!=c7":"c6=c7")<<endl;
    return 0;
}


运行结果:


学习小结:

好像是第一次来钟楼编程!有点紧张啊,哈哈

程序中遇到几个问题,用debug顺利的解决了,才发现这玩意真的很好用

还有一个没解决的问题就是在set();函数中怎么判断输入的两个是int型数据,不然我如果输入三个字母,程序就无限循环了。


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值