Question

Program 4 Revised Problem: Write a program that uses the class Rational (below) for performing arithmetic with fractions. The member function definitions for the class rational should include: a. b. c. 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, yl and dx2, 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.

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 << "\nDIVIDE BY ZERO ERROR!!!" << '\n';
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

0 0
Add a comment Improve this question Transcribed image text
Request Professional Answer

Request Answer!

We need at least 10 more requests to produce the answer.

0 / 10 have requested this problem solution

The more requests, the faster the answer.

Request! (Login Required)


All students who have requested the answer will be notified once they are available.
Know the answer?
Add Answer to:
Header file for the Rational class: #ifndef RATIONAL_H #define RATIONAL_H class Rational { public: Rational( int...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Similar Homework Help Questions
  • 13.21 Lab: Rational class This question has been asked here before, but every answer I have...

    13.21 Lab: Rational class This question has been asked here before, but every answer I have tested did not work, and I don't understand why, so I'm not able to understand how to do it correctly. I need to build the Rational.cpp file that will work with the main.cpp and Rational.h files as they are written. Rational Numbers It may come as a bit of a surprise when the C++ floating-point types (float, double), fail to capture a particular value...

  • If you have already answered this question, please do not repost your old solutions, you will be thumbs downed. I'm...

    If you have already answered this question, please do not repost your old solutions, you will be thumbs downed. I'm looking for NEW solutions only! Frac.h: // a Fraction object holds one Fraction number, one fraction #ifndef FRAC_H #define FRAC_H #include <iostream> using namespace std; //Creaing a Fraction class class Fraction { public: Fraction(int = 0, int = 1); // Function Declarations which performs operations on Fraction class Fraction add(const Fraction &); Fraction subtract(const Fraction& a); Fraction multiply(const Fraction& a);...

  • C++ Create a Rational Number (fractions) class like the one in Exercise 9.6 of the textbook....

    C++ Create a Rational Number (fractions) class like the one in Exercise 9.6 of the textbook. Provide the following capabilities: Create a constructor that prevents a 0 denominator in a fraction, reduces or simplifies fractions (by dividing the numerator and the denominator by their greatest common divisor) that are not in reduced form, and avoids negative denominators. Overload the addition, subtraction, multiplication, and division operators for this class. Overload the relational and equality operators for this class. Provide a function...

  • (Rational Numbers) Create a class called Rational for performing arithmetic with fractions. Write a program to...

    (Rational Numbers) Create a class called Rational for performing arithmetic with fractions. Write a program to test your class. Use integer variables to represent the private instance variables of the class- the numerator and the denominator. Provide a constructor that enables an object of this class to be initialized when it's declared. The constructor should store the fraction in reduced form. The fraction 2/4 is equivalent to h and would be stored in the object as 1 in the numerator...

  • Make a new class called Rational to represent rational numbers in C++ language. Use a long...

    Make a new class called Rational to represent rational numbers in C++ language. Use a long to store each part (numerator and denominator). Be sure your main function fully test the class. 1. Create a helper function called gcd to calculate the greatest common divisor for the two given parameters. See Euclid’s algorithm (use google.com). Use this function to always store rationals in “lowest terms”. 2. Create a default, one argument and two argument constructor. For the two argument constructor,...

  • Having to repost this as the last answer wasn't functional. Please help In the following program...

    Having to repost this as the last answer wasn't functional. Please help In the following program written in C++, I am having an issue that I cannot get the reducdedForm function in the Rational.cpp file to work. I cant figure out how to get this to work correctly, so the program will take what a user enters for fractions, and does the appropriate arithmetic to the two fractions and simplifies the answer. Rational.h class Rational { private: int num; int...

  • Please use Java language. Thanks in advance. HOME WORK due 09. 18.2019 (Rational Numbers) Create a...

    Please use Java language. Thanks in advance. HOME WORK due 09. 18.2019 (Rational Numbers) Create a class called Rational for performing arithmetic with fractions. Write a program to test your class. Use integer variables to represent the private instance variables of the class- the numerator and the denominator. Provide a constructor that enables an object of this class to be initialized when it's declared. The constructor should store the fraction in reduced form. The fraction 2/4 is equivalent to 2...

  • In C++ Fix any errors you had with HW5(Fraction class). Implement a function(s) to help with...

    In C++ Fix any errors you had with HW5(Fraction class). Implement a function(s) to help with Fraction addition. \**************Homework 5 code*****************************/ #include<iostream> using namespace std; class Fraction { private: int wholeNumber, numerator, denominator;    public: //get methods int getWholeNumber() { return wholeNumber; } int getNumerator() { return numerator; } int getDenominator() { return denominator; } Fraction()// default constructor { int w,n,d; cout<<"\nEnter whole number : "; cin>>w; cout<<"\nEnter numerator : "; cin>>n; cout<<"\nEnter denominator : "; cin>>d; while(d == 0)...

  • C++ and/or using Xcode... Define a class of rational numbers. A rational number is a number...

    C++ and/or using Xcode... Define a class of rational numbers. A rational number is a number that can be represented as the quotient of two integers. For example, 1/2, 3/4, 64/2, and so forth are all rational numbers. (By 1/2, etc., we mean the everyday meaning of the fraction, not the integer division this expression could produce in a C++ program). Represent rational numbers as two values of type int, one for the numerator and one for the denominator. Call...

  • c++ Write a rational number class. A rational number is a number that can be written...

    c++ Write a rational number class. A rational number is a number that can be written as p/q where p and q are integers. The division is not carried out, only indicated. Thus you should represent rational numbers by two int values, numerator and denominator. Constructors must be present to create objects with any legal values. You should provide constructors to make objects out of pairs of int values; that is, a constructor with two int parameters. Since very int...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT