Create a class Rational Number (fractions) with the following capabilities

/*
 
Problem Statement:
Create a class Rational Number (fractions) with the following capabilities:
a) Create a constructor that prevents a 0 denominator in a fraction, reduces or simplifies
fractions that are not in reduced form and avoids negative denominators.
b) Overload the addition, subtraction, multiplication and division operators for this class.
c) Overload the relational and equality operators for this class.
*/
#include<iostream>
#include<cstdlib>
using namespace std;
class Rational1
{
 int numerator,denominator;
public:
 Rational1();
 Rational1(int,int);
 Rational1 operator+(Rational1);
 Rational1 operator-(Rational1);
 Rational1 operator*(Rational1);
 Rational1 operator/(Rational1);
 bool operator>(Rational1);
 bool operator<(Rational1);
 bool operator>=(Rational1);
 bool operator<=(Rational1);
 bool operator==(Rational1);
 bool operator!=(Rational1);
 void printRational1();
 void reduce();
};
 
// default constructor: parameters are numerator and denominator respectively
Rational1::Rational1(int n, int d) {
 numerator = n;
 denominator = d;
 if (d==0 || d<0)
 {
  cout<<"Cannot enter zero or negative numbers for denominator"<<endl;
  exit(1);
 }
 reduce();
}
Rational1::Rational1() {
 numerator = 0;
 denominator =1;
}
 
//--------------------------------- add --------------------------------------
// overloaded +: addition of 2 Rational1s, current object and parameter
Rational1 Rational1::operator+(Rational1 a) {
 Rational1 t;
 t.numerator = a.numerator * denominator + a.denominator * numerator;
 t.denominator = a.denominator * denominator;
 t.reduce();
 return t;
}
 
//------------------------------ subtract ------------------------------------
// subtraction of 2 Rational1s, current object and parameter
 
Rational1 Rational1::operator-(Rational1 s) {
 Rational1 t;
 t.numerator = s.denominator * numerator - denominator * s.numerator;
 t.denominator = s.denominator * denominator;
 t.reduce();
 return t;
}
 
//------------------------------ multiply ------------------------------------
// multiplication of 2 Rational1s, current object and parameter
Rational1 Rational1::operator*(Rational1 m) {
 Rational1 t;
 
 t.numerator = m.numerator * numerator;
 t.denominator = m.denominator * denominator;
 t.reduce();
 return t;
}
 
//-------------------------------- divide ------------------------------------
// division of 2 Rational1s, current object and parameter,
// division by zero crashes
Rational1 Rational1::operator/(Rational1 v) {
 Rational1 t;
 
 t.numerator = v.denominator * numerator;
 t.denominator = denominator * v.numerator;
 t.reduce();
 
 return t;
}
bool Rational1::operator>(Rational1 v) {
 float f1=numerator/float(denominator);
 float f2=v.numerator/float(v.denominator);
 if(f1>f2)
 return true;
 else return false;
}
bool Rational1::operator<(Rational1 v) {
 float f1=numerator/float(denominator);
 float f2=v.numerator/float(v.denominator);
 if(f1<f2)
 return true;
 else return false;
 
}
bool Rational1::operator>=(Rational1 v) {
 float f1=numerator/float(denominator);
 float f2=v.numerator/float(v.denominator);
 if(f1>=f2)
 return true;
 else return false;
 
}
bool Rational1::operator<=(Rational1 v) {
 float f1=numerator/float(denominator);
 float f2=v.numerator/float(v.denominator);
 if(f1<=f2)
 return true;
 else
 return false;
 
}
bool Rational1::operator==(Rational1 v) {
 float f1=numerator/float(denominator);
 float f2=v.numerator/float(v.denominator);
 if(f1==f2)
 return true;
 else
 return false;
}
bool Rational1::operator!=(Rational1 v) {
 float f1=numerator/float(denominator);
 float f2=v.numerator/float(v.denominator);
 if(f1!=f2)
 return true;
 else return false;
}
//---------------------------- printRational1 ---------------------------------
void Rational1::printRational1() {
 if (denominator == 0)
 cout << endl << "DIVIDE BY ZERO ERROR!!!" << endl;
 else if (numerator == 0)
 cout << 0;
 else
 cout << numerator << "/" << denominator;
}
 
//-------------------------------- reduce ------------------------------------
// reduce fraction to lowest terms
void Rational1::reduce() {
 int n = numerator < 0 ? -numerator : numerator;
 int d = denominator;
 int largest = n > d ? n : d;
 int gcd = 0; // greatest common divisor
 
 for (int loop = largest; loop >= 2; loop--)
 if (numerator % loop == 0 && denominator % loop == 0) {
 gcd = loop;
 break;
 }
 
 if (gcd != 0) {
 numerator /= gcd;
 denominator /= gcd;
 }
}
int main()
{
Rational1 r1,r2,r3;
int n,d;
int choice;
char ch;
cout<<"Enter in format :numerator <space> denominator\n";
cout<<"Enter first Rational1 number - numerator and denominator\t: ";
cin>>n>>d;
r1=Rational1(n,d);
cout<<"Enter second Rational1 number - numerator and denominator\t:";
cin>>n>>d;
r2=Rational1(n,d);
do
{
 cout<<"**********MENU***********\n";
 cout<<"1. +\t2. -\t3.*\t4. /\t5. >\t6. <\n7. >=\t8. <=\t9. !=\t10. ==";
 cout<<"\n11. Print\t12.Exit\n";
 cout<<"Enter your choice";
 cin>>choice;
 switch(choice)
 {
 case 1:
 r3=r1+r2;
 r3.printRational1();
 break;
 case 2:
 r3=r1-r2;
 r3.printRational1();
 break;
 case 3:
 r3=r1*r2;
 r3.printRational1();
 break;
 case 4:
 r3=r1/r2;
 r3.printRational1();
 break;
 case 5:
 if(r1>r2)
 {
 r1.printRational1();
 cout<<" is greater than ";
 r2.printRational1();
 }
 else
 {
 r1.printRational1();
 cout<<" is not greater than ";
 r2.printRational1();
 }
 break;
 case 6:
 if(r1<r2)
 {
 r2.printRational1();
 cout<<" is greater than ";
 r1.printRational1();
 }
 else
 {
 r2.printRational1();
 cout<<" is not greater than ";
 r1.printRational1();
 }
 break;
 case 7:
 if(r1>=r2)
 {
 r1.printRational1();
 cout<<" is greater than or equal to ";
 r2.printRational1();
 }
 else
{
 r1.printRational1();
 cout<<" is not greater than or equal to ";
 r2.printRational1();
 }
 break;
 case 8:
 if(r1<=r2)
 {
 r2.printRational1();
 cout<<" is greater than or equal to ";
 r1.printRational1();
 }
 else
 {
 r2.printRational1();
 cout<<" is not greater than or equal to ";
 r1.printRational1();
 }
 break;
 case 9:
 if(r1!=r2)
 {
 r1.printRational1();
 cout<<" is not equal to ";
 r2.printRational1();
 }
 else
{
 r1.printRational1();
 cout<<" is  equal to ";
 r2.printRational1();
 }
 break;
 case 10:
 if(r1==r2)
 {
 r1.printRational1();
 cout<<" is equal to ";
 r2.printRational1();
 }
 else
 {
 r1.printRational1();
 cout<<" is not equal to ";
 r2.printRational1();
 }
 break;
 case 11:
 cout<<"\nFirst Rational Number\n";
 r1.printRational1();
 cout<<"\nSecond Rational Number\n";
 r2.printRational1();
 cout<<"\nThird Rational Number\n";
 r3.printRational1();
 break;
 case 12:
 exit(0);;
 }
cout<<"\nDo you want to continue(y/n)\n";
cin>>ch;
}while(ch=='y' || ch=='Y');
return 0;
}
 
/*
Output:
system@system-Lenovo:~$ g++ rational.cpp
system@system-Lenovo:~$ ./a.out
Enter in format :numerator <space> denominator
Enter first Rational1 number - numerator and denominator : 1 2
Enter second Rational1 number - numerator and denominator :1 4
**********MENU***********
1. + 2. - 3.* 4. / 5. > 6. <
7. >= 8. <= 9. != 10. ==
11. Print 12.Exit
Enter your choice1
3/4
Do you want to continue(y/n)
y
**********MENU***********
1. + 2. - 3.* 4. / 5. > 6. <
7. >= 8. <= 9. != 10. ==
11. Print 12.Exit
Enter your choice2
1/4
Do you want to continue(y/n)
y
**********MENU***********
1. + 2. - 3.* 4. / 5. > 6. <
7. >= 8. <= 9. != 10. ==
11. Print 12.Exit
Enter your choice3
1/8
Do you want to continue(y/n)
y
**********MENU***********
1. + 2. - 3.* 4. / 5. > 6. <
7. >= 8. <= 9. != 10. ==
11. Print 12.Exit
Enter your choice4
2/1
Do you want to continue(y/n)
y
**********MENU***********
1. + 2. - 3.* 4. / 5. > 6. <
7. >= 8. <= 9. != 10. ==
11. Print 12.Exit
Enter your choice5
1/2 is greater than 1/4
Do you want to continue(y/n)
y
**********MENU***********
1. + 2. - 3.* 4. / 5. > 6. <
7. >= 8. <= 9. != 10. ==
11. Print 12.Exit
Enter your choice6
1/4 is not greater than 1/2
Do you want to continue(y/n)
y
**********MENU***********
1. + 2. - 3.* 4. / 5. > 6. <
7. >= 8. <= 9. != 10. ==
11. Print 12.Exit
Enter your choice7
1/2 is greater than or equal to 1/4
Do you want to continue(y/n)
y
**********MENU***********
1. + 2. - 3.* 4. / 5. > 6. <
7. >= 8. <= 9. != 10. ==
11. Print 12.Exit
Enter your choice8
1/4 is not greater than or equal to 1/2
Do you want to continue(y/n)
y
**********MENU***********
1. + 2. - 3.* 4. / 5. > 6. <
7. >= 8. <= 9. != 10. ==
11. Print 12.Exit
Enter your choice9
1/2 is not equal to 1/4
Do you want to continue(y/n)
y
**********MENU***********
1. + 2. - 3.* 4. / 5. > 6. <
7. >= 8. <= 9. != 10. ==
11. Print 12.Exit
Enter your choice10
1/2 is not equal to 1/4
Do you want to continue(y/n)
y
**********MENU***********
1. + 2. - 3.* 4. / 5. > 6. <
7. >= 8. <= 9. != 10. ==
11. Print 12.Exit
Enter your choice11
 
First Rational Number
1/2
Second Rational Number
1/4
Third Rational Number
2/1
Do you want to continue(y/n)
n
system@system-Lenovo:~$ 
*/

Post a Comment

1 Comments