Question: Can you make the program to reduce the final results and show the simplest fractions? main.cpp: #include #include fraction.h using namespace std; int main() {

Can you make the program to reduce the final results and show the simplest fractions?

main.cpp:

#include

#include "fraction.h"

using namespace std;

int main()

{

fraction f1(9, 8);

fraction f2(2, 3);

fraction result;

cout << "The result starts off at ";

result.print();

cout << endl;

cout << "The product of ";

f1.print();

cout << " and ";

f2.print();

cout << " is ";

result = f1.multipliedBy(f2);

result.print();

cout << endl;

cout << "The quotient of ";

f1.print();

cout << " and ";

f2.print();

cout << " is ";

result = f1.dividedBy(f2);

result.print();

cout << endl;

cout << "The sum of ";

f1.print();

cout << " and ";

f2.print();

cout << " is ";

result = f1.addedTo(f2);

result.print();

cout << endl;

cout << "The difference of ";

f1.print();

cout << " and ";

f2.print();

cout << " is ";

result = f1.subtract(f2);

result.print();

cout << endl;

if (f1.isEqualTo(f2)) {

cout << "The two Fractions are equal." << endl;

}

else {

cout << "The two Fractions are not equal." << endl;

}

const fraction f3(12, 8);

const fraction f4(202, 303);

result = f3.multipliedBy(f4);

cout << "The product of ";

f3.print();

cout << " and ";

f4.print();

cout << " is ";

result.print();

cout << endl;

system("pause");

}

/*

The result starts off at 0/1

The product of 9/8 and 2/3 is 18/24

The quotient of 9/8 and 2/3 is 27/16

The sum of 9/8 and 2/3 is 43/24

The difference of 9/8 and 2/3 is 11/24

The two Fractions are not equal.

The product of 12/8 and 202/303 is 2424/2424

*/

fraction.cpp:

#include

#include "fraction.h"

using namespace std;

fraction::fraction()

{

numer = 0;

denom = 1;

}

fraction::fraction(int inNumer, int inDenom)

{

numer = inNumer;

denom = inDenom;

}

fraction fraction::multipliedBy(const fraction otherFraction) const

{

int newNumer;

int newDenom;

newNumer = numer * otherFraction.numer;

newDenom = denom * otherFraction.denom;

fraction newFraction(newNumer, newDenom);

return newFraction;

}

fraction fraction::addedTo(const fraction otherFraction) const

{

int newNumer;

int newDenom;

newDenom = denom * otherFraction.denom;

newNumer = (numer * otherFraction.denom) + (denom * otherFraction.numer);

fraction newFraction(newNumer, newDenom);

return newFraction;

}

fraction fraction::subtract(const fraction otherFraction) const

{

int newNumer;

int newDenom;

newDenom = denom * otherFraction.denom;

newNumer = (numer * otherFraction.denom) - (denom * otherFraction.numer);

fraction newFraction(newNumer, newDenom);

return newFraction;

}

fraction fraction::dividedBy(const fraction otherFraction) const

{

int newNumer;

int newDenom;

newNumer = numer * otherFraction.denom;

newDenom = denom * otherFraction.numer;

fraction newFraction(newNumer, newDenom);

return newFraction;

}

bool fraction::isEqualTo(const fraction otherFraction) const

{

return ((numer == otherFraction.numer) && (denom == otherFraction.denom));

}

void fraction::print() const

{

cout << numer << "/" << denom;

}

fraction.h:

#pragma once

/*

Header file for the fraction class that creates fractions given a user input numerator and denominator.

This file adds, subtracts, multiplys, divides, and compares them with other fractions.

The punlic member functions of the fraction class:

fraction(int inNumer, int inDenom) : Pre-condition: inNumer and inDenom must be positive numbers and inDenom must not be 0

Post-condition: a fraction object of numerator inNumer and denominator inDenom will be initialized and then stored in reduced form

Pre-conditions for ALL following members: none.

fraction() :Post-condition: a fraction object of numerator 0 and denominator 1 will be initialized

MultipliedBy(const fraction otherFraction) const :Post-condition: multiplies the calling fraction by the parameter fraction to initialize a new fraction

AddedTo(const fraction otherFraction) const : Post-condition: adds the calling fraction by the parameter fraction to initialize a new fraction

Subtract(const fraction otherFraction) const : Post-condition: subtracts the calling fraction by the parameter fraction to initialize a new fraction

DividedBy(const fraction otherFraction) const : Post-condition: divides the calling fraction by the parameter fraction to create a new fraction

isEqualTo(const fraction otherFraction) const : Post-condition: returns a 'true' or 'false' after comparing two fractions to see if they are equal

print() const : Post-condition: the calling fraction will be printed as 'numerator/denominator'. Improper fractions are left as is.

*/

#ifndef fraction_h

#define fraction_h

class fraction {

public:

fraction();

fraction(int inNumer, int inDenom);

fraction multipliedBy(const fraction otherFraction) const;

fraction addedTo(const fraction otherFraction) const;

fraction subtract(const fraction otherFraction) const;

fraction dividedBy(const fraction otherFraction) const;

bool isEqualTo(const fraction otherFraction) const;

void print() const;

private:

int numer;

int denom;

};

#endif

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!