Question: Intro to Computer Programming 2 Class : C++ Hi I am stuck on the part where it says Represent rational numbers as two pointers to
Intro to Computer Programming 2 Class : C++

Hi I am stuck on the part where it says Represent rational numbers as two pointers to integers, one for the numerator and one for the denominator. Could you please add the part about pointers or do I have it in there. I am just confused Thanks
Heres my code:
#include
using namespace std;
class Rational
{
private:
int numerator;
int denominator;
public:
Rational()
{
numerator = 0;
denominator = 1;
}
Rational(int num, int denom)
{
numerator = num;
denominator = denom;
}
void output()
{
cout
}
//Arithmetic operators: + - * /
Rational operator+(Rational second)
{
Rational temp;
temp.setNumerator(numerator*second.getDenominator() + denominator*second.getNumerator());
temp.setDenominator(denominator * second.getDenominator());
return temp.reduce();
}
Rational operator-(Rational second)
{
Rational temp;
temp.setNumerator(numerator*second.getDenominator() - denominator*second.getNumerator());
temp.setDenominator(denominator * second.getDenominator());
return temp.reduce();
}
Rational operator*(Rational second)
{
Rational temp;
temp.setNumerator(numerator * second.getNumerator());
temp.setDenominator(denominator * second.getDenominator());
return temp.reduce();
}
Rational operator/(Rational second)
{
Rational temp;
temp.setNumerator(numerator * second.getDenominator());
temp.setDenominator(denominator * second.getNumerator());
return temp.reduce();
}
//Relational operators: >=
bool operator
{
return ((double)numerator / denominator)
}
bool operator
{
return ((double)numerator / denominator)
}
bool operator>(Rational second)
{
return ((double)numerator / denominator) > ((double)second.getNumerator() / second.getDenominator());
}
bool operator>=(Rational second)
{
return ((double)numerator / denominator) >= ((double)second.getNumerator() / second.getDenominator());
}
//Equality operators: == !=
bool operator==(Rational second)
{
return ((double)numerator / denominator) == ((double)second.getNumerator() / second.getDenominator());
}
bool operator!=(Rational second)
{
return ((double)numerator / denominator) != ((double)second.getNumerator() / second.getDenominator());
}
void setNumerator(int num)
{
numerator = num;
}
void setDenominator(int den)
{
denominator = den;
}
int getNumerator()
{
return numerator;
}
int getDenominator()
{
return denominator;
}
//Method to reduce Rational
int gcd() // returns the greatest common divisor of num and denom
{
int mn = min(); // min of num and denom
int mx = max(); // mX of num and denom
for (int x = mn; x > 0; x--) // find the greatest common divisor
if (mx % x == 0 && mn % x == 0) return x;
return 1;
}
Rational reduce() // simplify the Rational number
{
int tmp = gcd();
return Rational(numerator / tmp, denominator / tmp);
}
int max() { return (numerator >= denominator) ? numerator : denominator; }
int min() { return (numerator >= denominator) ? denominator : numerator; }
//Stream Insertion:
friend ostream &operator
//Stream Extraction: >> (i.e. 3/7 form)
friend istream &operator>>(istream &, Rational &);
};
ostream &operator
{
output
return output;
}
istream &operator>>( istream &input, Rational &c )
{
char ch;
input >> c.numerator >> ch >> c.denominator;
return input;
}
int main() // make sure to simplify/reduce in operator >>
{
Rational f1, f2, sum, diff, prod, quo;
cout
cin >> f1;
cout
cin >> f2;
cout
cout
sum = f1 + f2;
diff = f1 - f2;
prod = f1 * f2;
quo = f1 / f2;
cout
cout
cout
cout
if(f1
cout
else
cout
if(f1
cout
else
cout
if(f1 > f2)
cout "
else
cout "
if(f1 >= f2)
cout= "
else
cout= "
if (f1 == f2)
cout
else
cout
if (f1 != f2)
cout
else
cout
return 0;
}
Question #1: Define a class for rational numbers. A rational number is a number that can be represented as the quotient of two integers. For example, 1/2, 3/4, 64/2, and so forth are all rational numbers. Represent rational numbers as two pointers to integers, one for the numerator and one for the denominator. Call the class Rational. Include a constructor with two arguments that can be used to set the member variables of an object to any legitimate values. Also, include a constructor that has only a single parameter of type int; call this single parameter whole number and define the constructor so that the object will be initialized to the rational number whole number/1. Also, include a default constructor that initializes an object to 0 (that is, to 0/1). Write input and output functions. Numbers are to be output in the form 1/2, 15/32, 300/401, and so forth. Note that the numerator, the denominator, or both may contain a minus sign, so-1/2, 15/- 32, and 300 401 are also possible inputs. Overload all of the following operators so that they correctly apply to the type Rational K, and Hints: Two rational numbers a/b and c/d are equal if a d equals c*b. If b and d are positive rational numbers, a/b is less than c d provided a d is less than c You should include a function to normalize the values stored so that, after normalization, the denominator is positive and the numerator and denominator are as small as possible. For example, after normalization 4/- 8 would be represented the same as 1/2
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
