如何在C++中将分数转换为浮点数

How to convert fractions into floating points in c++

我的要求是使用 类 加、乘、减和除有理分数,然后使用 friend 函数实现独立(非类)函数 void printRationalAsFloating(const Rational &r) 来打印传递的 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 ;
}

我尽量使其简洁,但如果您仍需要更多代码,请在下方评论,我会上传更多代码以供您使用。谢谢

这个:

n = r.numerator / r.denominator;

调用整数除法

尝试将至少一个操作数转换为 double,如下所示:

n = r.numerator / (double) r.denominator;

您将 int 除以 int。这给出了一个 int 后来转换为 float。首先将您的 int 转换为 float

float n = r.numerator / (float)r.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 ;

}