In: Computer Science
Define a class called Rational for rational numbers. A
rational number is a number that can be represented as the quotient
of two integers. For example, /2, 3/4, 64/2, and so forth are all
rational numbers.
Represent rational numbers as two private values of type int, numfor the numerator and den for the denominator. The class has a default constructor with default values (num = 0,den = 1), a copy constructor, an assignment operator and three friend functions for operator overloading +, << and >>.
In + operator, the sum of a/b and c/d is (a*d+b*c)/bd. In >> operator, if the user enter 0 for the denominator, output error message and prompt user to type again (use while loop). In << operator, output a rational number in the format a/b. If a = 0, just output 0. If b = 1, just output a. If the number is a/-b, output –a/b. If the number is –a/-b, output a/b. In the main function, declare two objects and use operator >> and assign data for them. Then sum the two numbers and display the results. Sample output:
Enter two integers: 3 0
Error: the denominator cannot be 0. Try again.
Enter two integers: 2 3
Enter two integers: 3 -4
2/3 + -3/4 = -1/12
(2 bonus marks) Add a private member function with function prototype void normalize( ) to normalize the values stored so that, after normalization, the denominator is positive and the numerator and denominator are as small as possible. For example, after normalization 4/-8 would be represented the same as -1/2. You must email me your code for this part.
//Your CPP code
#include
using namespace std;
class Rational{//class
private:
int num,den;
public :
Rational(){//default
constructor
num=0;
den=1;
}
Rational(Rational const
&obj){
num=obj.num;
den=obj.den;
}
int gcd(int a,int b){//gcd to normalize
if(a == 0)return b;
else
gcd(b%a,a);
}
void normalize(){
int n = this->num;
int m = this->den;
if( m < 0 ){
m = -m;
n = -n;
}
int k = gcd(m,n);
if(k>0){//normalization
m = m/k;
n = n/k;}
this->num=n;
this->den=m;
}//friend function prototype
friend Rational operator+(Rational const
&,Rational const &);
friend istream &operator>>(istream
&in,Rational &);
friend ostream &operator<<(ostream
&out,Rational &);
};
ostream &operator<<(ostream &out,Rational
&ob){
if(ob.den < 0){
ob.den = -ob.den;
ob.num = -ob.num;
}
if(ob.num == 0 || ob.den == 1){
out<
out<
}
Rational operator+(Rational const &obj1 , Rational const
&obj2 ){
Rational R;
R.num = obj1.num*obj2.den + obj2.num*obj1.den;
R.den = obj1.den*obj2.den;
return R;
}
istream &operator>>(istream &in ,Rational
&obj1){
int n,d;
printf("Enter Numerator : ");
in>>n;
while(1){
printf("Enter Denominator :
");
in>>d;
if(d == 0)
printf("Wrong input again Enter
,Denominator can't be 0\n");
else
break;
}
obj1.num = n;
obj1.den = d;
return in;
}
int main(){
int i , j , k ;
Rational R1,R2,R4;//object creation
cin>>R1;
cin>>R2;//>> operator called
Rational R3 = R1+R2;//+ operator called
printf("Sum result\n");
cout<
R4.normalize();// Your additional function
printf("Normalize answer\n");
cout<
}
//Screenshot of my editor
//Screenshot of my output terminal