Search code examples
c++classfractionsfriend-function

How to convert fractions into floating points in c++


My requirement is to add,multiply,subtract and divide rational fractions using classes then implement a standalone(nonclass) function void printRationalAsFloating(const Rational &r) using friend function for printing the passed Rational numbers into floating point numbers. My output is fine with multiplication but with addition is wrong. I don't know if something is wrong with either friend function or with printRationalAsFloating.

my output:

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 << "  ";

header file:

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;
};

here is my coding:

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 ;
}

I tried to make it concise but if you still need further code please comment below and i will upload more for your ease. Thanks


Solution

  • 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 ;
    
    }