Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

The header #ifndef RATIONAL_H #define RATIONAL_H class Rational { public: Rational(int = 0, int = 1); // default constructor Rational addition(const Rational &); Rational subtraction(const

The header

#ifndef RATIONAL_H

#define RATIONAL_H

class Rational

{

public:

Rational(int = 0, int = 1); // default constructor

Rational addition(const Rational &);

Rational subtraction(const Rational &);

Rational multiplication(const Rational &);

Rational division(const Rational &);

void printRational(void);

void printRationalAsDouble(void);

private:

int numerator;

int denominator;

void reduction(void); // utility function 18

};

#endif

#include

#include "Rational.h"

using namespace std;

int main()

{

Rational c(1, 8), d(3, 8), x;

c.printRational();

cout << " + ";

d.printRational();

x = c.addition(d);

cout << " = ";

x.printRational();

cout << ' ';

x.printRational();

cout << " = ";

x.printRationalAsDouble();

cout << " ";

c.printRational();

cout << " - ";

d.printRational();

x = c.subtraction(d);

cout << " = ";

x.printRational();

cout << ' ';

x.printRational();

cout << " = ";

x.printRationalAsDouble();

cout << " ";

c.printRational();

cout << " x ";

d.printRational();

x = c.multiplication(d);

cout << " = ";

x.printRational();

cout << ' ';

x.printRational();

cout << " = ";

x.printRationalAsDouble();

cout << " ";

c.printRational();

cout << " / ";

d.printRational();

x = c.division(d);

cout << " = ";

x.printRational();

cout << ' ';

x.printRational();

cout << " = ";

x.printRationalAsDouble();

cout << endl;

system("pause");

return 0;

}

#include

#include "Rational.h"

#include

using namespace std;

Rational::Rational(int a, int b)

{

numerator = a;

denominator = b;

}

Rational Rational::addition(const Rational & a)

{

int Denominator = denominator * a.denominator;

int Numerator = numerator * a.denominator + denominator * a.numerator;

Rational result(Numerator, Denominator);

result.reduction();

return result;

}

Rational Rational::subtraction(const Rational & a)

{

int Denominator = denominator * a.denominator;

int Numerator = numerator * a.denominator - denominator * a.numerator;

Rational result(Numerator, Denominator);

result.reduction();

return result;

}

Rational Rational::multiplication(const Rational & a)

{

int Denominator = denominator * a.denominator;

int newNumerator = numerator * a.numerator;

Rational result(newNumerator, Denominator);

result.reduction();

return result;

}

Rational Rational::division(const Rational & a)

{

int Denominator = denominator * a.numerator;

int Numerator = numerator * a.denominator;

Rational result(Numerator, Denominator);

result.reduction();

return result;

}

void Rational::printRational()

{

cout << numerator << "/" << denominator << " (" << setprecision(2)

<< numerator*1.0 / denominator << ") " << endl;

}

void Rational::printRationalAsDouble()

{

cout <<

}

void Rational::reduction()

{

int largest;

int gcd = 0;

largest = numerator > denominator ? numerator : denominator;

for (int i = 2; i <= largest; i++)

if (numerator % i == 0 && denominator % i == 0)

gcd = i;

if (gcd != 0)

{

numerator /= gcd;

denominator /= gcd;

}

}

How do I corrent this program?

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Structured Search For Big Data From Keywords To Key-objects

Authors: Mikhail Gilula

1st Edition

012804652X, 9780128046524

Students also viewed these Databases questions

Question

What about leadership lessons from particularly good or bad bosses?

Answered: 1 week ago

Question

When would you use one approach, and when would you use another?

Answered: 1 week ago