In: Computer Science
Wondering where to start on this C++ homework assignment involving friend functions and overloading operators implemented in a Rational class. These are the instructions:
Your class will need to store two internal, integer values for each Rational number, the numerator (top) and denominator (bottom) of the fraction. It will have three constructor functions, with zero, one and two arguments, used as follows:
Rational test1, test2(10), test3(1, 2);
The declaration for test1 calls the default (no argument) constructor, which should set the value to 0. (Like any other whole number, zero will have a 1 in the denominator: we cannot divide by zero.) The declaration for test2 calls a constructor with one argument. The value for test2 will be 10, stored as 10 on the top and 1 on the bottom. The declaration for test3 calls the constructor with two arguments. test3 is equal to 0.5, with 1 on the top and 2 on the bottom.
All of the operators required in the problem should be provided for the Rational class as overloaded, Friend functions. In general, you should be able to execute code such as following:
// Display the three values to test cout
cout << "\nTest1 equals " <<
test1;
cout << "\nTest2 equals " <<
test2;
cout << "\nTest3 equals " <<
test3;
// Test our operators
cout << "\nTest1 * Test2 equals " <<
test1*test2;
cout << "\nTest1 / Test3 equals " <<
test1/test3;
cout << "\nTest2 + Test3 equals " <<
test2+test3;
cout << "\nTest3 - Test1 equals " <<
test3-test1;
if (test1 == test2)
cout << "\nTest1
is equal to Test2";
if (test1 < test2)
cout << "\nTest1
is less than Test2";
and so on until you test all the implemented member functions and Friend functions of the Rational class.
This is what I have so far
#include <fstream>
#include <iostream>
#include <stdlib.h>
#include <time.h>
class Rational
{
private:
int numerator;
int denominator;
void reduce();
public:
Rational();
Rational(int n, int
d);
int
getNumerator();
int
getDenominator();
void add(Rational
addend);
void sub(Rational
subtractor);
void mul(Rational
multiplicand);
void div(Rational
dividend);
int
greatestCommonDenominator(int a,int b);
bool less(Rational
comparator);
bool eq(Rational
comparator);
bool neq(Rational
comparator);
void
output(std::ostream& oss);
};
Rational::Rational() {
numerator = 0;
denominator = 1;
}
Rational::Rational(int n, int d) {
// cout<<"Creating rational number
"<<n<<"/"<<d<<endl;
numerator = n;
denominator = d;
if (numerator < 0 and denominator < 0)
{
numerator = -numerator;
denominator = - denominator;
}
if (numerator*denominator < 0) {
cout<<"Error negative
fraction"<<std::endl;
exit(1);
}
}
int Rational::getNumerator() {
return numerator;
}
int Rational::getDenominator() {
return denominator;
}
int Rational::greatestCommonDenominator(int a, int b) {
//cout<<"Finding greatest common denominator of
"<<a<<" and "<<b<<endl;
int gcd = 1;
int smallerNum = a;
if ( a > b ) {
smallerNum = b;
}
for ( int i = 1; i <= smallerNum; i++ ) {
if (((a % i) == 0) and ((b % i) ==
0)) {
gcd = i;
}
}
//cout<<"Greatest common denominator is
"<<gcd<<endl;
return gcd;
}
void Rational::reduce() {
int gcd = greatestCommonDenominator(numerator,
denominator);
numerator = numerator / gcd;
denominator = denominator / gcd;
}
void Rational::add(const Rational addend) {
numerator = numerator*addend.denominator +
addend.numerator*denominator;
denominator = denominator*addend.denominator;
reduce();
return;
}
void Rational::sub(Rational subtractor) {
numerator = numerator*subtractor.denominator -
subtractor.numerator*denominator;
denominator =
denominator*subtractor.denominator;
if ( numerator < 0 ) {
cout<<"Error - negative
fraction"<<std::endl;
}
reduce();
return;
}
void Rational::mul(Rational multiplicand) {
numerator *= multiplicand.numerator;
denominator *= multiplicand.denominator;
reduce();
}
void Rational::div(Rational dividend) {
denominator *= dividend.numerator;
numerator *= dividend.denominator;
reduce();
}
bool Rational::less(Rational comparator) {
return (numerator*comparator.denominator <
comparator.numerator*denominator);
}
bool Rational::eq(Rational comparator) {
return (numerator*comparator.denominator ==
comparator.numerator*denominator);
}
bool Rational::neq(Rational comparator) {
return !eq(comparator);
}
void Rational::output(std::ostream& oss) {
oss<<numerator<<"/"<<denominator<<std::endl;
}
int main() {
Rational fraction1(1,2);
Rational fraction2(2,4);
fraction1.add(fraction2);
fraction1.sub(fraction2);
if (fraction1.eq(fraction2)) {
cout<<"Add and eq work
great"<<endl;
}
cout<<"Just checking"<<endl;
fraction1.output(cout);
fraction2.output(cout);
//insert any test code for Rational class
return (0);
}
#include <fstream>
#include <iostream>
#include <stdlib.h>
#include <time.h>
class Rational
{
private:
int numerator;
int denominator;
void reduce();
public:
Rational();
Rational(int n); // add constructor that takes only numerator
Rational(int n, int d);
int getNumerator();
int getDenominator();
void add(Rational addend);
void sub(Rational subtractor);
void mul(Rational multiplicand);
void div(Rational dividend);
int greatestCommonDenominator(int a,int b);
bool less(Rational comparator);
bool eq(Rational comparator);
bool neq(Rational comparator);
void output(std::ostream& oss);
friend Rational operator +(const Rational &r1, const
Rational &r2);
friend Rational operator -(const Rational &r1, const Rational
&r2);
friend Rational operator *(const Rational &r1, const Rational
&r2);
friend Rational operator /(const Rational &r1, const Rational
&r2);
friend bool operator ==(const Rational &r1, const Rational
&r2);
friend bool operator !=(const Rational &r1, const Rational
&r2);
friend bool operator <(const Rational &r1, const Rational
&r2);
friend bool operator >(const Rational &r1, const Rational
&r2);
friend bool operator <=(const Rational &r1, const Rational
&r2);
friend bool operator >=(const Rational &r1, const Rational
&r2);
friend std::ostream& operator <<(std::ostream
&outs, const Rational &r);
friend std::istream& operator >>(std::istream &ins,
Rational &r);
};
Rational::Rational() {
numerator = 0;
denominator = 1;
}
Rational::Rational(int n)
{
numerator = n; // set numerator to n
denominator = 1; // set denominator to 1
}
Rational::Rational(int n, int d) {
// cout<<"Creating rational number
"<<n<<"/"<<d<<endl;
numerator = n;
denominator = d;
if (numerator < 0 and denominator < 0) {
numerator = -numerator;
denominator = -denominator;
}
if (numerator*denominator < 0) {
std::cout<<"Error negative fraction"<<std::endl;
exit(1);
}
}
int Rational::getNumerator() {
return numerator;
}
int Rational::getDenominator() {
return denominator;
}
int Rational::greatestCommonDenominator(int a, int b) {
//cout<<"Finding greatest common denominator of
"<<a<<" and "<<b<<endl;
int gcd = 1;
int smallerNum = a;
if ( a > b ) {
smallerNum = b;
}
for ( int i = 1; i <= smallerNum; i++ ) {
if (((a % i) == 0) and ((b % i) == 0)) {
gcd = i;
}
}
//cout<<"Greatest common denominator is
"<<gcd<<endl;
return gcd;
}
void Rational::reduce() {
int gcd = greatestCommonDenominator(numerator, denominator);
numerator = numerator / gcd;
denominator = denominator / gcd;
}
void Rational::add(const Rational addend) {
numerator = numerator*addend.denominator +
addend.numerator*denominator;
denominator = denominator*addend.denominator;
reduce();
return;
}
void Rational::sub(Rational subtractor) {
numerator = numerator*subtractor.denominator -
subtractor.numerator*denominator;
denominator = denominator*subtractor.denominator;
if ( numerator < 0 ) {
std::cout<<"Error - negative
fraction"<<std::endl;
}
reduce();
return;
}
void Rational::mul(Rational multiplicand) {
numerator *= multiplicand.numerator;
denominator *= multiplicand.denominator;
reduce();
}
void Rational::div(Rational dividend) {
denominator *= dividend.numerator;
numerator *= dividend.denominator;
reduce();
}
bool Rational::less(Rational comparator) {
return (numerator*comparator.denominator <
comparator.numerator*denominator);
}
bool Rational::eq(Rational comparator) {
return (numerator*comparator.denominator ==
comparator.numerator*denominator);
}
bool Rational::neq(Rational comparator) {
return !eq(comparator);
}
void Rational::output(std::ostream& oss) {
oss<<numerator<<"/"<<denominator<<std::endl;
}
// overloaded operators as friend functions
Rational operator +(const Rational &r1, const Rational
&r2)
{
Rational r(r1.numerator*r2.denominator +
r2.numerator*r1.denominator, r1.denominator*r2.denominator);
r.reduce();
return r;
}
Rational operator -(const Rational &r1, const Rational
&r2)
{
Rational r(r1.numerator*r2.denominator -
r2.numerator*r1.denominator, r1.denominator*r2.denominator);
r.reduce();
return r;
}
Rational operator *(const Rational &r1, const Rational
&r2)
{
Rational r(r1.numerator*r2.numerator,
r1.denominator*r2.denominator);
r.reduce();
return r;
}
Rational operator /(const Rational &r1, const Rational
&r2)
{
Rational r(r1.numerator*r2.denominator ,
r1.denominator*r2.numerator);
r.reduce();
return r;
}
bool operator ==(const Rational &r1, const Rational
&r2)
{
return (r1.numerator*r2.denominator ==
r2.numerator*r1.denominator);
}
bool operator !=(const Rational &r1, const Rational
&r2)
{
return !(r1 == r2);
}
bool operator <(const Rational &r1, const Rational
&r2)
{
return (r1.numerator*r2.denominator <
r2.numerator*r1.denominator);
}
bool operator >(const Rational &r1, const Rational
&r2)
{
return (r1.numerator*r2.denominator >
r2.numerator*r1.denominator);
}
bool operator <=(const Rational &r1, const Rational
&r2)
{
return (r1.numerator*r2.denominator <=
r2.numerator*r1.denominator);
}
bool operator >=(const Rational &r1, const Rational
&r2)
{
return (r1.numerator*r2.denominator >=
r2.numerator*r1.denominator);
}
// overloaded stream insertion operator
std::ostream& operator <<(std::ostream &outs, const
Rational &r)
{
outs<<r.numerator<<"/"<<r.denominator;
return outs;
}
// overloaded stream extraction operator
std::istream& operator >>(std::istream &ins, Rational
&r)
{
char sep;
// reads the rational number in the format
numerator/denominator
ins>>r.numerator>>sep>>r.denominator;
return ins;
}
int main() {
Rational fraction1(1,2);
Rational fraction2(2,4);
fraction1.add(fraction2);
fraction1.sub(fraction2);
if (fraction1.eq(fraction2)) {
std::cout<<"Add and eq work great"<<std::endl;
}
std::cout<<"Just checking"<<std::endl;
fraction1.output(std::cout);
fraction2.output(std::cout);
//insert any test code for Rational class
Rational test1, test2(10), test3(1, 2);
// Display the three values to test cout
std::cout << "\nTest1 equals " << test1;
std::cout << "\nTest2 equals " << test2;
std::cout << "\nTest3 equals " << test3;
// Test our operators
std::cout << "\nTest1 * Test2 equals " <<
test1*test2;
std::cout << "\nTest1 / Test3 equals " <<
test1/test3;
std::cout << "\nTest2 + Test3 equals " <<
test2+test3;
std::cout << "\nTest3 - Test1 equals " <<
test3-test1;
if (test1 == test2)
std::cout << "\nTest1 is equal to Test2";
if (test1 < test2)
std::cout << "\nTest1 is less than Test2";
return (0);
}
//end of program
Output: