Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Create a class called Rational for performing arithmetic with fractions. Use the integer variables to represent the private data of the class - the numerator
Create a class called Rational for performing arithmetic with fractions. Use the integer variables to represent the private data of the class - the numerator and the denominator. Provide a constructor that enables an object of this class to initialized when it's declared. The constructor should contain default values in case no initializers are provided and should store the fraction in reduced form. For example, the fraction 2/4 would be stored in the object as 1 in the numerator and 2 in the denominator. Provide public member functions that perform each of the following tasks:
add - Adds to Rational numbers. The result should be stored in a reduced form.
subtract - Subtract two Rational numbers. Store the result in reduced form.
multiply - Multiplies two Rational numbers. Store the result in reduced form.
divide - Divides two Rational numbers. The result should be stored in a reduced form.
toRationalString - Return a string representation of a Rational number in the form a/b, where a is the numerator and b is the denominator.
toDouble - Returns the Rational number as double
Output sample:
1/3 + 7/8 = 29/24
29/24 = 1.20833
1/3 - 7/8 = -13/24
-13/24 = -0.541667
1/3 + 7/8 = 7/24
7/24 = 0.291667
1/3 + 7/8 = 8/21
8/21 = 0.380952
Steps on how to do the lab assignment 2.
In this lab assignment, you put all the code including the main function in one file called lab2.
#include
#include // for ostringstream class
using namespace std;
class Rational {
public:
Rational(int = 0, int = 1); // default constructor
Rational add(const Rational) const;
Rational subtract(const Rational) const;
Rational multiply(const Rational) const;
Rational divide(const Rational) const;
std::string toRationalString() const; // return as string format
double toDouble() const; // return rational as double
private:
int numerator; // integer numerator
int denominator; // integer denominator
void reduce(); // utility function
};
//Define the two-argument constructors
//Your code
//I provided the code for the function add
Rational Rational::add(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;
}
//Define the function subtract
//Your code
//Define the function multiply
//Your code
//Define the function divide
//Your code
string Rational::toRationalString() const {
if (denominator == 0) { // validates denominator
return " DIVIDE BY ZERO ERROR!!!";
}
else if (numerator == 0) { // validates numerator
return "0";
}
else {
ostringstream output;
output << numerator << '/' << denominator;
return output.str();
}
}
double Rational::toDouble() const {
return static_cast(numerator) / denominator;
}
//The function reduce is provided for you. This function reduces the fraction.
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;
}
}
int main() {
Rational c(2,6);
Rational d(7,8);
Rational x = c.add(d); // adds object c and d; sets the value to x
cout << c.toRationalString() << " + " << d.toRationalString() << " = " << x.toRationalString() << ' ';
cout << x.toRationalString() << " = " << x.toDouble() << " ";
//Write code to subtract Rational object d from c ( c-d) and display the output. The output is similar to the format for adding to Rational objects above.
//Write code to multiply Rational objects c and d, and display the output. The output is similar to the format for adding to Rational objects above.
//Write code to divide Rational object c by d (c/d) and display the output. The output is similar to the format for adding to Rational objects above.
//Write code to display the result of dividing c by d in the double format as shown in the output sample above.
}
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