通过使用具有私有数据成员的Class重载+运算符来添加分数

时间:2018-02-11 04:22:39

标签: c++ class operator-overloading private-members fractions

首先,这是一项家庭作业。我只是需要一些帮助    解决与Fraction运算符+()函数相关的问题。它应该在函数BinarayMathTest中将数组中的两个分数加在一起,但Fraction :: operator +()函数只返回分母。

#include <iostream>
#include <string>

using namespace std;



class Fraction
{
    private:
    int num,denom;
    public:

    Fraction operator + (const Fraction &right)const;

    friend ostream&operator<<(ostream&stream,Fraction obj);
    Fraction(int a=0,int b=1){num=a; denom=b;}

};

ostream&operator<<(ostream&stream,Fraction obj)
{
    stream<<obj.num<<'/';
   stream<<obj.denom;
   return stream;
}

Fraction Fraction::operator+(const Fraction &right)const
{
    Fraction temp;
    Fraction tempo;
    Fraction full;
    temp.num = ((num*right.denom) + (right.num*denom));
    tempo.denom =(denom*right.denom);
    full = (temp,tempo);
    return full;
}


void BinaryMathTest();


int main()
{

    BinaryMathTest();

    return 0;
}



void BinaryMathTest()
{
    cout << "\n----- Testing binary arithmetic between Fractions\n";

    const Fraction fr[] = {Fraction(1, 6), Fraction(1,3),
                       Fraction(-2,3), Fraction(5), Fraction(-4,3)};

    for (int i = 0; i < 4; i++) {
          cout << fr[i] << " + " << fr[i+1] << " = " << fr[i] + fr[i+1]
<<endl;}}



/* OUTPUT
----- Testing binary arithmetic between Fractions
1/6 + 1/3 = 0/18
1/3 + -2/3 = 0/9
-2/3 + 5/1 = 0/3
5/1 + -4/3 = 0/3
*/

2 个答案:

答案 0 :(得分:2)

  

Fraction :: operator +()函数只返回分母。

让我们按照该函数中的代码行进行操作,看看会发生什么。

Fraction temp;  // Creates an object with num = 0, denom = 1
Fraction tempo; //    ditto
Fraction full;  //    ditoo

temp.num = ((num*right.denom) + (right.num*denom)); // Sets the num of temp

tempo.denom =(denom*right.denom);  // Sets the denom of tempo

我有一种感觉,你不知道发生在下一行。

full = (temp,tempo);

该行的RHS是一个使用comma operator的表达式。

评估temp并丢弃该值。它评估tempo并将其分配给full。在该行之后,full只有分母,因为tempo的分子从未从0更改。

// Returns an object whose numerator is zero.
return full;

您可以将该功能简化为:

Fraction Fraction::operator+(const Fraction &right)const
{
   int num = ((num*right.denom) + (right.num*denom));
   int denom = (denom*right.denom);

   return Fraction(num, denom);
}

上述实现中需要注意的一点是,如果您不断添加num个对象,它会快速增加denomFraction的值。您可以延迟/防止整数溢出,您必须将numdenom减少为较小的值,并将它们除以GCD

Fraction Fraction::operator+(const Fraction &right)const
{
   int num = ((num*right.denom) + (right.num*denom));
   int denom = (denom*right.denom);
   int gcd = get_gcd(num, denom);

   return Fraction(num/gcd, denom/gcd);
}

答案 1 :(得分:0)

#include <iostream>
#include <string>

using namespace std;

class Fraction
{
    private:
    int num,denom;
    float full;
    public:

    Fraction operator + (const Fraction &right)const;

    friend ostream&operator<<(ostream&stream,Fraction obj);
    Fraction(int a=0,int b=1){num=a; denom=b;}

};

ostream&operator<<(ostream&stream,Fraction obj)
{
    stream<<obj.num<<'/';
   stream<<obj.denom;
   return stream;
}

Fraction Fraction::operator+(const Fraction &right)const
{
    Fraction temp;

    temp.num = ((num*right.denom) + (right.num*denom));
    temp.denom =(denom*right.denom);
    Fraction full(temp.num,temp.denom); /*objet of class cannot give call to constructor by using = unless it is overloaded*/

    return full;
}


void BinaryMathTest();


int main()
{

    BinaryMathTest();

    return 0;
}



void BinaryMathTest()
{
    cout << "\n----- Testing binary arithmetic between Fractions\n";

    const Fraction fr[] = {Fraction(1, 6), Fraction(1,3),
                       Fraction(-2,3), Fraction(5), Fraction(-4,3)};

    for (int i = 0; i < 4; i++) {
          cout << fr[i] << " + " << fr[i+1] << " = " << fr[i] + fr[i+1]
<<endl;}}