第8周-项目3-分数类中的运算符重载(续)-++、--、>>、<<

来源:互联网 发布:mysql 数据库快照 编辑:程序博客网 时间:2024/05/29 13:51
问题及代码:

/*   *Copyright (c)2015,烟台大学计算机与控制工程学院   *All rights reserved.   *文件名称:CFraction.cpp   *作    者:单昕昕   *完成日期:2015年5月5日   *版 本 号:v1.0   *问题描述:(在第8周项目3基础上(1)定义分数的一目运算+和-,分别代表分数取正和求反,将“按位取反运算符”~重载为分数的求倒数运算。(2)定义分数类中<<和>>运算符重载,实现分数的输入输出,改造原程序中对运算结果显示方式,使程序读起来更自然。*程序输入:两个分数。*程序输出:分数运算结果。*/ //Sample Input//-2/3 6/10#include <iostream>#include <cmath>using namespace std;class CFraction{private:    int nume;  // 分子    int deno;  // 分母public:    CFraction(int nu=0,int de=1);   //构造函数,初始化用    void set(int nu=0,int de=1);    //置值,改变值时用    void input();               //按照"nu/de"的格式,如"5/2"的形式输入    void simplify();            //化简(使分子分母没有公因子)    void display();    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 operator + (const CFraction &c2);    CFraction operator - (const CFraction &c2);    CFraction operator * (const CFraction &c2);    CFraction operator / (const CFraction &c2);    CFraction operator+();//分数取正    CFraction operator-();//分数求反    friend istream& operator>>(istream&input,CFraction &c);    friend ostream& operator<<(ostream&output,CFraction &c);};int gys(int a,int b){    return (a%b!=0?(gys(b,a%b)):b);}int gbs(int u,int v){    int h;    h=gys(u,v);    return (u*v/h);}CFraction::CFraction(int nu,int de){    if(de!=0)    {        nume=nu;        deno=de;    }}void CFraction::set(int nu,int de)//置值,改变值时用{    if(de!=0)    {        nume=nu;        deno=de;    }}bool CFraction::operator > (CFraction &c){    int r;    if (deno!=c.deno) //将分数化成相同分母便于比较    {        r=gbs(deno,c.deno);        if(nume*r/deno>c.nume*r/c.deno)            return true;        else            return false;    }    else    {        if(nume>c.nume)            return true;        else            return false;    }}bool CFraction::operator < (CFraction &c){    int r;    if (deno!=c.deno) //将分数化成相同分母便于比较    {        r=gbs(deno,c.deno);        if(nume*r/deno<c.nume*r/c.deno)            return true;        else            return false;    }    else    {        if(nume<c.nume)            return true;        else            return false;    }}bool CFraction::operator >= (CFraction &c){    return !(*this < c);}bool CFraction::operator <= (CFraction &c){    return !(*this > c);}bool CFraction::operator == (CFraction &c){    return !((*this > c)||(*this < c));}bool CFraction::operator != (CFraction &c){    return ((*this > c)||(*this < c));}CFraction CFraction::operator + (const CFraction &c2){    CFraction c;    int r;    if (deno!=c.deno) //取分母的最大公倍数    {        r=gbs(deno,c2.deno);        c.nume=(nume*r/deno)+(c2.nume*r/c2.deno);        c.deno=r;    }    else    {        c.nume=nume+c2.nume;        c.deno=deno;    }    return c;}CFraction CFraction::operator - (const CFraction &c2){    CFraction c;    int r;    if (deno!=c.deno) //取分母的最大公倍数    {        r=gbs(deno,c2.deno);        c.nume=(nume*r/deno)-(c2.nume*r/c2.deno);        c.deno=r;    }    else    {        c.nume=nume-c2.nume;        c.deno=deno;    }    return c;}CFraction CFraction::operator * (const CFraction &c2){    CFraction c;    c.nume=nume*c2.nume;    c.deno=deno*c2.deno;    return c;}CFraction CFraction::operator / (const CFraction &c2){    CFraction c;    c.nume=nume*c2.deno;    c.deno=deno*c2.nume;    return c;}void CFraction::simplify()//化简(使分子分母没有公因子){    int r;    r=gys(nume,deno);    nume/=r;    deno/=r;}//流插入istream& operator>>(istream&input,CFraction &c){    int a,b;    char sign;    do    {        cout<<"please input a fraction(a/b):";        input>>a>>sign>>b;    }    while(!(sign=='/'));    c.nume=a;    c.deno=b;    return input;}//流提取ostream& operator<<(ostream&output,CFraction &c){    c.simplify();    if(c.deno!=1)        output<<c.nume<<"/"<<c.deno;    else        output<<c.nume;    return output;}CFraction CFraction::operator+()//分数取正{    return CFraction(fabs(nume),fabs(deno));}CFraction CFraction::operator-()//分数求反{    return CFraction(-fabs(nume),fabs(deno));}int main(){    CFraction x,y,s;    cin>>x>>y;    cout<<"两个分数是:"<<x<<'\t'<<y<<endl;    cout<<"下面比较两个分数的大小:"<<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;    if (x<=y)   cout<<x<<"≤"<<y<<endl;    cout<<"下面对两个分数进行计算:"<<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;    s=x/y;    cout<<"x/y="<<s<<endl;    cout<<"下面对两个分数进行取正和求反:"<<endl;    s=(+x);    cout<<"+x="<<s<<endl;    s=(-y);    cout<<"-y="<<s<<endl;    return 0;}


运行结果:


知识点总结:
重载单目运算符。

学习心得:

不得不说,这个程序是本周项目里面最简单的一个噢哈哈~~

0 0
原创粉丝点击