Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Frac.h: // a Fraction object holds one Fraction number, one fraction #ifndef FRAC_H #define FRAC_H #include using namespace std; class Fraction { // not fully

image text in transcribed

Frac.h:

// a Fraction object holds one Fraction number, one fraction

#ifndef FRAC_H

#define FRAC_H

#include

using namespace std;

class Fraction { // not fully commented

public:

Fraction(int = 0, int = 1); // default constructor

Fraction add(const Fraction &);

/* add the prototypes of required methods here.

...

*/

void printFractionAsFloat();

private:

int numerator;

int denominator;

void reduce(); // utility function, reduce to lowest terms

};

#endif

Frac.cpp:

// a Fraction object holds one Fraction number, one fraction

#include "Frac.h"

//------------------------------ Fraction ------------------------------------

// default constructor: parameters are numerator and denominator respectively

// if the number is negative, the negative is always stored in the numerator

Fraction::Fraction(int n, int d) {

numerator = (d

denominator = (d

reduce();

}

//(a)--------------------------------- add --------------------------------------

// overloaded +: addition of 2 Fractions, current object and parameter

Fraction Fraction::add(const Fraction& a) {

Fraction t;

t.numerator = a.numerator * denominator + a.denominator * numerator;

t.denominator = a.denominator * denominator;

t.reduce();

return t;

}

//(b)------------------------------ subtract ------------------------------------

// subtraction of 2 Fractions, current object and parameter

//(c)------------------------------ multiply ------------------------------------

// multiplication of 2 Fractions, current object and parameter

//(d)-------------------------------- divide ------------------------------------

// division of 2 Fractions, current object and parameter,

// division by zero crashes

//(e)---------------------------- printFraction ---------------------------------

//(f)------------------------ printFractionAsFloat ------------------------------

void Fraction::printFractionAsFloat() {

if (denominator == 0)

cout

else

cout

}

//-------------------------------- reduce ------------------------------------

// reduce fraction to lowest terms

void Fraction::reduce() {

int n = numerator

int d = denominator;

int largest = n > d ? n : d;

int gcd = 0; // greatest common divisor

for (int loop = largest; loop >= 2; loop--)

if (numerator % loop == 0 && denominator % loop == 0) {

gcd = loop;

break;

}

if (gcd != 0) {

numerator /= gcd;

denominator /= gcd;

}

}

Building a class for Fraction ADT (Part l) Create a class called Fraction for performing arithmetic with fractions. Use integer variables to represent the private data of the class-the numerator and the denominator. Provide a constructor function that enables an object of this class to be initialized when it is declared. The constructor should contain default values in case no initializers are provided. Provide public member functions for each of the following: a) Addition of two Fraction numbers. See the solution as an example below. b) Subtraction of two Fraction numbers. c) Multiplication of two Fraction numbers. d) Division of two Fraction numbers. e) Printing Fraction numbers in the form a/b where a is the numerator and b is the denominator. f) Printing Fraction numbers in double floating-point format. Note: 1. You just need to finish the skeleton codes in two files: the interface Frac.h and the implementation Frac.cpp 2. A driver program (do not change it!) to test your class:FracDriver.cpp 3. For submission, you need to upload the two source files: your Frac.h and Frac.cpp to ANGEL drop-box (please do NOT zip them)

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database In Depth Relational Theory For Practitioners

Authors: C.J. Date

1st Edition

0596100124, 978-0596100124

More Books

Students also viewed these Databases questions

Question

In an Excel Pivot Table, how is a Fact/Measure Column repeated?

Answered: 1 week ago