C Object Oriented Programming Programming Assignment 5 Ratio

C++ Object Oriented Programming
Programming Assignment #5 Rational Class
Create a class called Rational that represents a rational number. A rational number is any number that can be expressed as a ratio of two integers (a/b), with the denominator not equal to zero.
1) Use only two private member variables numerator and denominator. Do not add any other ones.
2) Create two constructors; one of which should be a default constructor.
3) Add all necessary Get and Set functions.
4) Create a private member helper function to store the result in the most reduced form. 4/16 must be converted to 1/4 before it is stored in the object.
5) If the denominator and the nominator are both negative numbers, the class removes the signs automatically. If the denominator is a negative number, reverse the signs. This is done by the previous private helper function. Examples: -3/-5 should be stored as 3/5. 5/-7 should be stored as -5/7.
6) Create a member function to add two Rational numbers (two objects of the class).
7) Add three more similar functions to subtract, multiply, and divide two Rational numbers (two object of the rational class).
8) Create a function to print the rational number in the form of a/b (1/4).
9) Add another function to print the number in the form of a floating point (.25).
10) Remember, all functions are member functions, such as add( ) and multiply( ).
In order to test your class in your main function, prompt the user for numerator and the denominator for your first object. Repeat the prompt for the second object. Perform all mathematical operations by calling the functions. Print the two rational numbers in both print forms (a/b) and as a floating point. Then, print the result of the calculations in both forms. Make sure you label all numbers printed, for instance:
Your first rational number is: 3/2 which is 1.5. Your second number is 5/2 which is 2.5.
The sum of the numbers is 4/1 or 4.00.......Output the rest of the result the same way.
Note that in the above example 3/2 + 5/2 = 8/2, your class stores the fraction in the reduced form, 4/1. 4.00 is the floating point version of the same number.
Allow the user to continue the process (use a loop).
Here is some code examples to get you started.
Function definition: passing a Rational object to the member function of another.
Rational Rational :: multiply (Rational num) {
Rational temp; //creating a temporary object temp.numerator = numerator * num.numerator; temp.denominator = denominator * num.denominator; temp.simplify( ); //calling the helper function to reduce the fraction return temp;
}
In your main function:
Rational num1, num2, result;
//get values from the user for num1 and num2 //some other codes result = num1.add(num2);
//some other codes here result.printRational( ); result.printFloating( );
As usual, add all necessary comments. Run your program with several test cases. Capture the screen and save it. Remember, testing is as important as writing codes. In fact, the software industry spends a lot more money and time on testing and maintenance than writing the original codes.

Solution

Hi, Please find my program.

Please let me know in case of any issue.

########### Rational.h ###########

// Header file Rational.h declares class Rational.
class Rational
{
public:
   // Constructors
   // Post: Numerator and denominator have been set to zero
   Rational();
  
   // Post: Numerator has been set to initNumerator;
   // denominator has been set to initDenominator.
   Rational(int initNumerator, int initDenominator);

   // Post: self + frac1 is returned.
   Rational Add(Rational sfrac1);

   // Post: self - frac1 is returned.
   Rational Subtract(Rational frac1);

   // Post: self * frac1 is returned.
   Rational Multply(Rational frac1);

   // Post: self / frac1 is returned.
   Rational Divide(Rational frac1);

   // Post: Numerator of frac1 is returned.
   int GetNumerator() const;

   // Post: Denominator of frac1 is returned.
   int GetDenominator() const;

   void Print();

   void PrintFraction();
  
private:
   // Calculates the greates common divisor with Euclid\'s algorithm both
   //arguments have to be positive
   int gcd(int a, int b);
   int numerator;
   int denominator;
};

############# Rational.cpp ############

#include \"Rational.h\"
#include <iostream>

// Constructors
Rational::Rational(){
   numerator = 0;
   denominator = 1;
}
// Post: Numerator and denominator have been set to zero
Rational::Rational(int initNumerator, int initDenominator){
   if(initDenominator == 0){
       std::cout<<\"Denominator can not be zero\"<<std::endl;
       exit(0);
   }
   int gc = gcd(initNumerator, initDenominator);
   numerator = initNumerator/gc;
   denominator = initDenominator/gc;
}
// Post: Numerator has been set to initNumerator;
// denominator has been set to initDenominator.
Rational Rational::Add(Rational frac1){

int num = (numerator * frac1.denominator)+(frac1.numerator*denominator);
int denom = denominator * frac1.denominator;
  
int gc = gcd(num, denom);
return Rational(num/gc, denom/gc);
}
// Post: self + frac1 is returned.
Rational Rational::Subtract(Rational frac1){

int num = (numerator * frac1.denominator)-(frac1.numerator*denominator);
int denom = denominator * frac1.denominator;
  
int gc = gcd(num, denom);
return Rational(num/gc, denom/gc);
}
// Post: self - frac1 is returned.
Rational Rational::Multply(Rational frac1){

int num = numerator * frac1.numerator;
int denom = denominator * frac1.denominator;
  
int gc = gcd(num, denom);
return Rational(num/gc, denom/gc);
}
// Post: self * frac1 is returned.
Rational Rational::Divide(Rational frac1){
int num = numerator * frac1.denominator;
int denom = denominator * frac1.numerator;
  
int gc = gcd(num, denom);
return Rational(num/gc, denom/gc);
}
// Post: self / frac1 is returned.
int Rational::GetNumerator() const{
   return numerator;
}
// Post: Numerator of frac1 is returned.
int Rational::GetDenominator() const{
   return denominator;
}
// Post: Denominator of frac1 is returned.
void Rational::Print(){
   if(denominator < 0){
       numerator = (-numerator);
       denominator = (-denominator);
   }

   if(denominator == 1)
       std::cout<<numerator;
   else
       std::cout<<numerator<<\"/\"<<denominator;
}

