我的要求是使用类对有理分数进行加,减,除,然后使用void printRationalAsFloating(const Rational &r)函数实现独立的(非类)函数friend,以将传递的Rational数字打印为浮点数。我的输出使用multiplication很好,但是使用addition是错误的。我不知道friend functionprintRationalAsFloating是否出了问题。

我的输出:

Testing addition : 1/2 * 1/2 = 4/4
Rational as floating:1                //it should be something like 1.00
Testing multiplication : 1/2 * 1/2 = 1/4
Rational as floating:0.25

int main();
cout<<"Testing addition : ";
                    n1 = n2.addition(n3);
                    n2.printRational();
                    cout <<" + ";
                    n3.printRational();
                    cout <<" = ";
                    n1.printRational();
                    cout << endl;
                    cout << "Rational as floating:";
                    printRationalAsFloating(n1);
                    cout << endl;
                    Rational n1;
                    Rational n2(1,2);
                    Rational n3(n2);
                    cout<<"Testing multiplication : ";
                    n1 = n2.multiplication(n3);
                    n2.printRational();
                    cout <<" * ";
                    n3.printRational();
                    cout <<" = ";
                    n1.printRational();
                    cout << endl;
                    cout << "Rational as floating:";
                    printRationalAsFloating(n1);
                    cout << "  ";

头文件:
using namespace std;

class Rational {

  public:
    Rational();        // default constructor
    Rational(int, int); //std (initialisation) constructor
    Rational(const Rational&); //copy constructor
    Rational multiplication(const Rational &);
    Rational addition(const Rational &);
    friend void printRationalAsFloating(const Rational &);
    void printRational();
  private:
   int numerator;
   int denominator;
};

这是我的编码:
void printRationalAsFloating(const Rational &r)
{
    float numerator,n;
    float denominator;
    if (r.denominator==0)
    {
        string exceptionString = "\n\nError: 'Cannot divide by zero'.\n";
        throw exceptionString;
        cout << "ERROR: ATTEMPTING TO DIVIDE BY ZERO" << endl;
        exit(0); // will terminate the program if division by 0 is attempted
    }
    n = r.numerator / r.denominator;

    cout << n ;

}

using namespace std;
//addition
Rational Rational::addition(const Rational &a)
{
      return Rational(numerator * a.denominator + a.numerator * denominator,
      denominator * a.denominator);
}
//multiplication
Rational Rational::multiplication(const Rational &a)
{
   return Rational((numerator * a.numerator) ,denominator * a.denominator);
}

//display fraction
void Rational::printRational()
{
    cout << numerator << "/" << denominator ;
}

我试图使其简洁,但是如果您仍然需要更多代码,请在下面评论,为了您的方便,我将上传更多内容。谢谢

最佳答案

void printRationalAsFloating(const Rational &r)
{
float numerator,n,denominator;
if (r.denominator==0)
    {
        string exceptionString = "\n\nError: 'The denominator of a rational cannot     be zero'.\n";
        throw exceptionString;
        //cout << "ERROR: ATTEMPTING TO DIVIDE BY ZERO" << endl;
        //exit(0); // will terminate the program if division by 0 is attempted
    }
n = (float)r.numerator / r.denominator;

cout << n ;

}

关于c++ - 如何在C++中将分数转换为浮点数,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/50486377/

10-12 20:08