• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

第9周项目3-分数类的运算符重载续

武飞扬头像
Ticking
帮助1

问题描述:

在分数类中的运算符重载基础上

(1)定义分数的一目运算 和-,分别代表分数取正和求反,将“按位取反运算符”~重载为分数的求倒数运算。

(2)定义分数类中<<和>>运算符重载,实现分数的输入输出,改造原程序中对运算结果显示方式,使程序读起来更自然。

代码:

#include <iostream>
#include <cmath>
using namespace std;

class CFraction
{
private:
    int nume;
    int deno;
public:
    CFraction(int n=0,int d=1):nume(n),deno(d){};
    void display();
    void simplify();
    CFraction operator (CFraction &c2);
    CFraction operator-(CFraction &c2);
    CFraction operator*(CFraction &c2);
    CFraction operator/(CFraction &c2);
    bool operator>(CFraction &c2);
    bool operator<(CFraction &c2);
    bool operator==(CFraction &c2);
    bool operator!=(CFraction &c2);
    bool operator>=(CFraction &c2);
    bool operator<=(CFraction &c2);
    CFraction operator ();
    CFraction operator-();
    CFraction operator~();
    friend istream& operator>>(istream& in,CFraction &c);
    friend ostream& operator<<(ostream& out,CFraction &c);
};
istream &operator>>(istream &in,CFraction &c)
{
    char n;
    cin>>c.nume>>n>>c.deno;
    return in;
}
ostream& operator<<(ostream& out,CFraction &c)
{
    cout<<c.nume<<"/"<<c.deno;
    return out;
}
CFraction CFraction::operator~()
{
    return CFraction(deno,nume);
}
CFraction CFraction::operator ()
{

    return *this;
}
CFraction CFraction::operator-()
{
    CFraction c(-nume,deno);
    return c;
}
void CFraction::display()
{
    cout<<nume<<"/"<<deno;
}
void CFraction::simplify()
{
    int m,d,r;
    m=abs(nume);
    d=abs(deno);
    while((r=(m%d)))
    {
        m=d;d=r;
    }
    nume/=d;deno/=d;
}
CFraction CFraction::operator (CFraction &c2)
{
    CFraction c;
    c.nume=nume*c2.deno c2.nume*deno;
    c.deno=deno*c2.deno;
    c.simplify();
    return c;
}
CFraction CFraction::operator-(CFraction &c2)
{
    CFraction c;
    c.nume=nume*c2.deno-c2.nume*deno;
    c.deno=deno*c2.deno;
    c.simplify();
    return c;
}
CFraction CFraction::operator*(CFraction &c2)
{
    CFraction c;
    c.nume=nume*c2.nume;
    c.deno=deno*c2.deno;
    c.simplify();
    return c;
}
CFraction CFraction::operator/(CFraction &c2)
{
    CFraction c;
    c.nume=nume*c2.deno;
    c.deno=deno*c2.nume;
    c.simplify();
    return c;
}
bool CFraction::operator>(CFraction &c2)
{
    if(nume*c2.deno>c2.nume*deno)
        return true;
    return false;
}
bool CFraction::operator<(CFraction &c2)
{
    if(nume*c2.deno<c2.nume*deno)
        return true;
    return false;
}
bool CFraction::operator==(CFraction &c2)
{
    if(nume*c2.deno==c2.nume*deno)
        return true;
    return false;
}
bool CFraction::operator>=(CFraction &c2)
{
    return !((*this)<c2);
}
bool CFraction::operator<=(CFraction &c2)
{
    return !((*this)>c2);
}
bool CFraction::operator!=(CFraction &c2)
{
    return !((*this)==c2);
}
int main()
{
    CFraction c1,c2,c;
    cin>>c1>>c2;
    cout<<"c1="<<c1<<endl;
    cout<<"c2="<<c2<<endl;
    c=-c1;
    cout<<"-c1="<<c<<endl;
    c=~c1;
    cout<<"~c1="<<c<<endl;
    cout<<endl;
    c=c1 c2;
    cout<<"c1 c2="<<c;
    c=c1-c2;
    cout<<endl;
    cout<<"c1-c2="<<c;
    cout<<endl;
    c=c1*c2;
    cout<<"c1*c2="<<c;
    cout<<endl;
    c=c1/c2;
    cout<<"c1/c2="<<c;
    cout<<endl;
    if(c1>c2) cout<<"c1>c2"<<endl;
    if(c1<c2) cout<<"c1<c2"<<endl;
    if(c1==c2) cout<<"c1==c2"<<endl;
    if(c1>=c2) cout<<"c1>=c2"<<endl;
    if(c1<=c2) cout<<"c1<=c2"<<endl;
    if(c1!=c2) cout<<"c1!=c2"<<endl;
    return 0;
}

运行结果:

学新通

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhhhjaeh
系列文章
更多 icon
同类精品
更多 icon
继续加载