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 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.
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