Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and the Yahoo Answers website is now in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.

how would I print rational numbers in C++?

I just need to define printRational() and printRationalAsDouble()

everything else seems to work just fine......not sure how to print a fraction though

here is what I got so far.......

#ifndef RATIONAL_H

#define RATIONAL_H

#

using std::ostream;

#

using std::istream;

#

#

class Rational{

public:

rational(int num, int den);

rational(int num);

rational();

int getnum();

int getden();

void input(istream& in);

void output(ostream& out);

bool less(Rational r);

Rational neg();

Rational addition(Rational r);

Rational subtraction(Rational r);

Rational multiplication(Rational r);

Rational division(Rational r);

private:

int numer;

int denom;

};

#include "Rational.h"

#include <iostream>

#include <cmath>

using namespace std;

int Rational::getnum()

{

return numer;

}

int Rational::getden()

{

return denom;

}

Rational::rational(int num, int den)

{

numer = num;

denom = den;

}

Rational::rational(int num)

{

numer = num;

denom = 1;

}

Rational::rational()

{

numer = 1;

denom = 1;

}

Rational::rational addition(Rational r)

{

Rational l;

int a = l.getnum();

int b = l.getden();

int c = r.getnum();

int d = r.getden();

Rational result = ((a * d + b * c) / (b * d));

return result;

}

Rational::rational subtraction(Rational r)

{

Rational l;

int a = l.getnum();

int b = l.getden();

int c = r.getnum();

int d = r.getden();

Rational result = ((a * d - b * c) / (b * d));

return result;

}

Rational::rational multiplication(Rational r)

{

Rational l;

int a = l.getnum();

int b = l.getden();

int c = r.getnum();

int d = r.getden();

Rational result = ((a * b) / (c * d));

return result;

}

Rational::rational division(Rational r, Rational l)

{

int a = l.getnum();

int b = l.getden();

int c = r.getnum();

int d = r.getden();

Rational result = ((a * d) / (c * b));

return result;

}

bool Rational::less(Rational r)

{

Rational l;

int a = l.getnum();

int b = l.getden();

int c = r.getnum();

int d = r.getden();

if ((a * d)<(c * b))

return true;

else

return false;

}

void Rational::input(istream& in)

{

int numer;

int denom;

char slash;

in >> numer >> slash >> denom;

return;

}

void Rational::output(ostream& out)

{

cout<<numer<<"/"<<denom;

}

this is my tester

#include "Rational.h" // include definition of class Rational

#include <iostream>

using namespace std;

int main()

{

Rational c( 2, 6 ), d( 7, 8 ), x; // creates three rational objects

c.printRational(); // prints rational object c

cout << " + ";

d.printRational(); // prints rational object d

x = c.addition( d ); // adds object c and d; sets the value to x

cout << " = ";

x.printRational(); // prints rational object x

cout << '\n';

x.printRational(); // prints rational object x

cout << " = ";

x.printRationalAsDouble(); // prints rational object x as double

cout << "\n\n";

c.printRational(); // prints rational object c

cout << " - ";

d.printRational(); // prints rational object d

x = c.subtraction( d ); // subtracts object c and d

cout << " = ";

x.printRational(); // prints rational object x

cout << '\n';

x.printRational(); // prints rational object x

cout << " = ";

x.printRationalAsDouble(); // prints rational object x as double

cout << "\n\n";

c.printRational(); // prints rational object c

cout << " x ";

d.printRational(); // prints rational object d

x = c.multiplication( d ); // multiplies object c and d

cout << " = ";

x.printRational(); // prints rational object x

cout << '\n';

x.printRational(); // prints rational object x

cout << " = ";

x.printRationalAsDouble(); // prints rational object x as double

cout << "\n\n";

c.printRational(); // prints rational object c

cout << " / ";

d.printRational(); // prints rational object d

x = c.division( d ); // divides object c and d

cout << " = ";

x.printRational(); // prints rational object x

cout << '\n';

x.printRational(); // prints rational object x

cout << " = ";

x.printRationalAsDouble(); // prints rational object x as double

cout << endl;

system("pause");

return 0;

} // end main

Update:

this is what i want it to print

toRational() = 1/3 - 7/8 = -13/2

RationalToDouble() = -13/24 = -0.541667

3 Answers

Relevance
  • 1 decade ago
    Favorite Answer

    You already have the fraction form written. Just call r.output(cout) to display a Rational r on cout. As in:

    Rational a(1,3), b(7,8);

    cout << "toRational() = ";

    a.output(cout);

    cout << " - ";

    b.output(cout);

    cout << " = ";

    a.subtraction(b).output();

    cout << endl;

    If you want to convert to double, use something like:

    double RationalToDouble(Rational r)

    {

    return (double)r.getnum / r.getden();

    }

    ...though this should really be a method of the Rational class.

    Have fun. C++ isn't the best language to learn object programming. The C-style separation of declaration and definition has some advantages for large and complicated classes, but it can be confusing at the beginning to have to look back-and-forth to find the different pieces of the class.

  • 1 decade ago

    Something along these lines:

    cout << numer << '/' << denom;

    But I can't see how you'd use doubles. If the value is a rational number, how would you have fractional values in numer and denom? That would be like 2.5/7.345. Doesn't make sense.

    Hope that helps.

  • ?
    Lv 7
    1 decade ago

    I was going to suggest printf %f, but I guess you want them as a fraction. You'd have to convert to a fraction by shifting decimal points, then factorize to reduce to the simplest form.

Still have questions? Get your answers by asking now.