第十三周上机实践——项目1-分数类中的运算符重载

/* 
 *Copyright (c)2016,烟台大学计算机与控制工程学院 
 *All rights reserved. 
 *文件名称:main.cpp,Fraction.h,Fraction.cpp 
 *作    者:郭永恒 
 *完成日期:2016年5月27日 
 *版 本 号:v1.0 
 * 
 *问题描述:实现分数类中的运算符重载,可以完成分数和分数,
 *分数和整数之间的四则运算,符合交换律,定义取正、取反、取倒数
 *的运算符和<<、>>运算符
 */ 


Fraction.h

#ifndef FRACTION_H_INCLUDED
#define FRACTION_H_INCLUDED
#include <iostream>

class CFraction
{
    //整数和分数的四则运算
    friend CFraction operator+(int ifrac, CFraction& ffrac);
    friend CFraction operator-(int ifrac, CFraction& ffrac);
    friend CFraction operator*(int ifrac, CFraction& ffrac);
    friend CFraction operator/(int ifrac, CFraction& ffrac);

    friend std::istream& operator>>(std::istream& is, CFraction& obj);
    friend std::ostream& operator<<(std::ostream& os, CFraction& obj);
public:
    CFraction(int up = 0,int down = 1):nume(up),deno(down)
    {
        if(down == 0)
            std::cerr << "Deno Error" << std::endl;
        correct();
    }
    CFraction(const CFraction &fraction):nume(fraction.nume),deno(fraction.deno){}//拷贝构造函数

    //分数的四则运算
    CFraction operator+(CFraction& frac);
    CFraction operator-(CFraction& frac);
    CFraction operator*(const CFraction& frac);
    CFraction operator/(const CFraction& frac);

    //分数和整数的四则运算
    CFraction operator+(int frac);
    CFraction operator-(int frac);
    CFraction operator*(const int frac);
    CFraction operator/(const int frac);

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

private:
    void correct();//保持负号在分子上
    void same(CFraction& num);//通分
    void reduction();//化简自己
    int greatestCommonDivisor(int, int);//最大公约数
    int nume;//分子
    int deno;//分母
};

#endif // FRACTION_H_INCLUDED

Fraction.cpp

#include "Fraction.h"

//输入输出运算符的重载放在头文件会出错
std::istream& operator>>(std::istream& is, CFraction& obj)
{
    char temp;
    is >> obj.nume >> temp >> obj.deno;
    return is;
}

std::ostream& operator<<(std::ostream& os, CFraction& obj)
{
    //如果分母是1
    if(obj.deno == 1)
        os << obj.nume;
    //如果分子是0
    else if(obj.nume == 0)
        os << obj.nume;
    else
        os << obj.nume << "/" << obj.deno;
    return os;
}


//通分是两个分数对象,化简是一个分数
int CFraction::greatestCommonDivisor(int num1, int num2)//最大公约数
{
    int temp;
    num1 = num1 > 0 ? num1:-num1;
    num2 = num2 > 0 ? num2:-num2;
    while(num1 % num2)
    {
        temp = num1;
        num1 = num2;
        num2 = temp%num2;
    }
    return num2;
}

void CFraction::reduction()//化简自己
{
    int re = greatestCommonDivisor(nume,deno);
    nume /= re;
    deno /= re;
}

void CFraction::same(CFraction& num)//通分
{
    int re = greatestCommonDivisor(this->deno,num.deno);
    re = (this->deno * num.deno) / re;//最小公倍数
    int mul = re / this->deno;
    this->deno *= mul;
    this->nume *= mul;
    mul = re / num.deno;
    num.deno *= mul;
    num.nume *= mul;
}

void CFraction::correct()
{
    if((deno < 0 && nume < 0) ||(deno < 0 && nume > 0))
    {
        deno = -deno;
        nume = -nume;
    }
}

//分数和之间的四则元算
CFraction CFraction::operator*(const CFraction& frac)
{
    CFraction temp;
    temp.nume = this->nume * frac.nume;
    temp.deno = this->deno * frac.deno;
    temp.reduction();
    temp.correct();
    return temp;
}

