关闭

第13周项1—分数类中的运算符重载 (2)

153人阅读 评论(0) 收藏 举报
分类:


//
*Copyright(c) 2016.烟台大学计算机与控制工程学院
*ALL rights  reserved.
*文件名称:main.cpp
 *作者:孙亚茹
*完成日期:2016年5月31日
*问题描述:在分数类中完成分数的取正取反,按位运算。
*//

#include<iostream>
#include<cmath>
using namespace std;
class CFraction
{
private:
    int nume;  // 分子
    int deno;  // 分母
public:
    CFraction(int num=0,int den=1):nume(num),deno(den) {};
    CFraction operator+(const CFraction &c1);
    CFraction operator-(const CFraction &c1);
    CFraction operator*(const CFraction &c1);
    CFraction operator/(const CFraction &c1);
    friend istream &operator>>(istream &in,CFraction &c1);
    friend ostream &operator<<(ostream &out,CFraction &c1);
    bool operator==(const CFraction &c1);
    bool operator>(const CFraction &c1);
    bool  operator<(const CFraction &c1);\
     CFraction operator+();
   CFraction operator-();
  CFraction operator~();

    void huajian();

    //构造函数及运算符重载的函数声明
};
void CFraction::huajian()
{
    int t,m,n;
    m=deno;
    n=nume;
    while(n!=0)
    {
        t=m%n;
        m=n;
        n=t;
    }
    deno/=m;
    nume/=m;
}
CFraction CFraction::operator+(const CFraction &c1)
{
    CFraction c;
    c.deno=deno*c1.deno;
   c.nume=nume*c1.deno+c1.nume*deno;
    c.huajian();
    return c;
}
CFraction CFraction::operator-(const CFraction &c1)
{
    CFraction c;
    c.deno=deno*c1.deno;
    c.nume=nume*c1.deno-c1.nume*deno;
    c.huajian();
    return c;
}
CFraction CFraction::operator*(const CFraction &c1)
{

    CFraction c;
    c.deno=deno*c1.deno;
    c.nume=nume*c1.nume;
    c.huajian();
    return c;

}

CFraction CFraction::operator/(const CFraction &c1)
{
    CFraction c;
    c.deno=deno*c1.nume;
    c.nume=nume*c1.deno;
    c.huajian();
    return c1;
}
istream &operator>>(istream &in,CFraction &c1)
{
    char ch;
    while(1)
    {
        cin>>c1.nume>>ch>>c1.deno;
        if(c1.deno==0)
            cout<<"分母不能为0,请重输"<<endl;
        else if(ch!='/')
            cout<<"格式错误,请重输"<<endl;
        else
            break;
    }
    return cin;
}
ostream &operator<<(ostream &out,CFraction &c1)
{
    cout<<c1.nume<<'/'<<c1.deno;
    return cout;
}
bool CFraction::operator==(const CFraction &c1)
{
    int x,y;
    x=nume*c1.deno;
    y=c1.nume*deno;
    if(x-y==0)
        return true;
    else
        return false;

}
bool CFraction::operator>(const CFraction &c1)
{
    int x,y;
    x=nume*c1.deno;
    y=c1.nume*deno;
    if(x-y>0)
        return true;
    else
        return false;

}
bool CFraction::operator<(const CFraction &c1)
{
    int x,y;
    x=nume*c1.deno;
    y=c1.nume*deno;
    if(x-y<0)
        return true;
    else
        return false;

}
CFraction CFraction::operator+()
{
    return *this;
}
CFraction CFraction ::operator-()
{
    CFraction n;
    n.nume=-nume;
    n.deno=deno;
    return n;
}
CFraction CFraction::operator~()
{
    CFraction n;

    if(nume<0)
   {
       n.deno=-nume;
       n.nume=-deno;
    }
    else
    {
        n.nume=deno;
        n.deno=nume;
    }

    return n;
}

int main()
{
    CFraction c1,c2,c3;

    cout<<"请输入第一个分数:"<<endl;
    cin>>c1;
    cout<<"请输入第二个分数:"<<endl;
    cin>>c2;
    cout<<endl;
    cout<<"下面进行计算:"<<endl;
    c3=c1+c2;
    cout<<"x+c2= "<<c3<<endl;
    c3=c1-c2;
    cout<<"x-c2= "<<c3<<endl;
    c3=c1*c2;
    cout<<"c1*c2= "<<c3<<endl;
    c3=c1/c2;
    cout<<"c1/c2= "<<c3<<endl;
    cout<<endl;
    cout<<"下面进行比较:"<<endl;
    if(c1>c2)
        cout<<"c1>c2"<<endl;
    if(c1<c2)
        cout<<"c1<c2"<<endl;
    if(c1==c2)
        cout<<"c1=c2"<<endl;
        cout<<endl;
    cout<<"下面进行分数取反:"<<endl;
    c3=+c1;
    cout<<"+c1 "<<c3<<endl;
    c3=-c1;
    cout<<"-c1 "<<c3<<endl;
    c3=~c1;
    cout<<"~c1 "<<c3<<endl;
    return 0;
}

总结:

         在取反运算最后输出时开始写的是 cout<<+x<<endl; 但是出现了许多错误,经过思考尝试写成和前面加减一样的 c3=+c1;
    cout<<"+c1 "<<c3<<endl; 形式,结果问题解决,所以结果不能直接输出。以后遇到错误时要多尝试才有可能改过来。


0
0

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