关闭

项目一——分数类运算符重载3

59人阅读 评论(0) 收藏 举报

问题及代码:

/*copyright(c)2016.烟台大学计算机学院
 * All rights reserved,
 * 文件名称:text.Cpp
 * 作者:李一波
 * 完成日期:2016年5月30日
 * 版本号:vc++6.0
 *
 * 问题描述:实现分数类中的运算符重载,在分数类中可以完成分数的加减乘除(运算后再化简)、比较(6种关系)的运算。
 * 输入描述:
 * 程序输出:
 */
#include <iostream>
using namespace std;
class CFraction
{
private:
    int nume;  // 分子
    int deno;  // 分母
public:
    CFraction(){}
    CFraction(int n,int d):nume(n),deno(d){}
    void show();
    friend istream &operator>>(istream &in,CFraction &x);
    friend ostream &operator<<(ostream &out,CFraction x);
    CFraction operator+(const CFraction &a);
    CFraction operator-(const CFraction &a);
    CFraction operator*(const CFraction &a);
    CFraction operator/(const CFraction &a);
    CFraction operator+(const int &a);
    CFraction operator-(const int &a);
    CFraction operator*(const int &a);
    CFraction operator/(const int &a);
    bool operator>(const CFraction &a);
    bool operator<(const CFraction &a);
    bool operator>=(const CFraction &a);
    bool operator<=(const CFraction &a);
    bool operator==(const CFraction &a);
    bool operator!=(const CFraction &a);
    CFraction hua_jian();
    CFraction operator+();
    CFraction operator-();
    CFraction operator~();
};
istream &operator>>(istream &in,CFraction &x)
{
    char ch;
    while(1)
    {
        cin>>x.nume>>ch>>x.deno;
        if (x.deno==0)
            cerr<<"分母为0, 请重新输入\n";
        else if(ch!='/')
            cerr<<"格式错误(形如m/n)! 请重新输入\n";
        else
            break;
    }
    return cin;
}

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

CFraction CFraction::operator+()//取正数
{
    return this->hua_jian();
}
CFraction CFraction::operator-()//取负数
{
    CFraction c;
    c.nume=-nume;
    c.deno=-deno;

    return c;
}
CFraction CFraction::operator~()//取倒数
{
    CFraction c;
    c.deno=nume;
    c.nume=deno;
    if(c.deno<0)
    {
        c.deno=-c.deno;
        c.nume=-c.nume;
    }
    return c.hua_jian();
}
CFraction CFraction::operator+(const CFraction &a)
{
    CFraction c;
    if(deno==a.deno)
        c.nume=nume+a.nume;
    else
    {
        c.deno=deno*a.deno;
        c.nume=nume*a.deno+a.nume*deno;
    }
    return c.hua_jian();
}
CFraction CFraction::operator-(const CFraction &a)
{
    CFraction c;
    if(deno==a.deno)
        c.nume=nume-a.nume;
    else
    {
        c.deno=deno*a.deno;
        c.nume=nume*a.deno-a.nume*deno;
    }
    return c.hua_jian();
}
CFraction CFraction::operator*(const CFraction &a)
{
    CFraction c;
    c.deno=deno*a.deno;
    c.nume=nume*a.nume;
    return c.hua_jian();
}
CFraction CFraction::operator/(const CFraction &a)
{
    CFraction c;
    c.deno=deno*a.nume;
    c.nume=nume*a.deno;
    return c.hua_jian();
}
CFraction CFraction::operator+(const int &a)
{
    CFraction c;
    c.deno=deno;
    c.nume=nume+deno*a;
    return c.hua_jian();
}
CFraction CFraction::operator-(const int &a)
{
    CFraction c;
    c.deno=deno;
    c.nume=nume-deno*a;
    return c.hua_jian();
}
CFraction CFraction::operator*(const int &a)
{
    CFraction c;
    c.deno=deno;
    c.nume=nume*a;
    return c.hua_jian();
}
CFraction CFraction::operator/(const int &a)
{
    CFraction c;
    c.deno=deno*a;
    c.nume=nume;
    return c.hua_jian();
}
bool CFraction::operator>(const CFraction &a)
{
    if(deno==a.deno)
        if(nume>a.nume)
            return 1;
        else
            return -1;
    else
    {
        if(nume*a.deno>a.nume*deno)
            return 1;
        else
            return -1;
    }
}
bool CFraction::operator<(const CFraction &a)
{
    if(deno==a.deno)
        if(nume<a.nume)
            return 1;
        else
            return -1;
    else
    {
        if(nume*a.deno<a.nume*deno)
            return 1;
        else
            return -1;
    }
}
bool CFraction::operator>=(const CFraction &a)
{
    if(deno==a.deno)
        if(nume>=a.nume)
            return 1;
        else
            return -1;
    else
    {
        if(nume*a.deno>=a.nume*deno)
            return 1;
        else
            return -1;
    }
}
bool CFraction::operator<=(const CFraction &a)
{
    if(deno==a.deno)
        if(nume<=a.nume)
            return 1;
        else
            return -1;
    else
    {
        if(nume*a.deno<=a.nume*deno)
            return 1;
        else
            return -1;
    }
}
bool CFraction::operator==(const CFraction &a)
{
    if(deno==a.deno)
        if(nume==a.nume)
            return 1;
        else
            return -1;
    else
    {
        if(nume*a.deno==a.nume*deno)
            return 1;
        else
            return -1;
    }
}
bool CFraction::operator!=(const CFraction &a)
{
    if(deno==a.deno)
        if(nume!=a.nume)
            return 1;
        else
            return -1;
    else
    {
        if(nume*a.deno!=a.nume*deno)
            return 1;
        else
            return -1;
    }
}
CFraction CFraction::hua_jian()
{
    int t,t1,t2;
    t1=nume;
    t2=deno;
    while(t2!=0)
    {
        t=t1%t2;
        t1=t2;
        t2=t;
    }
    nume/=t1;
    deno/=t1;
    if(deno<0)
    {
        deno=-deno;
        nume=-nume;
    }
    return *this;
}
void CFraction::show()
{
    cout<<nume<<'/'<<deno<<endl;
}
//重载函数的实现及用于测试的main()函数
int main()
{
    CFraction c1(2,3),c2(5,6),c3;
    int i=2;
    c3=c1+c2;
    cout<<"c1+c2之和是:";
    c3.show();
    c3=c1-c2;
    cout<<"c1-c2之差是:";
    c3.show();
    c3=c1*c2;
    cout<<"c1*c2是:";
    c3.show();
    c3=c1/c2;
    cout<<"c1/c2是:";
    c3.show();
    c3=c1+i;
    cout<<"c1+i之和是:";
    c3.show();
    c3=c1-i;
    cout<<"c1-i之差是:";
    c3.show();
    c3=c1*i;
    cout<<"c1*i是:";
    c3.show();
    c3=c1/i;
    cout<<"c1/i是:";
    c3.show();
    if((c1>c2)==1)
        cout<<"c1大于c2"<<endl;
    else
        cout<<"c1小于c2"<<endl;
    if((c1<c2)==1)
        cout<<"c1小于c2"<<endl;
    else
        cout<<"c1大于c2"<<endl;
    if((c1>=c2)==1)
        cout<<"c1大于等于c2"<<endl;
    else
        cout<<"c1小于c2"<<endl;
    if((c1<=c2)==1)
        cout<<"c1小于等于c2"<<endl;
    else
        cout<<"c1大于c2"<<endl;
    if((c1==c2)==1)
        cout<<"c1等于c2"<<endl;
    else
        cout<<"c1不等于c2"<<endl;
    if((c1!=c2)==1)
        cout<<"c1不等于c2"<<endl;
    else
        cout<<"c1等于c2"<<endl;
    cout<<"-c1="<<-c1<<endl;
    cout<<"+c1="<<+c1<<endl;
    cout<<"c1的倒数: "<<~c1<<endl;
    return 0;
}

运行结果:

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:23960次
    • 积分:1432
    • 等级:
    • 排名:千里之外
    • 原创:124篇
    • 转载:9篇
    • 译文:1篇
    • 评论:0条