Question

In: Computer Science

In Cpp A rational number is a quotient of two integers.For example, 12/5,12/-4,-3/4, and 4/6 are...

In Cpp

A rational number is a quotient of two integers.For example, 12/5,12/-4,-3/4, and 4/6 are all rational numbers .A rational number is said to be in reduced form if its denominator is positive and its numerator and denominator have no common divisor other than 1

For example,the reduced forms of the rational numbers given above are 12/5, -3/1, -3/4 and 2/3.

Write a class called Rational with a constructor Rational ( int, int) that takes two integers, a numerator and a denominator,and stores those two values in reduced form in corresponding private members.The class should have a private member function void reduce( ) that is used to accomplish the transformation to reduced form. The class should have an overloaded insertion operator << that will be used for output of objects of the class.

Part 2

Modify the class Rational of Programming Challenge 8 to add overloaded operators+,-,*,and/to be used for addition, subtraction,multiplication,and division.Test the class by reading and processing from the key board (or from a file) a series of rational expressions such as

2/3 + 2/8

2/3 * - 2/8

2/3 - 2/ 8

2/3 / 2/8

To facilitate parsing of the input, you may assume that numbers and arithmetic operators are separated by white space.

You should be turning in a Rational.h, Rational.cpp and rationaldriver.cpp that has your main.

All meaningful rational number code should be encapsulated in your Rational class.

Make sure to overload the asked for operators.

Make sure to create Rationals for each of the 4 stated examples in Rational Arithmetic I and show they reduce by using the overloaded << to output to the console.

Make sure to test your overloaded arithmetic expressions using the 4 expressions outlined in Rational Arithmetic II and output the resulting Rationals.

Use console for I/O. Use consts where appropriate.

Solutions

Expert Solution

Note: Could you plz go this code and let me know if u need any changes in this.Thank You
_________________

#include <iostream>
#include <string>
#include <cmath>
using namespace std;

class Rational {

private:
   //Declaring variables

int numer, denom;

void reduce();

public:

Rational(int n, int d);

friend ostream& operator<<(ostream& y, const Rational& out);

friend Rational operator+(Rational a, Rational b);

friend Rational operator-(Rational a, Rational b);

friend Rational operator*(Rational a, Rational b);

friend Rational operator/(Rational a, Rational b);

};


Rational::Rational(int numer, int denom) {

this->numer = numer;

this->denom = denom;

}

void Rational::reduce() {

if (numer < 0 && denom < 0) {//If both Denominator and Numerator are negative, it equals positive

numer *= -1;

denom *= -1;

}

if (numer < 0) { //If numerator is negative it temporarly makes it positive until end of if-statement

numer *= -1;

for (int r = denom * numer; r > 1; r--) {

if ((denom % r == 0) && (numer % r == 0)) {

denom /= r;

numer /= r;

}

}

numer *= -1;

}

if (denom < 0) {

numer *= -1; //If denominator is negative it formats it so negative comes before numerator at end

for (int r = denom * numer; r > 1; r--) {

if ((denom % r == 0) && (numer % r == 0)) {

denom /= r;

numer /= r;

}

}

denom *= -1;

}

else // If both positive no changes are made

for (int r = denom * numer; r > 1; r--) {

if ((denom % r == 0) && (numer % r == 0)) {

denom /= r;

numer/= r;

}

}

}

ostream& operator<<(ostream& y, const Rational& out ) {

if (out.denom == 1)

y << out.numer ;

else

y << out.numer << "/" << out.denom ;

return (y);

}

Rational operator+(Rational r1, Rational r2) {

int a, b, c, d;
a =r1.numer;
b = r1.denom;
c = r2.numer;
d = r2.denom;
int sumnumer = (a * d + b * c);
int sumdenom = (b * d);
  
Rational r(sumnumer, sumdenom);
r.reduce();

return r;

}

Rational operator-(Rational r1, Rational r2) {

int a, b, c, d;
a = r1.numer;
b = r1.denom;
c = r2.numer;
d = r2.denom;
int subnumer = (a * d - b * c);
int subdenom = (b * d);
  
Rational r(subnumer, subdenom);
r.reduce();
return r;

}

Rational operator*(Rational a, Rational b) {
int subnumer = (a.numer*b.numer);
int subdenom = (a.denom*b.denom);

Rational result(subnumer, subdenom);
result.reduce();
return result;

}

Rational operator/(Rational a, Rational b) {

int subnumer =(a.numer*b.denom);
int subdenom = (a.denom*b.numer);
  
Rational result(subnumer, subdenom);
result.reduce();
return result;

}

