C++自定义一个分数类,实现分数基本功能与运算符重载

分数类的基本功能

建立一个分数类。分数类的数据成员包括分子和分母,操作包括约分、通分、加、减、乘、除、求倒数、比较、显示和输入。在运算符重载中,对   = , + , − , ∗ , / , + = , − = , ∗ = , / = , + + , − − \ =,+,-,*,/,+=,-=,*=,/=,++,--  =,+,,,/,+=,=,=,/=,++,进行重载

分数类构成

类的定义

class fraction
{
    int above;                      //分子
    int below;                      //分母
    void reduction();               //约分
    void makeCommond(fraction);     //通分
public:
    fraction(int=0, int=1);         //构造函数
    fraction add(fraction);         //两分数相加
    fraction sub(fraction);         //两分数相减
    fraction mul(fraction);         //两分数相乘
    fraction div(fraction);         //两分数相除
    fraction reciprocal();          //求倒数
    bool equal(fraction);           //等于运算
    bool greaterThan(fraction);     //大于运算  
    bool lessThan(fraction);        //小于运算
    void display();                 //显示分数
    void input();                   //输入分数
    int compare(fraction);          //比较大小,小于返回-1,等于返回0,大于返回1   

    //运算符重载
    fraction operator=(fraction);
    friend fraction operator+(const fraction &, const fraction &);
    friend fraction operator-(const fraction &, const fraction &);
    friend fraction operator*(const fraction &, const fraction &);
    friend fraction operator/(const fraction &, const fraction &);
    friend fraction &operator+=(fraction &, const fraction &);
    friend fraction &operator-=(fraction &, const fraction &);
    friend fraction &operator*=(fraction &, const fraction &);
    friend fraction &operator/=(fraction &, const fraction &);
    friend fraction operator++(fraction &);              //前置++
    friend fraction operator++(fraction &, int);         //后置++
    friend fraction operator--(fraction &);              //前置--
    friend fraction operator--(fraction &, int);         //后置--
};

基本函数

构造函数

fraction::fraction(int i, int j)
{
    above = i;
    below = j;
}

约分函数

void fraction::reduction()
{
    int i, j;
    
    // 判断分子是否为0,若为0,将分母置1,返回
    if (above == 0)
    {
        below = 1;
        return;
    }

    if (abs(above) > abs(below))
        j = below;
    else
        j = above;
    
    if (j < 0)
        j = abs(j);

    // 寻找最大公约数
    for (i = j; i > 1; i--)
        if (below % i == 0 && above % i == 0)
        {
            below /= i;
            above /= i;
            return;
        } 
}

通分函数

void fraction::makeCommond(fraction f)
{
    below = below * f.below;
    above = above * f.below;  
}

分数相加

fraction fraction::add(fraction f)
{
    fraction f1(above, below);
    fraction tempf(f.above, f.below);   
    f.makeCommond(f1);          //输入分数对原分数通分
    f1.makeCommond(tempf);      //原分数对输入分数通分      
    f1.above += f.above;        //分子相加
    f1.reduction();             //约分
    return f1;
}

分数相减

fraction fraction::sub(fraction f)
{
    fraction f1(above, below);
    fraction tempf(f.above, f.below);   
    f.makeCommond(f1);          //输入分数对原分数通分
    f1.makeCommond(tempf);      //原分数对输入分数通分      
    f1.above -= f.above;        //分子相减
    f1.reduction();             //约分
    return f1;
}

分数相乘

fraction fraction::mul(fraction f)
{
    f.above *= above;
    f.below *= below;
    f.reduction();
    return f;
}

分数相除

fraction fraction::div(fraction f)
{
    f = f.reciprocal();
    f = mul(f);
    return f;
}

分数倒数

fraction fraction::reciprocal()
{
    fraction f;
    try{
        if (above == 0)
            throw("分数为0,没有倒数");
        f.above = below;
        f.below = above;
    }catch (const char* msg){
        cerr << msg << endl;
    }
    return f;
}

判断分数相等

bool fraction::equal(fraction f)
{
    reduction();
    f.reduction();
    if (f.above == above && f.below == below)
        return true;
    return false;
}

判断分数大于

bool fraction::greaterThan(fraction f)
{
    fraction f1(above, below);
    fraction tempf(f.above, f.below);   
    f.makeCommond(f1);          //输入分数对原分数通分
    f1.makeCommond(tempf);      //原分数对输入分数通分
    if (f1.above > f.above)
        return true;
    else
        return false;
}

判断分数小于

bool fraction::lessThan(fraction f)
{
    return !greaterThan(f);
}

分数显示

void fraction::display()
{
    reduction();
    if (above == 0)
        cout << '0';
    else if (below == 1)
        cout << above;
    else
        cout << above << '/' << below;
}

分数输入

void fraction::input()
{
    cout << "请分别输入分数的分子和分母: " << endl;
    cin >> above >> below;
}

分数比较

int fraction::compare(fraction f)
{
    if (equal(f))
        return 0;
    else if (greaterThan(f))
        return 1;
    else 
        return -1;
}

运算符重载

=重载

fraction fraction::operator=(fraction f)
{
    fraction temp;
    temp.above = f.above;
    temp.below = f.below;
    above = temp.above;
    below = temp.below;
    return temp;
}

+ - * / 重载

fraction operator+(const fraction &a, const fraction &b)
{
    fraction temp(a.above, a.below);
    return temp.add(b);
}

fraction operator-(const fraction &a, const fraction &b)
{
    fraction temp(a.above, a.below);
    return temp.sub(b);
}

fraction operator*(const fraction &a, const fraction &b)
{
    fraction temp(a.above, a.below);
    return temp.mul(b);
}

fraction operator/(const fraction &a, const fraction &b)
{
    fraction temp(a.above, a.below);
    return temp.div(b);
}

+=,-=,*=,/=重载

fraction &operator+=(fraction &a, const fraction &b)
{
    fraction temp;
    temp = a.add(b);
    a.above = temp.above;
    a.below = temp.below;
    return a;
}

fraction &operator-=(fraction &a, const fraction &b)
{
    fraction temp;
    temp = a.sub(b);
    a.above = temp.above;
    a.below = temp.below;
    return a;
}

fraction &operator*=(fraction &a, const fraction &b)
{
    fraction temp;
    temp = a.mul(b);
    a.above = temp.above;
    a.below = temp.below;
    return a;
}

fraction &operator/=(fraction &a, const fraction &b)
{
    fraction temp;
    temp = a.div(b);
    a.above = temp.above;
    a.below = temp.below;
    return a;
}

前置++和- -重载

前置++和- -是先对原变量自增(减),再返回值

fraction operator++(fraction &a)
{
    fraction temp;
    temp = a.add(1);
    a.above = temp.above;
    a.below = temp.below;
    return temp;
}

fraction operator--(fraction &a)
{
    fraction temp;
    temp = a.sub(1);
    a.above = temp.above;
    a.below = temp.below;
    return temp;
}

后置++和- -重载

后置++和- -是先返回一个值,再自增(减),因此我在这里设置了两个临时变量,第二各参数int是为了区分前置与后置,这是C++所规定的

fraction operator++(fraction &a, int)
{
    fraction temp, temp2;
    temp.above = a.above;
    temp.below = a.below;
    temp2 = a.add(1);
    a.above = temp2.above;
    a.below = temp2.below;
    return temp;
}

fraction operator--(fraction &a, int)
{
    fraction temp, temp2;
    temp.above = a.above;
    temp.below = a.below;
    temp2 = a.sub(1);
    a.above = temp2.above;
    a.below = temp2.below;
    return temp;
}

功能测试

基本函数测试

测试代码

