Question

Create a class called Rational for performing arithmetic with fractions. Write a program to test your class. Use integer variables to represent the private data of the elass the numerator and the denominator. Provide a constructor that enables an object of this class to be initialized when its declared. The constructor should contain default values in case no initializers are provided and should store the fraction in reduced form. For example, the fraction would be stored in the object as I in the numerator and 2 in the denominator. Provide public member functions that perform each of the following tasks: a) Adding two Rational numbers. The result should be stored in reduced form. b) Subtracting two Rational numbers. The result should be stored in reduced form. c) Multiplying two Rational numbers. The result should be stored in reduced form. d) Dividing two Rational numbers. The result should be stored in reduced form. e) Printing Rational numbers in the form a/b, where a is the numerator and b is the denominator 0 Printing Rational numbers in floating-point format Create an object of class Rational in the main function and test all functions. Sum of vo frutiom+ ch a ad - ch b dbd Differna of tro fradions Product of two fractions:?× e) ad bc Division of taro fradtions: 73+778-29724 29/24 = 1.20833 3-7/813/24 13/24-0.541667 1/3 x 7/8-7/24 724 = 0.291667 1/3 1 718-8/21 8/21-0, 380952

C++...please help follow exact steps output needs to be the exact same

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include<bits/stdc++.h>

using namespace std;

/*

a/b+c/d=(a*d+b*c)/b*d

a/b-c/d=(a*d-b*c)/b*d

a/b*c/d=(a*c)/b*d

*/

class Rational

{ private:

int numerator,denominator; //private variables

public:

//constructor

Rational()

{

numerator=0;

denominator=1;

simplify();//to simplify numerator and denominator

}

//simplifies the fraction

void simplify()

{

int x=__gcd(numerator,denominator); //find gcd

numerator=numerator/x; //divide the gcd

denominator=denominator/x;

}

//constructor

Rational(int numerator,int denominator)

{

setNumerator(numerator); //set numerator

setDenominator(denominator);//set denominator

simplify();

}

//add function , returns a Rational object

Rational add( Rational obj)//add

{

Rational temp,r;

//finding numerator

int n=numerator*obj.denominator+obj.numerator*denominator;

int d=denominator*obj.denominator; //denominator

//simplify fraction

//move - sign to numerator

if(d<0)

{

d=d*-1;

n=n*-1;

}

//return new object

return Rational(n,d);

}

Rational sub(Rational obj)//subtract

{

Rational temp,r;

//find numerator

int n=numerator*obj.denominator-obj.numerator*denominator;

int d=denominator*obj.denominator;

//simplify the fraction(4/4 is reduced to 1/1)

int x=__gcd(n,d);

n=n/x;

d=d/x;

//for -ve sign

if(d<0)

{

d=d*-1;

n=n*-1;

}

return Rational(n,d);

}

Rational multiply(Rational obj)//multiply

{

Rational temp,r;

//numerator

int n=numerator*obj.numerator;

int d=denominator*obj.denominator;

//simplify

int x=__gcd(n,d);

n=n/x;

d=d/x;

//-ve sign

if(d<0)

{

d=d*-1;

n=n*-1;

}

return Rational(n,d);

}

Rational divide(Rational obj)

{

int n=numerator*obj.denominator;

int d=denominator*obj.numerator;

return Rational(n,d);

}

//function to set numerator

void setNumerator(int n)

{

numerator=n;

}

//function to set denominator

void setDenominator(int n)

{

denominator=n;

}

void printRational()

{

cout<<numerator<<"/"<<denominator;

}

void printFloat()

{

cout<<(float)numerator/denominator<<endl;

}

};

int main(int argc, char const *argv[])

{

/* code */

Rational a(1,3),b(7,8),c;

c=a.add(b); //add

a.printRational();//print a

cout<<" + ";

b.printRational(); //print b

cout<<" = " ; //print =

//add a+b

c.printRational(); //print c

cout<<endl;

c.printRational();

cout<<" = ";

c.printFloat();

cout<<endl;

//subtraction

c=a.sub(b);//a-b

a.printRational();

cout<<" - ";

b.printRational();

cout<<" = " ;

//add a+b

c.printRational(); //print

cout<<endl;

c.printRational();

cout<<" = ";

c.printFloat();

cout<<endl;

c=a.multiply(b);

a.printRational();

cout<<" * ";

b.printRational();

cout<<" = " ;

//add a+b

c.printRational(); //print

cout<<endl;

c.printRational();

cout<<" = ";

c.printFloat();

cout<<endl;

c=a.divide(b);

a.printRational();

cout<<" / ";

b.printRational();

cout<<" = " ;

//add a+b

c.printRational(); //print

cout<<endl;

c.printRational();

cout<<" = ";

c.printFloat();

cout<<endl;

return 0;

}

Add a comment
Know the answer?
Add Answer to:
C++...please help follow exact steps output needs to be the exact same Create a class called...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • (Rational class) Create a class called Rational for performing arithmetic with fractions. Write a program to...

    (Rational class) Create a class called Rational for performing arithmetic with fractions. Write a program to test your class. Use integers variables to represent the private data of the class- the numerator and the denominator. Provide a constructor that enables an object of this class to be initialized when it is declared. The constructor should contain default values in case are no initializers are provided and should store the fraction in reduced form. For example, the fraction 3/6 would be...

  • (Rational Numbers) Create a class called Rational for performing arithmetic with fractions. Write a program to...

    (Rational Numbers) Create a class called Rational for performing arithmetic with fractions. Write a program to test your class. Use integer variables to represent the private instance variables of the class- 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 store the fraction in reduced form. The fraction 2/4 is equivalent to h and would be stored in the object as 1 in the numerator...

  • Please use Java language. Thanks in advance. HOME WORK due 09. 18.2019 (Rational Numbers) Create a...

    Please use Java language. Thanks in advance. HOME WORK due 09. 18.2019 (Rational Numbers) Create a class called Rational for performing arithmetic with fractions. Write a program to test your class. Use integer variables to represent the private instance variables of the class- 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 store the fraction in reduced form. The fraction 2/4 is equivalent to 2...

  • PLEASE USE OBJECT ORIENTED PROGRAMMING IN C# USING MICROSOFT VISUAL. *****PLEASE USE C#(C SHARP)*...

    PLEASE USE OBJECT ORIENTED PROGRAMMING IN C# USING MICROSOFT VISUAL. *****PLEASE USE C#(C SHARP)***** Also please dont use BigInteger thanks it's really appreciated for all the help PLEASE MAKE IT SO ITS USER INPUT Create a class called Rational for prforming arithmetie with fractions. Write an app to test your class Use integer variables to represent the private instance variables of the class-the numerator and the denominator. Provide a constructor that enables an object of this class to be initialized...

  • Can somebody help me with this assignment. I will highly appreciate. Also, please display the output...

    Can somebody help me with this assignment. I will highly appreciate. Also, please display the output as well. I need to use JAVA to to write the program. This is the sample output provided by my professor. HOME WORK: due 09.17.2019 (Rational Numbers) Create a class called Rational for performing arithmetic with fractions. Write a program to test your class. Use integer variables to represent the private instance variables of the class- the numerator and the denominator. Provide a constructor...

  • Header file for the Rational class: #ifndef RATIONAL_H #define RATIONAL_H class Rational { public: Rational( int...

    Header file for the Rational class: #ifndef RATIONAL_H #define RATIONAL_H class Rational { public: Rational( int = 0, int = 1 ); // default constructor Rational addition( const Rational & ) const; // function addition Rational subtraction( const Rational & ) const; // function subtraction Rational multiplication( const Rational & ) const; // function multi. Rational division( const Rational & ) const; // function division void printRational () const; // print rational format void printRationalAsDouble() const; // print rational as...

  • In mathematics, a rational number is any number that can be expressed as the quotient or...

    In mathematics, a rational number is any number that can be expressed as the quotient or fraction p/q of two integers, p and q, with the denominator q not equal to zero. Since q may be equal to 1, every integer is a rational number. Define a class that can represent for a rational number. Use the class in a C++ program that can perform all of the following operations with any two valid rational numbers entered at the keyboard...

  • C++ Create a Rational Number (fractions) class like the one in Exercise 9.6 of the textbook....

    C++ Create a Rational Number (fractions) class like the one in Exercise 9.6 of the textbook. Provide the following capabilities: Create a constructor that prevents a 0 denominator in a fraction, reduces or simplifies fractions (by dividing the numerator and the denominator by their greatest common divisor) that are not in reduced form, and avoids negative denominators. Overload the addition, subtraction, multiplication, and division operators for this class. Overload the relational and equality operators for this class. Provide a function...

  • C++ For this assignment you will be building on the Original Fraction class you began last...

    C++ For this assignment you will be building on the Original Fraction class you began last week. You'll be making four major changes to the class. [15 points] Delete your set() function. Add two constructors, a default constructor that assigns the value 0 to the Fraction, and a constructor that takes two parameters. The first parameter will represent the initial numerator of the Fraction, and the second parameter will represent the initial denominator of the Fraction. Since Fractions cannot have...

  • C++ Create a class called Complex for performing arithmetic with complex numbers. Write a program to...

    C++ Create a class called Complex for performing arithmetic with complex numbers. Write a program to test your class. Complex numbers have the form realPart + j imaginaryPart Use double variables to represent the private data of the class. Provide a constructor that enables an object of this class to be initialized when it is declared. The constructor should contain default values in case no initializers are provided. Provide public member functions that perform the following tasks: Adding two Complex...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT