Using the previous assignment expand it to include methods f

Using the previous assignment, expand it to include methods for the other rational number arithmetic operations, according to the class declaration below.

Guidelines

Maintain fractions internally in lowest terms - the best place to do this is in setFraction()

Be sure to maintain the sign of the fraction correctly

Avoid a 0 in the denominator by forcing the fraction to 0

Enhance toString() to display a mixed fraction (whole numbers and fraction) when the numerator is greater than the denominator

Use the following test driver to test the class.


The output of the test driver is:

Solution

#include<iostream>
#include<cstdio>
#include<cstring>
#include <sstream>

using namespace std;

// Helper function to convert an integer to string
string IntToString (int a)
{
ostringstream temp;
temp<<a;
return temp.str();
}

class FullFraction
{
int numerator;
int denominator;
int gcd(int x, int y);
public:
FullFraction();
FullFraction(int n, int d);
FullFraction add(FullFraction rop);
FullFraction subtract(FullFraction rop);
FullFraction multiply(FullFraction rop);
FullFraction divide(FullFraction rop);
int getNumerator() const;
void setFullFraction(int num, int denom);
int getDenominator() const;
float getDecimal() const;
std::string toString();
};
FullFraction::FullFraction(){
this->numerator = 0;
this->denominator = 1;
}

FullFraction::FullFraction(int n, int d){
int signNum = 1, signDenom = 1; // Goal is to separate sign of num and denom and then assign correct sign to num
if(n < 0)
{
signNum = -1;
n = n * (-1);
}
if(d < 0)
{
signDenom = -1;
d = d * (-1);
}
int sign = signNum * signDenom;

this->setFullFraction(sign * n,d);
}

float FullFraction::getDecimal() const
{
return this->numerator / (float)this->denominator;
}
int FullFraction::getDenominator() const
{
return this->denominator;
}
int FullFraction::getNumerator() const
{
return this->numerator;
}
FullFraction FullFraction::add(FullFraction rop)
{
int num, denom;
denom = this->denominator * rop.denominator;
num = this->denominator * rop.numerator + this->numerator * rop.denominator; // simple addition
return FullFraction(num,denom);
}
FullFraction FullFraction::subtract(FullFraction rop)
{
FullFraction rop2(rop.numerator * (-1) , rop.denominator); // negate sign of second fraction and add the fractions
return this->add(rop2);
}
FullFraction FullFraction::multiply(FullFraction rop)
{
return FullFraction(this->numerator * rop.numerator, this->denominator * rop.denominator); // simple multiplication
}
FullFraction FullFraction::divide(FullFraction rop)
{
return FullFraction(this->numerator * rop.denominator, this->denominator * rop.numerator); // simple division
}

int FullFraction::gcd(int x, int y)
{
if (x <= 1 || y <= 1)
return 1;
while (x != y)
{
if (x > y)
{
if (0 == (x = x % y))
return y;
}
else
{
if (0 == (y = y % x))
return x;
}
}
return x;
}

void FullFraction::setFullFraction(int num, int denom){
// Goal is to reduce the fraction by dividing the num and denom by their gcd
int sign = 1;
if(num < 0){
sign = -1;
num = num * (-1);
}
int factor = gcd(num, denom);
num/= factor;
denom/= factor;
this->numerator = num * sign;
this->denominator = denom;
}

string FullFraction::toString(){
int num = this->numerator, denom = this->denominator;
int sign = 1, mixed = 0;
// sign stores the sign of the fraction, mixed will store the whole number in case of improper fraction
if(num == 0)
{
return \"0\"; // 0 returned in case of fraction = 0
}
else if(denom == 1)
{
return IntToString(num); // if denominator of fraction is 1 then only numerator returned
}
// Storing the sign of the fraction in sign
if(num < 0)
{
sign = -1;
num *= sign; // making the numerator positive since sign now stores the fraction of the sign
}
// In case of improper fraction
if(num > denom){
mixed = num / denom; // storing the whole number
num = num % denom;
return IntToString(mixed *sign) + \" \" + IntToString(num) + \"/\" + IntToString(denom);
}

return IntToString(num*sign) + \"/\" + IntToString(denom);
}

Using the previous assignment, expand it to include methods for the other rational number arithmetic operations, according to the class declaration below. Guide
Using the previous assignment, expand it to include methods for the other rational number arithmetic operations, according to the class declaration below. Guide
Using the previous assignment, expand it to include methods for the other rational number arithmetic operations, according to the class declaration below. Guide

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site