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

时间:2018-05-23 10:44:54

标签: c++ class fractions friend-function

我的要求是使用类添加,乘法,减去和除去有理分数,然后使用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 ;
}

我试着简明扼要但是如果你还需要进一步的代码请在下面评论,我会上传更多内容以方便你。感谢

3 个答案:

答案 0 :(得分:2)

此:

n = r.numerator / r.denominator;

调用整数除法

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

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

答案 1 :(得分:1)

您要将intint分开。这会使int后来投放到float。首先将您的int转换为float

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

答案 2 :(得分:0)

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 ;

}