Hi i need help in the c++ assignement. The instructions are provided in the firs
ID: 3688919 • Letter: H
Question
Hi i need help in the c++ assignement. The instructions are provided in the first picture . The Client program( can not be changed) is provided and the Required/expected output is also provided.
CLIENT PROGRAM:
OUTPUT:
Assignment 11.1(45 points) For this assignment you will be building on your fraction class However, the changes will be significant, so 1 would recommend starting from scratch and using your previous version as a resource when appropriate. You' continue working on your fraction class for Here are the client program and correct output Your class should support the following operations on fraction Construction of a fraction from two, one, or zero integer arguments. If two arguments, they are assumed to be the numerater and denominatst one is assumed to be a whole number, and aero argui entscreetes wutracten. Use erault parameters se that you only need single function to implement aill three of these constructors You should check to to 0. The easiest way to do this is to use an assert statement: assert(inDeronwator-O); You can put this statementthe top of your constructor. Note that the variable in the assert0 s the incoming parameter, not the data member. In order to use assertt), you must rinclude ccassert> For this assignment, you may ansume that all fractions are positive. We'll fx that next week you mwy .wrn that all fractions are Printing a fraction to a stream with an overloadedExplanation / Answer
/*
***CLASS INVARIANT:
There are two data members in this class. Numerator and denominator.
Fractions are always stored as an improper fraction in is simpliest form.
Fractions with a numerator of 0 will have their denominator set to 1.
Fractions entered with a negative denominator will have the negative symbol
moved to the numerator.
*/
#include <iostream>
#include "fraction.h"
using namespace std;
fraction::fraction(int inNumerator, int inDenominator)
{
numerator = inNumerator;
denominator = inDenominator;
reduce();
}
/*
Pre: An object is ready for output.
Post: The calling object is displayed as a Whole number, proper fraction, or a
mixed fraction. Mixed fractions have their whole number seperated by a '+'.
*/
ostream& operator<<(ostream& out, const fraction& right)
{
if (right.denominator == 1){
out << right.numerator;
} else if (abs(right.numerator) < right.denominator){
out << right.numerator << "/" << right.denominator;
} else {
out << right.numerator/right.denominator << "+" <<
(abs(right.numerator % right.denominator)) << "/" <<
right.denominator;
}
return out;
}
/*
Pre: An object is ready to read in.
Post: First we peek at the first character to determine if the fraction being
read in is negative or not. First numbers read in are assumed to be the
numerator unless through the in.peek() we find a '+', then the read in value
is assigned to wholeNumer. Continues reading in for the numerator untill a
'/' is found. This ends the numerator read in, then following numbers read
in are assigned to the denominator. Mixed fractions are converted to
improper fractions and reduced.
*/
istream& operator>>(istream& in, fraction &fractionRead)
{
int wholeNumber;
int posOrNeg = 1;
if (in.peek() == '-'){
posOrNeg = -1;
in.ignore();
}
in >> fractionRead.numerator;
if (in.peek() == '+'){
wholeNumber = fractionRead.numerator;
in.ignore();
in >> fractionRead.numerator;
}
if (in.peek() == '/'){
in.ignore();
in >> fractionRead.denominator;
} else {
fractionRead.denominator = 1;
}
fractionRead.numerator = posOrNeg * (wholeNumber * fractionRead.denominator + fractionRead.numerator);
fractionRead.reduce();
return in;
}
/*
Post: The calling object is reduced to it's most reduced fractoin.
Any negative denominators have their negative sign moved to the denominator.
Any calling object with a numerator of 0 has it's denominator changed to a
1. Fractions get reduced by determining the Greatest Common Factor, and
dividing both the numerator and denominator by the gcf.
*/
void fraction::reduce()
{
int gcf;
if (denominator < 0){
denominator = denominator * -1;
numerator = numerator * -1;
}
if (numerator == 0){
denominator = 1;
}
gcf = abs(numerator);
while ( gcf > 0 && ((numerator % gcf !=0) || (denominator % gcf !=0))) {
gcf--;
}
if (gcf > 0){
numerator /= gcf;
denominator /= gcf;
}
}
fraction operator+(const fraction &left, const fraction &right)
{
fraction sum;
sum.numerator = ((left.numerator * right.denominator) + (right.numerator * left.denominator));
sum.denominator = (left.denominator * right.denominator);
sum.reduce();
return sum;
}
fraction operator-(const fraction &left, const fraction &right)
{
fraction difference;
difference.numerator = ((left.numerator * right.denominator) - (right.numerator * left.denominator));
difference.denominator = (left.denominator * right.denominator);
difference.reduce();
return difference;
}
fraction operator*(const fraction &left, const fraction &right)
{
fraction product;
product.numerator = (left.numerator * right.numerator);
product.denominator = (left.denominator * right.denominator);
product.reduce();
return product;
}
fraction operator/(const fraction &left, const fraction &right)
{
fraction quotient;
quotient.numerator = (left.numerator * right.denominator);
quotient.denominator = (left.denominator * right.numerator);
quotient.reduce();
return quotient;
}
fraction fraction::operator+=(const fraction &right)
{
*this = *this + right;
return *this;
}
fraction fraction::operator-=(const fraction &right)
{
*this = *this - right;
return *this;
}
fraction fraction::operator*=(const fraction &right)
{
*this = *this * right;
return *this;
}
fraction fraction::operator/=(const fraction &right)
{
*this = *this / right;
return *this;
}
fraction fraction::operator++()
{
numerator = numerator + denominator;
return *this;
}
fraction fraction::operator++(int)
{
fraction temp(numerator, denominator);
numerator = numerator + denominator;
return temp;
}
fraction fraction::operator--()
{
numerator = numerator - denominator;
return *this;
}
fraction fraction::operator--(int)
{
fraction temp(numerator, denominator);
numerator = numerator - denominator;
return temp;
}
bool operator<(const fraction &left, const fraction &right)
{
return (left.numerator * right.denominator) < (right.numerator * left.denominator);
}
bool operator>(const fraction &left, const fraction &right)
{
return operator<(right, left);
}
bool operator==(const fraction &left, const fraction &right)
{
return (left.numerator * right.denominator) == (right.numerator * left.denominator);
}
bool operator!=(const fraction &left, const fraction &right)
{
return !operator==(left, right);
}
bool operator<=(const fraction &left, const fraction &right)
{
return !operator>(left, right);
}
bool operator>=(const fraction &left, const fraction &right)
{
return !operator<(left, right);
}
/*
*** File: fraction.h
*** The fraction class can be used to show the ratios of two numbers, in the
form of a proper fraction with a numerator and denominator, and a mixed
fraction with a whole number, numerator and denominator. All the
basic arithmetic operations, shorthand arithmetic operations, the
increment and decrement operations, and all relation operations. The class
provides as well a stream extraction and stream insertion operations.
* Constructor. Creates a fraction with a Numerator of inNumerator, and a
* Denominator of inDenominator.
fraction(int inNumerator=0, int inDenominator=1);
* Pre: Object is ready to be output.
* Post: The calling object is displayed as a Whole number, proper fraction, or a
mixed fraction. Mixed fractions have their whole number seperated by a '+'.
friend ostream& operator<<(ostream& out, const fraction &right);
* Pre: An object is ready to read in.
* Post: First we peek at the first character to determine if the fraction being
read in is negative or not. First numbers read in are assumed to be the
numerator unless through the in.peek() we find a '+', then the read in value
is assigned to wholeNumer. Continues reading in for the numerator untill a
'/' is found. This ends the numerator read in, then following numbers read
in are assigned to the denominator. Mixed fractions are converted to
improper fractions and reduced.
* Returns the sum of the left and right fractions in a reduced from.
friend istream& operator>>(istream& in, fraction &fractionRead);
friend fraction operator+(const fraction &left, const fraction &right);
* Returns the difference of the left and right fractions in a reduced form.
friend fraction operator-(const fraction &left, const fraction &right);
* Returns the product of the left and right fractions in a reduced from.
friend fraction operator*(const fraction &left, const fraction &right);
* Returns the quotient of the left and right fractions in a reduced form.
friend fraction operator/(const fraction &left, const fraction &right);
* Returns the sum of the itself plus right in a reduced form.
fraction operator+=(const fraction &right);
* Returns the difference of the calling object and the value of right.
fraction operator -=(const fraction &right);
* Returns the product of the calling object and the value of right.
fraction operator*=(const fraction &right);
* Returns the quotient of the calling object and the value of right.
fraction operator/=(const fraction &right);
* Returns the calling object plus one.
fraction operator++();
* Returns the calling object, then adds one to the calling object.
fraction operator++(int);
* Returns the calling object minus one.
fraction operator--();
* Returns the calling object, then subtracts one from the calling object.
fraction operator--(int);
* Returns true if left is less than right. False if not.
friend bool operator<(const fraction &left, const fraction &right);
* Returns true if left is more than right. False if not.
friend bool operator>(const fraction &left, const fraction &right);
* Returns true if left is equal to right. False if not.
friend bool operator==(const fraction &left, const fraction &right);
* Returns true if left does not equal right. False if not.
friend bool operator!=(const fraction &left, const fraction &right);
* Returns true if left is less than or equal to right. False if not.
friend bool operator<=(const fraction &left, const fraction &right);
* Returns true if left is greater than or equal to right. False if not.
friend bool operator>=(const fraction &left, const fraction &right);
*/
#ifndef FRACTION_H
#define FRACTION_H
#include <iostream>
using namespace std;
class fraction {
public:
fraction(int inNumerator=0, int inDenominator=1);
friend ostream& operator<<(ostream& out, const fraction &right);
friend istream& operator>>(istream& in, fraction &fractionRead);
friend fraction operator+(const fraction &left, const fraction &right);
friend fraction operator-(const fraction &left, const fraction &right);
friend fraction operator*(const fraction &left, const fraction &right);
friend fraction operator/(const fraction &left, const fraction &right);
fraction operator+=(const fraction &right);
fraction operator-=(const fraction &right);
fraction operator*=(const fraction &right);
fraction operator/=(const fraction &right);
fraction operator++();
fraction operator++(int);
fraction operator--();
fraction operator--(int);
friend bool operator<(const fraction &left, const fraction &right);
friend bool operator>(const fraction &left, const fraction &right);
friend bool operator==(const fraction &left, const fraction &right);
friend bool operator!=(const fraction &left, const fraction &right);
friend bool operator<=(const fraction &left, const fraction &right);
friend bool operator>=(const fraction &left, const fraction &right);
private:
int numerator;
int denominator;
void reduce();
};
#endif
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.