int main()
{
    fraction f1;
    cout << "输入分数f1" << endl;    //12/15
    f1.input();
    cout << "f1=";
    // fraction f1(4, 5);
    f1.display();
    cout << endl << "------------" << endl;
    
    fraction f2(5, 6);
    cout << "f2=";
    f2.display();
    fraction f3(1, 2);
    cout << " f3=";
    f3.display();
    cout << " f4=f2+f3=";
    fraction f4;
    f4 = f2.add(f3);
    f4.display();
    cout << endl << "------------" << endl;

    fraction f5;
    f5 = f3.sub(f2);
    cout << "f5=f3-f2=";
    f5.display();
    cout << endl << "------------" << endl;

    fraction f6;
    f6 = f1.mul(f2);
    cout << "f6=f1*f2=";
    f6.display();
    cout << endl << "------------" << endl;

    fraction f7;
    f7 = f1.div(f4);
    cout << "f7=f1/f4=";
    f7.display();
    cout << endl << "------------" << endl;

    fraction f8;
    f8 = f7.reciprocal();
    cout << "f8=1/f7=";
    f8.display();
    cout << endl << "------------" << endl;
    
    fraction f9(8, 10);
    bool r1;
    cout << "f9=";
    f9.display();
    cout << endl << "f9与f1相等吗:";
    r1 = f9.equal(f1);
    cout << r1;
    cout << endl << "------------" << endl;

    cout << "f1大于f2吗:";
    bool r2;
    r2 = f1.greaterThan(f2);
    cout << r2;
    cout << endl << "------------" << endl;

    cout << "f3小于f1吗:";
    bool r3;
    r3 = f3.lessThan(f1);
    cout << r3;
    cout << endl << "------------" << endl;

    cout << "f1与f2的比较: f1";
    int r4;
    r4 = f1.compare(f2);
    if (r4 == -1)
        cout << '<';
    else if (r4 == 0)
        cout << '=';
    else if (r4 == 1)
        cout << '>';
    cout << "f2";
    cout << endl << "------------" << endl;

    return 0;
}

运行结果

在这里插入图片描述

运算符重载测试

测试代码

int main2()
{
    fraction f1(2, 3);
    fraction f2(3, 4);
    cout << "f1="; f1.display(); cout << " f2="; f2.display();
    cout << endl << "------------" << endl;
    
    fraction f3;
    f3 = f1 + f2;
    cout << "f3=f1+f2="; f3.display();
    cout << endl << "------------" << endl;

    fraction f4;
    f4 = f1 - f2;
    cout << "f4=f1-f2="; f4.display();
    cout << endl << "------------" << endl;
    
    fraction f5;
    f5 = f1 * f2;
    cout << "f5=f1*f2="; f5.display();
    cout << endl << "------------" << endl;
    
    fraction f6;
    f6 = f1 / f2;
    cout << "f6=f1/f2="; f6.display();
    cout << endl << "------------" << endl;

    f3 += f1;
    cout << "f3+=f1, f3="; f3.display();
    cout << endl << "------------" << endl;

    f4 -= f1;
    cout << "f4-=f1, f4="; f4.display();
    cout << endl << "------------" << endl;

    f5 *= f1;
    cout << "f5*=f1, f5="; f5.display();
    cout << endl << "------------" << endl;
    
    f6 /= f1;
    cout << "f6/=f1, f6="; f6.display();
    cout << endl << "------------" << endl;

    f3 = f1++;
    cout << "f3=f1++, f3="; f3.display(); cout << " f1="; f1.display();
    cout << endl << "------------" << endl;

    f3 = ++f1;
    cout << "f3=++f1, f3="; f3.display(); cout << " f1="; f1.display();
    cout << endl << "------------" << endl;

    f4 = f2--;
    cout << "f4=f2--, f4="; f4.display(); cout << " f2="; f2.display();
    cout << endl << "------------" << endl;

    f4 = --f2;
    cout << "f4=--f2, f4="; f4.display(); cout << " f2="; f2.display();
    cout << endl << "------------" << endl;

    return 0;
}

运行结果

在这里插入图片描述

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值