In: Computer Science
use c++
Provide a header file as well. So for this question provide Rational.h, Rantional.cpp
Create a class called Rantional for performing arithmatic with
fractions. Then write a program to test your class. Use integer
variables to represent the private data of the class, meaning 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 contain default values in case no initializers
are provided and should store the fraction in reduced form. For
example fraction 2/4 would be stored in the object as 1 for the
numerator and 2 for the denominator.
Note: Remember that denominator cannot be 0.
Provide public member functions that perform each of the following
tasks:
a) add -- Adds 2 rational numbers. Result should be stored in
reduced form.
b) subtract -- Subtracts 2 rational numbers. Store result in
reduced form.
c) multiply -- Multiplies 2 rational numbers. Store result in
reduced form.
Write a main() function to test above functionalites.
Post your output
// I HOPE I COULD HELP :)
OUTPUT
//CODE
#include<iostream>
#include<cstdlib>
using namespace std;
class Rational{
int numerator;
int denominator;
public:
Rational()
{
numerator=0;
denominator=1;
}
Rational(int n,int d)
{
numerator=n;
denominator=d;
}
bool isdenomZero()
{
if(denominator==0)
return true;
else
return false;
}
void reduce(int &n,int
&d)
{
int
div=1;
int small;
if(abs(n)<abs(d))
{
small=abs(n);
}
else
small=abs(d);
for(int i=small;i>=1;i--)
{
if(n%i==0&&d%i==0)
{
div=i;
break;
}
}
n=n/div;
d=d/div;
}
Rational add(Rational
f2)
{
Rational result;
int
n1=this->numerator;
int
d1=this->denominator;
int
n2=f2.numerator;
int
d2=f2.denominator;
int
result_denom=d1*d2;
int
result_numer=n1*d2+n2*d1;
reduce(result_numer,result_denom);
result.numerator=result_numer;
result.denominator=result_denom;
return result;
}
Rational substract(Rational
f2)
{
Rational result;
int
n1=this->numerator;
int
d1=this->denominator;
int
n2=f2.numerator;
int
d2=f2.denominator;
int
result_denom=d1*d2;
int
result_numer=n1*d2-n2*d1;
reduce(result_numer,result_denom);
result.numerator=result_numer;
result.denominator=result_denom;
return result;
}
Rational multiply(Rational
f2)
{
Rational result;
int
n1=this->numerator;
int
d1=this->denominator;
int
n2=f2.numerator;
int
d2=f2.denominator;
int
result_denom=d1*d2;
int
result_numer=n1*n2;
reduce(result_numer,result_denom);
result.numerator=result_numer;
result.denominator=result_denom;
return result;
}
void display()
{
if(numerator!=0)
cout<<numerator<<"/"<<denominator<<endl;
else
if(numerator==0)
cout<<numerator<<endl;
}
};
int main()
{//4/5
Rational f1(1,2);
Rational f2(1,2);
cout<<"The first Rational number is:
";
f1.display();
cout<<"The second Rational number is:
";
f2.display();
//checking if user has entered a rational
number with denominator=0
if(f1.isdenomZero()||f2.isdenomZero())
{
cout<<"Denominator
cannot be zero";
}
else
{
Rational f3;
Rational f4;
Rational f5;
f3=f1.add(f2);
f4=f1.multiply(f2);
f5=f1.substract(f2);
cout<<"Result after adding the two
rational numbers: ";
f3.display();
cout<<"Result after multiplying the two
rational numbers: ";
f4.display();
cout<<"Result after substracting the two
rational numbers: ";
f5.display();
}
}