Create a class called Rational for performing arithmetic wit

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 1/2 and would be stored in the object as 1 in the numerator and 2 in the denominator. Provide a no-argument constructor with default values in case no initializers are provided. Provide public methods that perform each of the following operations: Add two Rational numbers: The result of the addition should be stored in reduced form. Implement this as a static method. Subtract two Rational numbers: The result of the subtraction should be stored in reduced form. Implement this as a static method. Multiply two Rational numbers: The result of the multiplication should be stored in reduced form. Implement this as a static method. Divide two Rational numbers: The result of the division should be stored in reduced form. Implement this as a static method. Return a String representation of a Rational number in the form a/b, where a is the numerator and b is the denominator. Return a String representation of a Rational number in floating-point format. (Consider providing formatting capabilities that enable the user of the

Solution

#include <iostream>
using namespace std;

class Rational
{
private:
int numerator;
int denominator;
public:
Rational() //default constructor
{
this->numerator =0;
this->denominator = 0;
}
Rational(int numerator,int denominator) //parameterized constructor
{
this->numerator=numerator;
this->denominator=denominator;
reduce();
}
Rational reduce() //reduce function
{
Rational temp;
int x,y,z;
x=numerator;
y=denominator;
z=(x*x<y*y)?(z=x):(z=y);
for(int i=2;i*i<=z*z;i++)
{
while((x%i)==0 && (y%i)==0)
{
x=x/i;
y=y/i;
z=z/i;
}
}
if(y<0)
{
temp.numerator=-x;
temp.denominator=-y;
}
else
{
temp.numerator=x;
temp.denominator=y;
}
return temp;
}
static Rational Add(Rational r1,Rational r2) //static add method
{
Rational temp;
temp.numerator=r1.numerator*r2.denominator+r2.numerator*r1.denominator;
temp.denominator=r1.denominator*r2.denominator;
temp.reduce();
return temp;
}
static Rational Sub(Rational r1,Rational r2) //static subtract method
{
Rational temp;
temp.numerator=r1.numerator*r2.denominator-r2.numerator*r1.denominator;
temp.denominator=r1.denominator*r2.denominator;
temp.reduce();
return temp;
}
static Rational Mul(Rational r1,Rational r2) //static multiply method
{
Rational temp;
temp.numerator=r1.numerator*r2.numerator;
temp.denominator=r1.denominator*r2.denominator;
temp.reduce();
return temp;
  
}
static Rational Div(Rational r1,Rational r2) //static division method
{
Rational temp;
temp.numerator=r1.numerator*r2.denominator;
temp.denominator=r1.denominator*r2.numerator;
temp.reduce();
return temp;
}

void display() //display in fraction
{
cout<<numerator<<\"/\"<<denominator;
}
void displayFloat() //display in floating point
{
   cout<<\"\\t In decimal :\"<<(float)numerator/(float)denominator;
  
}
};

int main()
{
   Rational r1(3,5);
   Rational r2(7,6);
   Rational result1,result2,result3,result4;
   cout<<\"r1 : \";
   r1.display();
   cout<<\"\ r2 : \";
   r2.display();
  
   cout<<\"\ Adding r1 and r2 : \";
   result1 = result1.Add(r1,r2);
   result1.display();
   result1.displayFloat();
   cout<<\"\ Subtracting r2 from r1: \";
   result2= result2.Sub(r1,r2);
   result2.display();
   result2.displayFloat();
   cout<<\"\ Multiplying r1 and r2 : \";
   result3=result3.Mul(r1,r2);
   result3.display();
   result3.displayFloat();
   cout<<\"\ Dividing r1 and r2 : \";
   result4=result4.Div(r1,r2);
   result4.display();
   result4.displayFloat();
  
  
  
   return 0;
}

output:

 Create a class called Rational for performing arithmetic with fractions. Write a program to test your class. Use integer variables to represent the private ins
 Create a class called Rational for performing arithmetic with fractions. Write a program to test your class. Use integer variables to represent the private ins
 Create a class called Rational for performing arithmetic with fractions. Write a program to test your class. Use integer variables to represent the private ins

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site