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

/*
 * Copyright (c) 2014, 烟台大学计算机学院
 * All rights reserved.
 * 文件名称:test.cpp
 * 作    者:刘佳琦
 * 完成日期:2015年 4 月 25 日
 * 版 本 号:v1.0
 *
 * 问题描述:(1)实现分数类中的运算符重载,在分数类中可以完成分数的加减乘除(运算后再化简)、比较(6种关系)的运算。可以在第4周分数类代码的基础上开始工作。
 * 程序输入:分数
 * 程序输出:重载函数运算的结果
 */
#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"的格式,如"5/2"的形式输入
    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的值: "<<endl;
    c1.set(5,8);
    c1.output(1);
    cout<<"请输入c2的值: ";
    c2.input();
    c2.output(1);
    cout<<"将c2化简: "<<endl;
    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);
    cout<<endl;
    return 0;
}


运行结果:

//用的是第九周项目的图,懒得再截图了,在做第九周项目的时候发现自己这个项目里面运算失误。。少打了个1.

学习心得:

通过以前的分数类雏形加上上一个项目积攒下的经验,这个程序想出来还是比较容易的,就是在编写过程中码的蛮累,然后等到比较大小的时候偷了个懒。这种重载感觉挺有趣的~技能已get√。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值