Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Description Create simple Fraction class that can be used in a C++ program to perform addition, subtraction, multiplication and division of rational numbers. Implementation You

Description
Create simple Fraction class that can be used in a C++ program to perform addition,
subtraction, multiplication and division of rational numbers.
Implementation
You will design and implement a class Fraction that encapsulates all functions related to the
processing of fractions. The << operator must be overloaded to write fractions on standard
output. The class Fraction must be defined in a source file Fraction.cpp and declared in
a header file Fraction.h. The implementation of the member functions should be defined in
the file Fraction.cpp.
The program testFraction.cpp (provided) must not be modified. It #includes the
header Fraction.h and uses the Fraction class in ways that define all the member
functions and operators that the Fraction class must implement. The internal representation of
the fraction should be in reduced form, i.e. using a pair of numbers that have no common factors.
Constructors and other member functions must ensure that the fraction is always reduced. Use
Euclid's algorithm to simplify fractions to reduced form. An example of a C implementation
reduce_fraction.c of this algorithm is provided in the file lecture1.zip. Do not
include the file reduce_fraction.c in your submission. The operators '+', '-', '*',
'/' and '=' must be overloaded and defined. Member functions getNum() and getDen()
should be implemented, returning the (reduced) numerator and denominator of the fraction.
Running the testFraction program must reproduce exactly the test output file
testFraction.out provided (including the same amount of white space). Additional test
programs will be compiled and linked to the Fraction class when grading. The
testFraction program must build using the provided Makefile without generating any
error messages or warnings.
Error processing and special cases
Any attempt to create  fraction with zero denominator should be handled by throwing an
exception of type invalid_argument defined in , including an appropriate
message (see testFraction.out to identify the message).
Submission
Create  tar file named hw1p2.tar containing the files Fraction.h and Fraction.cpp
only. Do not compress the tar file. Include your name and Student ID in a comment at the top of
each submitted file. Submit the file hw1p2.tar on Gradescope.


Here is the Fraction.cpp file:

// // testFraction.cpp // // DO NOT MODIFY THIS FILE //   #include "Fraction.h" #include using namespace std;   void print_fraction(const Fraction& f) {  cout << " print_fraction: " << f.getNum() << "/" << f.getDen() << endl; }   int main() {  Fraction w;  w = 4;  Fraction x(4,6);  Fraction y(5,6);  Fraction z(0);   cout << " w = " << w << endl;  cout << " x = " << x << endl;  cout << " y = " << y << endl;  cout << " z = " << z << endl;   cout << " x+y = " << x + y << endl;  cout << " x-y = " << x - y << endl;  cout << " x*y = " << x * y << endl;  cout << " x/y = " << x / y << endl;   cout << " -x = " << -x << endl;  cout << " x+2 = " << x + 2 << endl;  cout << " 2+x = " << 2 + x << endl;  cout << " x-2 = " << x - 2 << endl;  cout << " 2-x = " << 2 - x << endl;  cout << " 2*x = " << 2 * x << endl;  cout << " x*2 = " << x * 2 << endl;  cout << " x/2 = " << x / 2 << endl;  cout << " 2/x = " << 2 / x << endl;   cout << " w+x+y = " << w + x + y << endl;   print_fraction(y);   try  {    cout << " x / z = " << x / z << endl;  }  catch ( invalid_argument& e )  {    cout << "Exception: " << e.what() << endl;  }   cout << " 2 * ( x + y ) = " << 2 * ( x + y ) << endl;  Fraction u(12,18);  print_fraction(u); }

Here is the makefile:

# DO NOT MODIFY THIS FILE CXX=g++ CXXFLAGS=-Wall EXECS=testFraction all: $(EXECS) testFraction: testFraction.o Fraction.o $(CXX) -o $@ $^ clean: rm -f *.o $(EXECS)

This is the output you are supposed to get:

 w = 4 x = 2/3 y = 5/6 z = 0 x+y = 3/2 x-y = -1/6 x*y = 5/9 x/y = 4/5 -x = -2/3 x+2 = 8/3 2+x = 8/3 x-2 = -4/3 2-x = 4/3 2*x = 4/3 x*2 = 4/3 x/2 = 1/3 2/x = 3 w+x+y = 11/2 print_fraction: 5/6 x / z = Exception: zero denominator 2 * ( x + y ) = 3 print_fraction: 2/3

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

College Mathematics for Business Economics Life Sciences and Social Sciences

Authors: Raymond A. Barnett, Michael R. Ziegler, Karl E. Byleen

12th edition

321614003, 978-0321614001

More Books

Students also viewed these Algorithms questions

Question

In problem, find f (x). f(x) = ln x + 2ex - 3x2

Answered: 1 week ago