第八周项目三--分数类中的运算符重载

问题及代码:

/*
 * Copyright (c) 2014, 烟台大学计算机学院
 * All rights reserved.
 * 文件名称:test.cpp
 * 作    者:尚 月
 * 完成日期:2015年 05 月 05 日
 * 版 本 号:v1.0
 *
 * 问题描述:实现分数类中运算符的重载,在分数类中可以完成分数的加减乘除运算(化简后的)、比较的运算。
 * 程序输入:输入C1的值,输入C2的值
 * 程序输出:重载函数运算后的结果
 */
#include <iostream>
#include<cmath>
#include<cstdlib>
using namespace std;
int gcd(int m,int n);
class CFraction
{
private:
    int nume;  //分子
    int deno;  //分母
public:
    CFraction(int n=1,int d=1);  //构造函数,初始化用
    void set(int n,int d); //置值,改变值时用
    void input();          //按照“nu/de”的格式输入
    void simplify();       //化简(使分子分母没有公因子)
    void output(int style=0);
    CFraction operator+(CFraction &c1);
    CFraction operator-(CFraction &c1);
    CFraction operator*(CFraction &c1);
    CFraction operator/(CFraction &c1);
    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::CFraction(int n,int d)
{
    nume=n;
    deno=d;
}
void CFraction::set(int n,int d)
{
    nume=n;
    deno=d;
}
void CFraction::input()
{
    int n,d;
    char c='/';
    cin>>n>>c>>d;
    nume=n;
    deno=d;
}
void CFraction::simplify()
{
    int n=gcd(deno,nume);
    deno/=n;
    nume/=n;
}
int gcd(int m, int n)
{
    int r;
    if (m<n)
    {
        r=m;
        m=n;
        n=r;
    }
    while(r=m%n)  // 求m,n的最大公约数
    {

        m=n;
        n=r;
    }
    return n;
}
CFraction CFraction::operator+(CFraction &c1)
{
    CFraction c;
    c.nume=nume*c1.deno+c1.nume*deno;
    c.deno=deno*c1.deno;

    return c;
}

CFraction CFraction::operator-(CFraction &c1)
{
    CFraction c;
    c.nume=nume*c1.deno-c1.nume*deno;
    c.deno=deno*c1.deno;
    return c;
}
CFraction CFraction::operator*(CFraction &c1)
{
    CFraction c;
    c.nume=nume*c1.nume;
    c.deno=deno*c1.deno;

    return c;
}
CFraction CFraction::operator/(CFraction &c1)
{
    CFraction c;
    c.nume=nume*c1.deno;//除以一个数等于乘它的倒数。
    c.deno=deno*c1.nume;

    return c;
}

// 分数比较大小
bool CFraction::operator>(CFraction &c)
{
    int this_nume,c_nume,common_deno;
    this_nume=nume*c.deno;        // 计算分数通分后的分子,同分母为deno*c.deno
    c_nume=c.nume*deno;
    common_deno=deno*c.deno;
    if ((this_nume>c_nume&&common_deno>0)||(this_nume<c_nume&&common_deno<0)) return true; // 将通分后的分子比较大小
    return false;
}

// 分数比较大小
bool CFraction::operator<(CFraction &c)
{
    int this_nume,c_nume,common_deno;
    this_nume=nume*c.deno;
    c_nume=c.nume*deno;
    common_deno=deno*c.deno;
    if ((this_nume-c_nume)*common_deno<0) return true;
    return false;
}

// 分数比较大小
bool CFraction::operator==(CFraction &c)
{
    if (*this!=c) return false;
    return true;
}

// 分数比较大小
bool CFraction::operator!=(CFraction &c)
{
    if (*this>c || *this<c) return true;
    return false;
}

// 分数比较大小
bool CFraction::operator>=(CFraction &c)
{
    if (*this<c) return false;
    return true;
}

// 分数比较大小
bool CFraction::operator<=(CFraction &c)
{
    if (*this>c) return false;
    return true;
}
void CFraction::output(int style)
{
    int n;
    if(style==0)
        cout<<nume<<'/'<<deno<<endl;
    else if(style==1)
    {
        n=gcd(deno,nume);
        cout<<nume/n<<'/'<<deno/n<<endl;
    }
    else if(style==2)
        cout<<"带分数:" <<nume/deno<<'('<<nume%deno<<'/'<<deno<<')'<<endl;
    else if(style==3)
        cout<<"近似值:" <<nume/double(deno)<<endl;
    else
        cout<<"默认:" <<nume<<'/'<<deno<<endl;
}
int main()
{
    CFraction c1,c2,c3;
    cout<<"请输入c1的值:";
    c1.input();
    cout<<"改变c1的值为: ";
    c1.set(5,8);
    c1.output(1);
    cout<<"请输入c2的值: ";
    c2.input();
    cout<<"将c2化简后得之为: ";
    c2.simplify();
    c2.output(0);
    c3=c1+c2;
    cout<<"c1+c2=";
    c3.output(1);
    c3=c1-c2;
    cout<<"c1-c2=";
    c3.output(1);
    c3=c1*c2;
    cout<<"c1*c2=";
    c3.output(1);
    c3=c1/c2;
    cout<<"c1/c2=";
    c3.output(1);
    c1.output(1);
    if (c1>c2) cout<<" > "<<endl;
    if (c1<c2) cout<<" < "<<endl;
    if (c1==c2) cout<<" = "<<endl;
    c2.output(1);
    return 0;
}

运行结果:


学习总结:终于体会到平时计算个分数特别简单,但是用程序把它编出来,注意的细节太多了,有点晕@_@~~

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值