CFraction CFraction::operator/(const CFraction& frac)
{
    CFraction temp;
    temp.nume = this->nume * frac.deno;
    temp.deno = this->deno * frac.nume;
    temp.reduction();
    temp.correct();
    return temp;
}

CFraction CFraction::operator+(CFraction& frac)
{
    CFraction temp;
    if(this->deno != frac.deno)
        (*this).same(frac);
    temp.nume = this->nume + frac.nume;
    temp.deno = this->deno;
    temp.reduction();
    this->reduction();
    frac.reduction();
    return temp;
}

CFraction CFraction::operator-(CFraction& frac)
{
    CFraction temp;
    if(this->deno != frac.deno)
        (*this).same(frac);
    temp.nume = this->nume - frac.nume;
    temp.deno = this->deno;
    temp.reduction();
    this->reduction();
    frac.reduction();
    return temp;
}

//分数和整数的四则运算
CFraction CFraction::operator*(int frac)
{
    CFraction temp;
    temp.nume = this->nume * frac;
    temp.deno = this->deno;
    temp.reduction();
    temp.correct();
    return temp;
}

CFraction CFraction::operator/(int frac)
{
    CFraction temp;
    temp.deno = this->deno * frac;
    temp.nume = this->nume;
    temp.reduction();
    temp.correct();
    return temp;
}

CFraction CFraction::operator+(int frac)
{
    CFraction temp;
    frac *= this->deno;
    temp.nume = this->nume + frac;
    temp.deno = this->deno;
    temp.reduction();
    return temp;
}

CFraction CFraction::operator-(int frac)
{
    CFraction temp;
    frac *= this->deno;
    temp.nume = this->nume - frac;
    temp.deno = this->deno;
    temp.reduction();
    return temp;
}

//整数和分数的四则运算
CFraction operator*(int ifrac, CFraction& ffrac)
{
    CFraction temp;
    temp.nume = ifrac * ffrac.nume;
    temp.deno = ffrac.deno;
    temp.reduction();
    temp.correct();
    return temp;
}

CFraction operator/(int ifrac, CFraction& ffrac)
{
    CFraction temp;
    temp.nume = ifrac * ffrac.deno;
    temp.deno = ffrac.nume;
    temp.reduction();
    temp.correct();
    return temp;
}

CFraction operator+(int ifrac, CFraction& ffrac)
{
    CFraction temp;
    ifrac *= ffrac.deno;
    temp.nume = ffrac.nume + ifrac;
    temp.deno = ffrac.deno;
    temp.reduction();
    return temp;
}

CFraction operator-(int ifrac, CFraction& ffrac)
{
    CFraction temp;
    ifrac *= ffrac.deno;
    temp.nume = ifrac - ffrac.nume;
    temp.deno = ffrac.deno;
    temp.reduction();
    return temp;
}

//一目运算符
CFraction CFraction::operator+()
{
    return *this;
}

CFraction CFraction::operator-()
{
    CFraction temp;
    temp.nume = -this->nume;
    temp.deno = this->deno;
    return temp;
}

CFraction CFraction::operator~()
{
    CFraction temp;
    temp.nume = this->deno;
    temp.deno = this->nume;
    temp.correct();
    return temp;
}

main.cpp

#include "Fraction.h"
using namespace std;

int main()
{
    CFraction n1(1,-2),n2(1,2);
    CFraction n3 = n2 * n1;
    cout << n3 << endl;
    n3 = n1 / n2;
    cout << n3 << endl;
    n3 = n1 + n2;
    cout << n3 << endl;
    n3 = n1 - n2;
    cout << n3 << endl;

    n3 = 1 + n1;
    cout << n3 << endl;
    n3 = 3 * n2;
    cout << n3 << endl;
    n3 = 1 / n1;
    cout << n3 << endl;
    n3 = 1 - n1;
    cout << n3 << endl;
    return 0;
}

运行结果:


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值