In: Computer Science
Introduction: Real work is in end. I will give you thumps up for your work.
Lab2:
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:
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
Lab 2's Ans:
#include <iostream>
#include <sstream> // 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
Rational::Rational(int a, int b)
{
this->numerator = a;
this->denominator = b;
}
//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
Rational Rational::subtract(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 multiply
//Your code
Rational Rational::multiply(const Rational a) const
{
Rational t(numerator * a.numerator, denominator *
a.denominator);
t.reduce(); // store the fraction in reduced form
return t;
}
//Define the function divide
//Your code
Rational Rational::divide(const Rational a) const
{
Rational t(numerator * a.denominator, denominator *
a.numerator);
t.reduce(); // store the fraction in reduced form
return t;
}
string Rational::toRationalString() const
{
if (denominator == 0) { // validates denominator
return "\nDIVIDE 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<double>(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(1, 3);
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()
<< '\n';
cout << x.toRationalString() << " = " <<
x.toDouble() << "\n\n";
//Write code to subtract Rational object d from c ( c-d) and
display the output similar to the format for adding to Rational
//objects above
x = c.subtract(d); // adds object c and d; sets the value to
x
cout << c.toRationalString() << " - " <<
d.toRationalString() << " = " << x.toRationalString()
<< '\n';
cout << x.toRationalString() << " = " <<
x.toDouble() << "\n\n";
//Write code to multiply Rational objects c and d, and display
the output similar to the format for adding to Rational //objects
above
x = c.multiply(d); // adds object c and d; sets the value to
x
cout << c.toRationalString() << " * " <<
d.toRationalString() << " = " << x.toRationalString()
<< '\n';
cout << x.toRationalString() << " = " <<
x.toDouble() << "\n\n";
//Write code to divide Rational object c by d (c/d) and display
the output 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
x = c.divide(d); // adds object c and d; sets the value to x
cout << c.toRationalString() << " / " <<
d.toRationalString() << " = " << x.toRationalString()
<< '\n';
cout << x.toRationalString() << " = " <<
x.toDouble() << "\n\n";
}
______________________________________________________________________________
Now you job is to do rest:
Redo the lab 2 (RationalNumber class) to provide the following capabilities:
Attached is a partial lab7.cpp program. You need to write code for the following operator overloaded functions:
Note: Could you plz go through this code and let me
know if u need any changes in this.Thank You
=================================
#include <iostream>
#include <sstream> // for ostringstream class
using namespace std;
class Rational {
public:
Rational(int = 0, int = 1); // default constructor
Rational operator+(const Rational) const;
Rational operator-(const Rational) const;
Rational operator*(const Rational) const;
Rational operator/(const Rational) const;
/**
* Friend function declarations
* Output Rational number to an output stream, Example 3+4i
*/
friend std::ostream & operator << (std::ostream &
dout,const Rational & );
/**
* read Rational number to an input stream
*/
friend std::istream & operator >> (std::istream &
din, Rational & );
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
Rational::Rational(int a, int b)
{
this->numerator = a;
this->denominator = b;
}
//I provided the code for the function add
Rational Rational::operator+(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
Rational Rational::operator-(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 multiply
//Your code
Rational Rational::operator*(const Rational a) const
{
Rational t(numerator * a.numerator, denominator *
a.denominator);
t.reduce(); // store the fraction in reduced form
return t;
}
//Define the function divide
//Your code
Rational Rational::operator/(const Rational a) const
{
Rational t(numerator * a.denominator, denominator *
a.numerator);
t.reduce(); // store the fraction in reduced form
return t;
}
// Function implementation which display Rational numbers by
using the operator overloading '<<'
ostream& operator<<(ostream& dout, const
Rational& c)
{
dout <<"("<< c.numerator << "/" <<
c.denominator<<")";
return dout;
}
// Function implementation which read Rational numbers by using the
operator overloading '>>'
istream& operator>>(istream& din, Rational&
c)
{
char ch;
din >> c.numerator;
din >> ch;
din >> c.denominator;
return din;
}
double Rational::toDouble() const
{
return static_cast<double>(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,d;
cout<<"Rational Number#1:";
cin>>c;
cout<<"Rational Number#2:";
cin>>d;
Rational x = c+d; // adds object c and d; sets the value to x
cout << c<< " + " << d<< " = " <<
x<< '\n';
cout << x<< " = " << x.toDouble() <<
"\n\n";
//Write code to subtract Rational object d from c ( c-d) and
display the output similar to the format for adding to Rational
//objects above
x = c-d; // adds object c and d; sets the value to x
cout << c<< " - " << d<< " = " <<
x<< '\n';
cout << x<< " = " << x.toDouble() <<
"\n\n";
//Write code to multiply Rational objects c and d, and display
the output similar to the format for adding to Rational //objects
above
x = c*d; // adds object c and d; sets the value to x
cout << c<< " * " << d<< " = " <<
x<< '\n';
cout << x<< " = " << x.toDouble() <<
"\n\n";
//Write code to divide Rational object c by d (c/d) and display
the output 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
x = c/d; // adds object c and d; sets the value to x
cout << c << " / " << d<< " = " <<
x<< '\n';
cout << x<< " = " << x.toDouble() <<
"\n\n";
}
======================================
======================================
Output:
=====================Could you plz rate me well.Thank You