关闭

项目37-分数类中的运算符重载

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

 (1)实现分数类中的运算符重载,在分数类中可以完成分数的加减乘除(运算后再化简)、比较(6种关系)的运算。

<code class="hljs vala has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-class" style="box-sizing: border-box;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">class</span> <span class="hljs-title" style="box-sizing: border-box; color: rgb(102, 0, 102);">CFraction</span>
{</span>
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">private</span>:
    <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> nume;  <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 分子</span>
    <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> deno;  <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 分母</span>
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">public</span>:
    <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//构造函数及运算符重载的函数声明</span>
};
<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//重载函数的实现及用于测试的main()函数</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li></ul>

  (2)在(1)的基础上,实现分数类中的对象和整型数的四则运算。分数类中的对象可以和整型数进行四则运算,且运算符合交换律。例如:CFraction a(1,3),b; int i=2; 可以完成b=a+i;。同样,可以完成i+a, 45+a, a*27, 5/a等各种运算。 
  (3)定义分数的一目运算+和-,分别代表分数取正和求反,将“按位取反运算符”~重载为分数的求倒数运算。 

  (4)定义

运行代码:

#include <iostream>
using namespace std;
class CFraction
{
private:
    int nume;  // 分子
    int deno;  // 分母
public:
    CFraction(int nu=0,int de=1):nume(nu),deno(de) {}
    void simplify();
    void set(int n,int d);
    CFraction operator+(const CFraction &c);
    CFraction operator-(const CFraction &c);
    CFraction operator*(const CFraction &c);
    CFraction operator/(const CFraction &c);
    bool operator>(const CFraction &c);
    bool operator<(const CFraction &c);
    bool operator==(const CFraction &c);
    bool operator!=(const CFraction &c);
    bool operator>=(const CFraction &c);
    bool operator<=(const CFraction &c);
    CFraction operator+();
    CFraction operator-();
    CFraction operator~();
    friend ostream &operator<<(ostream &out,CFraction c);
    friend istream &operator>>(istream &input,CFraction c);
};
void CFraction::set(int n,int d)
{
    nume=n;
    deno=d;
}
ostream &operator<<(ostream &out,CFraction x)
{
    out<<x.nume<<'/'<<x.deno;
    return out;
}
istream &operator>>(istream &input,CFraction c)
{
    char a;
    input>>c.nume>>a>>c.deno;
    return input;
}
CFraction CFraction:: operator+()
{
    return *this;
}

CFraction CFraction:: operator-()
{
    CFraction x;
    x.nume=-nume;
    x.deno=deno;
    return x;
}

CFraction CFraction:: operator~()
{
    CFraction x;
    x.nume=deno;
    x.deno=nume;
    if(x.deno<0)
    {
        x.deno=-x.deno;
        x.nume=-x.nume;
    }
    return x;
}
void CFraction::simplify()
{
    int i,j;
    i=(deno>0)?deno:(-deno);//让分子分母都变为正
    j=(nume>0)?nume:(-nume);
    int k;
    while(k=j%i)//求最大公约数
    {
        j=i;
        i=k;
    }
    deno/=i;
    nume/=i;
}
CFraction CFraction::operator+(const CFraction &c)
{
    CFraction x;
    x.nume=nume*c.deno+deno*c.nume;
    x.deno=deno*c.deno;
    x.simplify();
    return x;
}
CFraction CFraction::operator-(const CFraction &c)
{
    CFraction x;
    x.nume=nume*c.deno-deno*c.nume;
    x.deno=deno*c.deno;
    x.simplify();
    return x;
}
CFraction CFraction::operator*(const CFraction &c)
{
    CFraction x;
    x.nume=nume*c.nume;
    x.deno=deno*c.deno;
    x.simplify();
    return x;
}
CFraction CFraction::operator/(const CFraction &c)
{
    CFraction x;
    x.nume=nume*c.deno;
    x.deno=deno*c.nume;
    x.simplify();
    return x;
}
bool CFraction::operator>(const CFraction &c)
{
    int this_nume,c_nume,common_deno;
    this_nume=nume*c.deno;        // 计算分数通分后的分子,同分母为deno*c.deno
    c_nume=c.nume*deno;
    common_deno=deno*c.deno;
    if ((this_nume-c_nume)*common_deno>0) return true;
    return false;
}
bool CFraction::operator<(const CFraction &c)
{
    int this_nume,c_nume,common_deno;
    this_nume=nume*c.deno;
    c_nume=c.nume*deno;
    common_deno=deno*c.deno;
    if ((this_nume-c_nume)*common_deno<0) return true;
    return false;
}
bool CFraction::operator==(const CFraction &c)
{
    if (*this!=c) return false;
    return true;
}
bool CFraction::operator!=(const CFraction &c)
{
    if (*this>c || *this<c) return true;
    return false;
}
bool CFraction::operator>=(const CFraction &c)
{
    if (*this<c) return false;
    return true;
}

bool CFraction::operator<=(const CFraction &c)
{
    if (*this>c) return false;
    return true;
}
int main()
{

    CFraction x,y,s;
    x.set(1,4);
    y.set(1,5);
    s=x+y;
    cout<<s<<endl;
    cout<<"x的倒数: "<<~x<<endl;
    if (x>y) cout<<"大于";
    if (x<y) cout<<"小于";
    if (x==y) cout<<"等于";
    return 0;
}
运行结果:


0
0

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