int main()
{
//Declaring variables
int numer,denom;
  
//Getting inputs entered by the user
cout<<"Rational#1:"<<endl;

cout<<"Enter numerator :";
cin>>numer;
cout<<"Enter denominator :";
cin>>denom;
Rational r1(numer,denom);
cout<<"Rational#2:"<<endl;
cout<<"Enter numerator :";
cin>>numer;
cout<<"Enter denominator :";
cin>>denom;
Rational r2(numer,denom);

//Performing Operations
cout<<r1;
cout<<" + ";
cout<<r2;
cout<<" = ";
cout<<r1+r2;

cout<<endl;

cout<<r1;
cout<<" - ";
cout<<r2;
cout<<" = ";
cout<<r1-r2;

cout<<endl;

cout<<r1;
cout<<" * ";
cout<<r2;
cout<<" = ";
cout<<r1*r2;

cout<<endl;
cout<<r1;
cout<<" / ";
cout<<r2;
cout<<" = ";
cout<<r1+r2;

cout<<endl;
  

  
return 0;
}

_____________________________

Output:

_______________Could you plz rate me well.Thank You


Related Solutions

1. A product of two functions (exponential) 2. A quotient of two functions (logarithmic) 3. example...
1. A product of two functions (exponential) 2. A quotient of two functions (logarithmic) 3. example of a composite function 4. A sum of two functions (rational) 5. A difference of two function (rational or either trigonometric) for all the functuons you come up w please give the domin range x and y int ans local max and min.
[4 5 5 2 4 4 6 3 3 7 5 3 6 3 4 4...
[4 5 5 2 4 4 6 3 3 7 5 3 6 3 4 4 6 5 4 5 3 7 5 5 4 2 6 5 6 6] This is my dataset Find mean, median, mode, variance, standard deviation, coefficient of variation, range, 70th percentile, 3rdquartile of the data and skewness and define what each of these statistics measure. For example, mean is a measure of the central tendency, what about the rest? Use Chebyshev’s rule to find...
[4 5 5 2 4 4 6 3 3 7 5 3 6 3 4 4...
[4 5 5 2 4 4 6 3 3 7 5 3 6 3 4 4 6 5 4 5 3 7 5 5 4 2 6 5 6 6] This is my dataset Split the dataset in two equal parts. You have 30 datavalues. If you split the data in two equal parts each part will contain 15 data values.  Call the first part Y and second part X.Draw scatter plot of the 2 datasets, X being on the horizontal...
12 #3 Find the probability of throwing a sum of 6 at least 4 times in...
12 #3 Find the probability of throwing a sum of 6 at least 4 times in 6 throws of a pair of fair dice.
Let A and b be the matrices A = 1 2 4 17 3 6 −12...
Let A and b be the matrices A = 1 2 4 17 3 6 −12 3 2 3 −3 2 0 2 −2 6 and b = (17, 3, 3, 4) . (a) Explain why A does not have an LU factorization. (b) Use partial pivoting and find the permutation matrix P as well as the LU factors such that PA = LU. (c) Use the information in P, L, and U to solve Ax = b
x^3-34x+12=0 1) list all rational roots that are possible according to the Rational Zero Theorm a)...
x^3-34x+12=0 1) list all rational roots that are possible according to the Rational Zero Theorm a) +-1 b)+-1,+-2,+-3,+-4,+-6,+-12 c)+-12 d) 1,2,3,4,6,12 b)use synthetic division to test several possible rational roots in order to identify one actual root c) use the root from part b to solve the equation the solution set it?
3 6 4 8 1 10 2 9 11 12 15 22 3 6 7 5...
3 6 4 8 1 10 2 9 11 12 15 22 3 6 7 5 8 1 12 14 Each column represents a different treatment given to sick rats. Each cell is a different rat. Use statistical analysis and use post hoc testing using contrasts to find the best treatment. Treatment 1: vitamins Treatment 2: prescription pills Treatment 3: brain surgery Treatment 4: shock therapy Treatment 5: dietary changes
By dividing 14528 by a certain number, Suresh gets 83 as quotient and 3 as remainder. What is the divisor?
By dividing 14528 by a certain number, Suresh gets 83 as quotient and 3 as remainder. What is the divisor?
a. Find three rational numbers between 3/4 and 0. 75 overbar b. Find three rational numbers...
a. Find three rational numbers between 3/4 and 0. 75 overbar b. Find three rational numbers between 1/9 and 0. 12 overbar
Return on Investment: Margin vs Volume Example 1: 12% = 6% x 2 Example 2: 12%...
Return on Investment: Margin vs Volume Example 1: 12% = 6% x 2 Example 2: 12% = 5% x 2.4 Example 3: 12% = 1% x 12 Example 4: 12% = 12% x 1 Why is it important for you to know whether your firm looks like Examples 1 and 2 or like Example 3 or Example 4? What are the riskier examples? Why?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT