Question

In: Computer Science

use c++ Provide a header file as well. So for this question provide Rational.h, Rantional.cpp Create...

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

Solutions

Expert Solution

// 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();
    }


}


Related Solutions

Using C++ Create the UML, the header, the cpp, and the test file for an ArtWork...
Using C++ Create the UML, the header, the cpp, and the test file for an ArtWork class. The features of an ArtWork are: Artist name (e.g. Vincent vanGogh or Stan Getz) Medium (e.g. oil painting or music composition) Name of piece (e.g. Starry Night or Late night blues) Year (e.g. 1837 or 1958)
**C++ program** Please provide a header file(bubble_sort.h) and a program file(bubble_sort.cpp). Also include notes to explain...
**C++ program** Please provide a header file(bubble_sort.h) and a program file(bubble_sort.cpp). Also include notes to explain code and output screenshots. Please use Bubble Sort code provided. Write a program for sorting a list of integers in ascending order using the bubble sort algorithm. Requirements Implement the following functions: Implement a function called readData int readData( int *arr) arr is a pointer for storing the integers. The function returns the number of integers. The function readData reads the list of integers...
Given the header file (grid.h) and the source file,(grid.cpp) create the file trap.cpp. Here are the...
Given the header file (grid.h) and the source file,(grid.cpp) create the file trap.cpp. Here are the requirements for trap.cpp: Write a main program in a file called trap.cpp that solves the following scenario, using your Grid class: Giant Mole People have risen from their underground lairs and are taking over the world. You have been taken prisoner and placed in an underground jail cell. Since the Mole People are blind and don't know how to build doors, your cell has...
Using C++ In a separate header file: Create a new type called "Patient" - you must...
Using C++ In a separate header file: Create a new type called "Patient" - you must use a  class. Give your "Patient" type at least five (5) member elements of your choosing, and at least one member function. You should have member elements to hold patient name, and visitReason (which can change), and other items of your choosing. In your cpp file: Create at least one instance of Patient type (example: CurrentPatient ). Create a menu-driven program OF YOUR OWN DESIGN...
A header file contains a class template, and in that class there is a C++ string...
A header file contains a class template, and in that class there is a C++ string object. Group of answer choices(Pick one) 1)There should be a #include for the string library AND a using namespace std; in the header file. 2)There should be a #include for the string library. 3)There should be a #include for the string library AND a using namespace std; in the main program's CPP file, written before the H file's include.
The requirements for this program are as follows: Create a header file named “Employee.h”. Inside this...
The requirements for this program are as follows: Create a header file named “Employee.h”. Inside this header file, declare an Employee class with the following features: Private members a std::string for the employee’s first name a std::string for the employee’s last name an unsigned int for the employee’s identification number a std::string for the city in which the employee works Public members A constructor that takes no arguments A constructor that takes two arguments, representing: the employee’s first name the...
Use the functions.h header file with your program (please write in C code): #ifndef FUNCTIONS_H #define...
Use the functions.h header file with your program (please write in C code): #ifndef FUNCTIONS_H #define FUNCTIONS_H typedef struct MyStruct { int value; char name[ 100 ]; } MyStruct; void sortArray( MyStruct*, int ); void printArray( MyStruct*, int ); #endif Create a source file named functions.c with the following: A sorting function named sortArray. It takes an array of MyStruct's and the length of that array. It returns nothing. You can use any of the sorting algorithms, you would like...
Write a C++ program that design a class definition to be put in a header file...
Write a C++ program that design a class definition to be put in a header file called fizzjazz.h A store sells online FizzJazz which are sound tones made by famous bands. For each FizzJazz, the store wants to keep track of the following attributes: * title - the name of the sound tone * band - Famous band name that created the tone * duration - this is in seconds and may be fractional: examples: 20.0, 34.5 Each attribute will...
Write a C++ program that design a class definition to be put in a header file...
Write a C++ program that design a class definition to be put in a header file called fizzjazz.h A store sells online FizzJazz which are sound tones made by famous bands. For each FizzJazz, the store wants to keep track of the following attributes: * title - the name of the sound tone * band - Famous band name that created the tone * duration - this is in seconds and may be fractional: examples: 20.0, 34.5 Each attribute will...
Write a C++ program that design a class definition to be put in a header file...
Write a C++ program that design a class definition to be put in a header file called fizzjazz.h A store sells online FizzJazz which are sound tones made by famous bands. For each FizzJazz, the store wants to keep track of the following attributes: * title - the name of the sound tone * band - Famous band name that created the tone * duration - this is in seconds and may be fractional: examples: 20.0, 34.5 Each attribute will...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT