Question Details

ECS40 Winter 2017 2017-01-19 Homework 1 due Tue 2017-01-31 at 20:00 Use the handin directory hw1 to submit your work Fraction calculator Description...

Here's my code so far. I am having trouble with compilation errors. I need to separate this into a Fraction.h and a Fraction.cpp

class Fraction{
        : num(0), den(1) {};

    Fraction( int n, int d)
        : num(n), den(d) {};

    int getNum() const {
        return num;
    int getDen() const {
        return den;
    void setNum(int n){
         num = n;
    void setDen(int d){
         den = d;
   //overload the stream insertion and extraction operators.
   friend ostream& operator <<(ostream&, const Fraction &);
   friend istream& operator >> (istream&, Fraction &);

   //   overload the arithmetic operators
   Fraction operator+( const Fraction & );
   Fraction operator-( const Fraction & );
    int num;
    int den;

Fraction Fraction ::operator+
                  ( const Fraction & rightFraction )
   Fraction temp;

   temp.num = ( num * rightFraction.den ) + ( den * rightFraction.num );
   temp.den = den * rightFraction.den;

   return temp;
}   //   end function operator+

Fraction Fraction ::operator-( const Fraction & rightFraction )
   Fraction temp;

   temp.num = ( num * rightFraction.den ) - ( den * rightFraction.num );
   temp.den = den * rightFraction.den;

   return temp;
}   //   end function operator-

//   Overlod the stream extraction operator <<
ostream& operator<<( ostream& osObject,
const Fraction & myFraction )
   osObject << myFraction.num << "/" << myFraction.den;

   return osObject;
}   //   end function operator<<

//   Overload the stream extraction operator >>
istream& operator>>( istream& isObject, Fraction & myFraction)
   char ch;
   cout<<"Enter numerator:";
   isObject >> myFraction.num;
   cout<<"Enter denomerator:";
       isObject>> myFraction.den;
   return isObject;

ECS40 Winter 2017 2017-01-19 Homework 1 due Tue 2017-01-31 at 20:00


Use the handin directory hw1 to submit your work Fraction calculator


Description Create a simple fraction calculator that can add and subtract any number of fractions and writes


the answer as a reduced fraction.


Your program will read input from stdin and write output on stdout.


A fraction is represented as the sequence:




where a and b are integers and any amount of white space characters ' ' (including none) can


separate a from '/' and '/' from b.


Input consists of an expression made of fractions separated by the operators '+' or '-'.


The number of fractions in the expression is arbitrary. Each of the following 6 lines is an


example of valid input expression:


1/2 + 3/4








/9- 21 /




-2/-3 +7 /5 -7 Note that the numerator and/or denominator of a fraction given as input may be negative.


The input will consist of a single expression on a single line terminated by a




The output should consist of a single, irreducible fraction written as




where c is a signed integer and d is a positive integer (i.e. the denominator cannot be negative in


the output). The numbers c and d should not have any common factors (apart from 1).


Error processing and special cases You can expect the input to consist of complete expressions, i.e. there will be no incomplete or


missing fractions in the input. Fractions with a zero denominator may appear in input, and must


cause the following error message to be printed on stdout: Error: denominator is zero


with no other output.


If the answer is an integer, the program should only print that integer and not a fraction


Example: input= 3/2 + 4/8 , output = 2


Examples of input and corresponding output will be provided on the web site site . Your output should exactly match the output in the example




Implementation The main program, called calculator.cpp , is provided. Your project must include a class


Fraction that encapsulates all functions related to the processing of fractions. The &gt;&gt; and &lt;&lt;


operators must be overloaded and used to read and write fractions. The presence of a zero


denominator in input should be handled by throwing an exception of type


invalid_argument defined in &lt;stdexcept&gt;. 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 class Fraction must


include a constructor Fraction::Fraction(int a, int b). 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 of this algorithm is provided on the web site in


the example programs of Lecture 1. 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. It should be possible to use the class


Fraction in the program useFraction.cpp which #includes the header


Fraction.h. The program useFraction.cpp is provided on the web site You should not modify that program.




Create a tar file named hw1.tar containing the files calculator.cpp Fraction.cpp


Fraction.h useFraction.cpp and Makefile. Do not compress the tar file. The files


calculator.cpp and useFraction.cpp must be identical to the files provided. The


Makefile must contain a target calculator , a target useFraction and a target all. The


target all should appear first and will be used to build both executables. The Makefile should


include the necessary definition to compile C++ files with the –Wall option. Include your name


and Student ID in a comment at the top of each file (except calculator.cpp and


useFraction.cpp). Submit your project using:


$ handin cs40 hw1 hw1.tar


Solution details:

Pay using PayPal (No PayPal account Required) or your credit card . All your purchases are securely protected by .

About this Question






Oct 15, 2019





We have top-notch tutors who can do your essay/homework for you at a reasonable cost and then you can simply use that essay as a template to build your own arguments.

You can also use these solutions:

  • As a reference for in-depth understanding of the subject.
  • As a source of ideas / reasoning for your own research (if properly referenced)
  • For editing and paraphrasing (check your institution's definition of plagiarism and recommended paraphrase).
This we believe is a better way of understanding a problem and makes use of the efficiency of time of the student.


Order New Solution. Quick Turnaround

Click on the button below in order to Order for a New, Original and High-Quality Essay Solutions. New orders are original solutions and precise to your writing instruction requirements. Place a New Order using the button below.


Order Now