Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Question in C++, use the header file code posted below: Header file for the Rati

ID: 652794 • Letter: Q

Question

Question in C++, use the header file code posted below:

Header file for the Rational class:


#ifndef RATIONAL_H
#define RATIONAL_H


class Rational
{
public:
Rational( int = 0, int = 1 ); // default constructor
Rational addition( const Rational & ) const; // function addition
Rational subtraction( const Rational & ) const; // function subtraction
Rational multiplication( const Rational & ) const; // function multi.
Rational division( const Rational & ) const; // function division
void printRational () const; // print rational format
void printRationalAsDouble() const; // print rational as double format
private:
int numerator; // integer numerator
int denominator; // integer denominator
void reduce(); // utility function
}; // end class Rational

#endif

Rational Class:


// Member-function definitions for class Rational.
#include "stdafx.h"
#include
#include "Rational.h" // include definition of class Rational
using namespace std;
Rational::Rational(int n, int d)
: numerator(n), denominator(d)
{
reduce(); // store the fraction in reduced form
} // end Rational constructor
Rational Rational::addition(const Rational &a) const
{
Rational t(a.numerator * denominator + a.denominator * numerator,
a.denominator * denominator);
t.reduce(); // store the fraction in reduced form
return t;
} // end function addition
Rational Rational::subtraction(const Rational &s) const
{
Rational t(s.denominator * numerator - denominator * s.numerator,
s.denominator * denominator);
t.reduce(); // store the fraction in reduced form
return t;
} // end function subtraction
Rational Rational::multiplication(const Rational &m) const
{
Rational t(m.numerator * numerator, m.denominator * denominator);
t.reduce(); // store the fraction in reduced form
return t;
} // end function multiplication
Rational Rational::division(const Rational &v) const
{
Rational t(v.denominator * numerator, denominator * v.numerator);
t.reduce(); // store the fraction in reduced form
return t;
} // end function division
void Rational::printRational() const
{
if (denominator == 0) // validates denominator
cout << " DIVIDE BY ZERO ERROR!!!" << ' ';
else if (numerator == 0) // validates numerator
cout << 0;
else
cout << numerator << '/' << denominator;
} // end function printRational
oid Rational::printRationalAsDouble() const
{
cout << static_cast< double >(numerator) / denominator;
} // end function printRationalAsDouble
void Rational::reduce()
{
int largest = numerator > denominator ? numerator : denominator;
int gcd = 0; // greatest common divisor
for (int loop = 2; loop <= largest; ++loop)
if (numerator % loop == 0 && denominator % loop == 0)
gcd = loop;
if (gcd != 0)
{
numerator /= gcd;
denominator /= gcd;
} // end if
} // end function reduction

Write a program that uses the class Rational (below) for performing arithmetic with fractions. The member function definitions for the class rational should include: Create a constructor that accepts two integer arguments. Create addition, subtraction, multiplication, and division operators for this class. Create void functions that check for a zero denominator, cast the rational number as a double, and reduce the fraction. (printRational, printRationalAsDouble, and reduce) Solution: The main or demonstration program should create three rational objects. You will need to offer a menu and ask for user input for two objects, c = x1, y1 and d = x2, y2. The third object should be called x and will be used to store the result of the operation. The menu should offer addition, subtraction, multiplication and division with -1 to quit. Be sure to display the results on the console in reduced form. A maximum of 10 points extra credit is available for error checking the user input.

Explanation / Answer

#include<iostream>
#include"rational.h"

using namespace std;

int main()
{
   // create two objects c and d , third object called x
   Rational x(0,0),c(0,0),d(0,0);
   // varibale to hold user Choice
   int choice;
   //variable to hold user variable
   int x1,y1,x2,y2;
  
   cout<<"############ Program to perform arithmetic with rational numbers#####################"<<endl;
   do
   {
       cout<<"Enter choice 1: Addition, 2: Subtraction , 3: Multiplication,4: Division ,Quit: -1"<<endl;
       cin>>choice;
       if( choice > 0 && choice !=5 )
       {
           cout<<"Enter values for c and d rational numbers in the form c= x1, y1, d= x2, y2"<<endl;
           cout<<"Enter value for c:" ;
           cin>>x1>>y1;
           cout<<"Enter value for d:" ;
           cin>>x2>>y2;
       }
       Rational c(x1,y1);
       Rational d(x2,y2);
       c.printRational();
       d.printRational();
       switch(choice)
       {
          
       case 1:
           cout<<"Addition of rational numbers "<<endl;
           x=c.addition(d);  
           cout<<"Print the result : ";
           x.printRational();
           break;
       case 2:
           cout<<"Subtraction of rational numbers "<<endl;
           x=c.subtraction(d);
           cout<<"Print the result : ";
           x.printRational();
           break;
       case 3:
           cout<<"Multiplication of rational numbers "<<endl;
           x=c.multiplication(d);
           cout<<"Print the result : ";
           x.printRational();
           break;
          
       case 4:
           cout<<"Division of rational numbers "<<endl;
           x=c.division(d);
           cout<<"Print the result : ";
           x.printRational();
           break;
          
       case -1:
           cout<<"Quit..."<<endl;  
           break;
       default:
           cout<<"Enter choice 1: Addition, 2: Subtraction , 3: Multiplication,4: Division, Quit: -1"<<endl;
           break;

       }
   }while(choice>0);

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote