第九周 项目三--分数类中的运算符重载(续)

问题及代码:

/*
* Copyright (c) 2015, 烟台大学计算机学院
* All rights reserved.
* 文件名称:Project3.cpp
* 作    者:吴胜男
* 完成日期:2015年4月29日
* 版 本 号:v1.0
*
* 问题描述:(1)定义分数的一目运算+和-,分别代表分数取正和求反,将“按位取反运算符”~重载为分数的求倒数运算。
(2)定义分数类中<<和>>运算符重载,实现分数的输入输出,改造原程序中对运算结果显示方式,使程序读起来更自然。
* 程序输入:略
* 程序输出:略
*/
#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 nu=0,int de=1):nume(nu),deno(de) {}   //构造函数,初始化用
    void simplify();            //化简(使分子分母没有公因子)
    CFraction operator+(const CFraction &c2);
    CFraction operator-(const CFraction &c2);
    CFraction operator*(const CFraction &c2);
    CFraction operator/(const CFraction &c2);
    CFraction operator+(const int a);
    CFraction operator-(const int a);
    CFraction operator*(const int a);
    CFraction operator/(const int a);
    friend CFraction operator+(const int a,const CFraction &c2);
    friend CFraction operator-(const int a,const CFraction &c2);
    friend CFraction operator*(const int a,const CFraction &c2);
    friend CFraction operator/(const int a,const CFraction &c2);
    bool operator > (CFraction &c1);
    bool operator < (CFraction &c1);
    bool operator >= (CFraction &c1);
    bool operator <= (CFraction &c1);
    bool operator == (CFraction &c1);
    bool operator != (CFraction &c1);
    CFraction operator-();
    CFraction operator+();
    CFraction operator~();
    friend istream &operator>>(istream &input,CFraction &x);
    friend ostream &operator<<(ostream &output,CFraction x);
};
istream &operator>>(istream &input,CFraction &x)
{
    char ch;
    while(1)
    {
        input>>x.nume>>ch>>x.deno;
        if (x.deno==0)
            cerr<<"分母为0, 请重新输入\n";
        else if(ch!='/')
            cerr<<"格式错误(形如m/n)! 请重新输入\n";
        else
            break;
    }
    return input;
}

// 重载输出运算符<<
ostream &operator<<(ostream &output,CFraction x)
{
    output<<x.nume<<'/'<<x.deno<<endl;
    return output;
}

CFraction CFraction::operator~()
{
    CFraction c;
    c.nume=deno;
    c.deno=nume;
    if(c.deno<0)   //保证负分数的负号在分子上
    {
        c.deno=-c.deno;
        c.nume=-c.nume;
    }
    return c;
}
CFraction CFraction::operator-()
{
    CFraction c;
    c.nume=-nume;
    c.deno=deno;
    return c;
}
CFraction CFraction::operator+()
{
    return *this;
}
int gcd(int m,int n)
{
    int g;
    if(n==0)
        g=m;
    else
        g=gcd(n,m%n);
    return g;
}
void CFraction::simplify()
{
    int l;
    l=gcd(nume,deno);
    nume=nume/l;
    deno=deno/l;
}

CFraction CFraction::operator+(const CFraction &c2)
{
    CFraction c;
    c.nume=nume*c2.deno+c2.nume*deno;
    c.deno=deno*c2.deno;
    c.simplify();
    return c;
}
CFraction CFraction::operator-(const CFraction &c2)
{
    CFraction c;
    c.nume=nume*c2.deno-c2.nume*deno;
    c.deno=deno*c2.deno;
    c.simplify();
    return c;
}
CFraction CFraction::operator*(const CFraction &c2)
{
    CFraction c;
    c.nume=nume*c2.nume;
    c.deno=deno*c2.deno;
    c.simplify();
    return c;
}
CFraction CFraction::operator/(const CFraction &c2)
{
    CFraction c;
    c.nume=nume*c2.deno;
    c.deno=deno*c2.nume;
    c.simplify();
    return c;
}
bool CFraction::operator > (CFraction &c1)
{
    CFraction c;
    c=*this-c1;
    if(c.nume/c.deno>0)
        return true;
    else
        return false;
}
bool CFraction::operator < (CFraction &c1)
{
    CFraction c;
    c=*this-c1;
    if(c.nume/c.deno<0)
        return true;
    else
        return false;
}
bool CFraction::operator >= (CFraction &c1)
{
    return !(*this<c1);
}
bool CFraction::operator <= (CFraction &c1)
{
    return !(*this>c1);
}
bool CFraction::operator == (CFraction &c1)
{
    if (*this!=c1)
        return false;
    else
        return true;
}
bool CFraction::operator != (CFraction &c1)
{
    if (*this>c1||*this<c1)
        return true;
    else
        return false;
}
CFraction CFraction::operator+(const int a)
{
    CFraction c;
    c.nume=nume+deno*a;
    c.deno=deno;
    c.simplify();
    return c;
}
CFraction CFraction::operator-(const int a)
{
    CFraction c;
    c.nume=nume-deno*a;
    c.deno=deno;
    c.simplify();
    return c;
}
CFraction CFraction::operator*(const int a)
{
    CFraction c;
    c.nume=nume*a;
    c.deno=deno;
    c.simplify();
    return c;
}
CFraction CFraction::operator/(const int a)
{
    CFraction c;
    c.nume=nume;
    c.deno=deno*a;
    c.simplify();
    return c;
}
CFraction operator+(const int a,const CFraction &c2)
{
    CFraction c;
    c.nume=c2.nume+c2.deno*a;
    c.deno=c2.deno;
    c.simplify();
    return c;
}
CFraction operator-(const int a,const CFraction &c2)
{
    CFraction c;
    c.nume=c2.deno*a-c2.nume;
    c.deno=c2.deno;
    c.simplify();
    return c;
}
CFraction operator*(const int a,const CFraction &c2)
{
    CFraction c;
    c.nume=c2.nume*a;
    c.deno=c2.deno;
    c.simplify();
    return c;
}
CFraction operator/(const int a,const CFraction &c2)
{
    CFraction c;
    c.nume=c2.deno*a;
    c.deno=c2.nume;
    c.simplify();
    return c;
}
int main()
{
    CFraction c1;
    CFraction c2;
    CFraction c;
    cout<<"输入c1: ";
    cin>>c1;
    cout<<"输入c2: ";
    cin>>c2;
    c=+c1+c2;
    cout<<"+c1+c2="<<c;
    cout<<"-c1="<<-c1;
    cout<<"+c2="<<+c2;
    cout<<"c1的倒数: "<<~c1;
    CFraction x(4,5),y(-2,6);
    CFraction p;
    cout<<"x=";
    cout<<x;
    cout<<"y=";
    cout<<y;
    p=x+y;
    cout<<"x+y=";
    cout<<p;
    p=x-y;
    cout<<"x-y=";
    cout<<p;
    p=x*y;
    cout<<"x*y=";
    cout<<p;
    p=x/y;
    cout<<"x/y=";
    cout<<p;
    p=x*3;
    p=3*y;
    p=3+x;
    p=y+3;
    p=10-x;
    p=y-6;
    p=y/10;
    p=4/x;
    if (x>y) cout<<"x大于y"<<endl;
    if (x<y) cout<<"x小于y"<<endl;
    if (x==y) cout<<"x等于y"<<endl;
    if (x>=y) cout<<"x大于等于y"<<endl;
    if (x<=y) cout<<"x小于等于y"<<endl;
    if (x!=y) cout<<"x不等于y"<<endl;
    cout<<endl;
    return 0;
}

运行结果:

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值