void Rational::PrintFraction(){
std::cout<<(float)numerator/(float)denominator<<std::endl;
}

// Calculates the greates common divisor with Euclid\'s algorithm both arguments have to be positive
int Rational::gcd(int a, int b) {
while (a != b) {
if (a > b) {
a -= b;
}else {
b -= a;
}
}
return a;
}

############### RationalTest.cpp #################

#include <iostream>
#include \"Rational.h\"
using namespace std;

int main(){

   double n, d, option;

   while(true){

       cout<<\"1.Add 2.Subtract 3.Multiplication 4.Divide 5.Exit\ \";
       cin>>option;

       if(option == 1){
           cout<<\"Enter numerator and denominator of first rational number: \";
           cin>>n>>d;
           // creating Rational
           Rational r1(n, d);
           cout<<\"Enter numerator and denominator of second rational number: \";
           cin>>n>>d;
           Rational r2(n, d);

           // calling sum method
           Rational sum = r1.Add(r2);
           cout<<\"Sum: \";
           r1.Print();
           cout<<\" + \";
           r2.Print();
           cout<<\" = \";
           sum.Print();
           cout<<\", \";
           sum.PrintFraction();
           cout<<endl;
       }else if(option == 2){
           cout<<\"Enter numerator and denominator of first rational number: \";
           cin>>n>>d;
           // creating Rational
           Rational r1(n, d);
           cout<<\"Enter numerator and denominator of second rational number: \";
           cin>>n>>d;
           Rational r2(n, d);

           // calling sum method
           Rational sub = r1.Subtract(r2);
           cout<<\"Subtract: \";
           r1.Print();
           cout<<\" - \";
           r2.Print();
           cout<<\" = \";
           sub.Print();
           cout<<\", \";
           sub.PrintFraction();
           cout<<endl;
       }else if(option == 3){
           cout<<\"Enter numerator and denominator of first rational number: \";
           cin>>n>>d;
           // creating Rational
           Rational r1(n, d);
           cout<<\"Enter numerator and denominator of second rational number: \";
           cin>>n>>d;
           Rational r2(n, d);

           // calling sum method
           Rational mul = r1.Multply(r2);
           cout<<\"Multiplication: \";
           r1.Print();
           cout<<\" * \";
           r2.Print();
           cout<<\" = \";
           mul.Print();
           cout<<\", \";
           mul.PrintFraction();
           cout<<endl;
       }else if(option == 4){
           cout<<\"Enter numerator and denominator of first rational number: \";
           cin>>n>>d;
           // creating Rational
           Rational r1(n, d);
           cout<<\"Enter numerator and denominator of second rational number: \";
           cin>>n>>d;
           Rational r2(n, d);

           // calling sum method
           Rational div = r1.Divide(r2);
           cout<<\"Division: \";
           r1.Print();
           cout<<\" / \";
           r2.Print();
           cout<<\" = \";
           div.Print();
           cout<<\", \";
           div.PrintFraction();
           cout<<endl;
       }else if(option == 5){
           break;
       }else{
           cout<<\"Invalid Option!!!\"<<endl;
       }

   }

   return 0;
}

C++ Object Oriented Programming Programming Assignment #5 Rational Class Create a class called Rational that represents a rational number. A rational number is
C++ Object Oriented Programming Programming Assignment #5 Rational Class Create a class called Rational that represents a rational number. A rational number is
C++ Object Oriented Programming Programming Assignment #5 Rational Class Create a class called Rational that represents a rational number. A rational number is
C++ Object Oriented Programming Programming Assignment #5 Rational Class Create a class called Rational that represents a rational number. A rational number is
C++ Object Oriented Programming Programming Assignment #5 Rational Class Create a class called Rational that represents a rational number. A rational number is

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site