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

问题及代码:

/*

 *Copyright (c) 2015,烟台大学计算机学院

*All rights reserved.

*文件名称:test.cop

*作者:

*完成日期:2015年5月17日

*版本号:v1.0

*

*问题描述: 

*输入描述:

*输出描述: 
#include <iostream>
using namespace std;
class CFraction
{
private:
    int nume;  // 分子
    int deno;  // 分母
public:
    //构造函数及运算符重载的函数声明
    CFraction(int n=0,int d=1){nume=n;deno=d;}
    friend ostream &operator<<(ostream &out,CFraction c);
    friend istream &operator>>(istream &in,CFraction &c);
    CFraction operator+();
    CFraction operator-();
    CFraction operator~();
    void display();
    void simply();
    CFraction operator+(const CFraction &t);
    CFraction operator-(const CFraction &t);
    CFraction operator*(const CFraction &t);
    CFraction operator /(const CFraction &t);
    bool operator>(const CFraction &t);
    bool operator<(const CFraction &t);
    bool operator>=(const CFraction &t);
    bool operator<=(const CFraction &t);
    bool operator==(const CFraction &t);
    bool operator!=(const CFraction &t);
};
//重载函数的实现及用于测试的main()函数
void CFraction::display()
{
    cout<<nume<<"/"<<deno<<endl;
}
void CFraction::simply()
{
    int a,b,c;
    if(nume>deno){a=nume;b=deno;}
    else {a=deno;b=nume;}
    while(a%b)
    {
        c=a%b;
        a=b;
        b=c;
    }
    nume=nume/b;
    deno=deno/b;

}
ostream &operator<<(ostream&out,CFraction c)
{
    out<<c.nume<<"/"<<c.deno<<endl;
    return out;
}
istream &operator>>(istream&in,CFraction &c)
{
    char a;
    while(1)
    {
        in>>c.nume>>a>>c.deno;
        if(c.deno!=0&&a=='/')break;
        else
            cout<<"输入格式错误,请从新输入:"<<endl;
    }
}

CFraction CFraction::operator-()
{
    nume=-nume;
    return *this;
}
CFraction CFraction::operator+()
{
    return *this;
}
CFraction CFraction::operator~()
{
    int a;
    a=nume;
    nume=deno;
    deno=a;
    return *this;
}
CFraction CFraction::operator+(const CFraction &t)
{
    CFraction c1;
    c1.deno=deno*t.deno;
    c1.nume=nume*t.deno+t.nume*deno;
    c1.simply();
    return c1;
}
CFraction CFraction::operator-(const CFraction &t)
{
CFraction c1;
    c1.deno=deno*t.deno;
    c1.nume=nume*t.deno-t.nume*deno;
    c1.simply();
    return c1;
}
CFraction CFraction::operator*(const CFraction &t)
{
    CFraction c1;
    c1.deno=deno*t.deno;
    c1.nume=nume*t.nume;
    return c1;
}
CFraction CFraction::operator/(const CFraction &t)
{
    CFraction c1;
    c1.deno=deno*t.nume;
    c1.nume=nume*t.deno;
    return c1;
}
bool CFraction::operator>(const CFraction &t)
{
    if(nume*t.deno>t.nume*deno)
           return true;
    else  return false;
}
bool CFraction::operator<(const CFraction &t)
{
    if(nume*t.deno>t.nume*deno)
        return false;
    else return true;
}
bool CFraction::operator<=(const CFraction &t)
{
    if(*this<t==true) return false;
    else return true;
}
bool CFraction::operator>=(const CFraction &t)
{
    if(*this>t==true) return false;
    else return true;
}
bool CFraction::operator==(const CFraction &t)
{
    if((*this>t==false)&&(*this<t==false)) return true;
    else return false;
}
bool CFraction::operator!=(const CFraction &t)
{
    if((*this==t)==false) return true;
    else return false;
}
int main()
{
    CFraction x,y,s;
    cout<<"输入x: ";
    cin>>x;
    cout<<"输入y: ";
    cin>>y;
    s=+x+y;
    cout<<"+x+y="<<s<<endl;
    s=x-y;
    cout<<"x-y="<<s<<endl;
    s=x*y;
    cout<<"x*y="<<s<<endl;
    s=x/y;
    cout<<"x/y="<<s<<endl;
    cout<<"-x="<<-x<<endl;
    cout<<"+x="<<+x<<endl;
    cout<<"x的倒数: "<<~x<<endl;

    cout<<x;
    if (x>y) cout<<"大于";
    if (x<y) cout<<"小于";
    if (x==y) cout<<"等于";
    cout<<y<<endl;
    return 0;
}

运行结果:



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值