Question
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
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started