Assignment 4: Fraction

due: 11:59 PM, Tuesday, Feb. 11, 2003

The first part of this assignment is to define and implement a Fraction class. You should overload the following operators as member functions: +=, -=, *=, /=, = (assignment operator). You should also overload the following operators as non-member friend functions: (+, -, *, /, >>, <<, <, >, ==, !=, >=, <=). You should also implement a constructor that creates a fraction from a long.

Here is a starting point for your Fraction.h:


class Fraction {
  ...
public:
  Fraction(long);
  ...
  friend Fraction operator+(const Fraction &, const Fraction &); 
  Fraction & operator+=(const Fraction &);
  ...
}

The second part of this assignment is to implement a template function that takes as arguments an array of ints, doubles, or Fractions and the size of the array. The function should return the maximal value (an int, double, or Fraction) that can be obtained by placing an operator (+,-,*,/) between every two consecutive numbers in the array and computing the resulting expression in the old calculator style. For example, if the array is 1,2,3 then the maximal value is 1+2*3 = 9. If the array is 1/2, 3/4, 6/5 then the maximal value is 1/2 + 3/4 + 6/5 = 49/20.

The third part of your assignment is to test your Fraction class and template function by implementing a main function that:

  1. Asks the user to choose the type of numbers: int, double, or Fraction.
  2. Asks the user to input the size of the array.
  3. Asks the user to input the numbers in the array.
  4. Calls the template function for this array and outputs the maximal value.

Example:

joe@prelude% fractions
Please choose the number type ("int", "double", "fraction") or enter "quit": int
Please enter the size of the array: 3
Please enter a number: 1
Please enter a number: 2
Please enter a number: 3
The maximal value for 1,2,3 is 9!
Please choose the number type ("int", "double", "fraction") or enter "quit": fraction
Please enter the size of the array: 3
Please enter a number: 1/2
Please enter a number: 3/4
Please enter a number: 6/5
The maximal value for 1/2,3/4,6/5 is 49/20!
Please choose the number type ("int", "double", "fraction") or enter "quit": quit
joe@prelude%

Note:   The red text is user input. The blue text is the output of your program.

Submission Instructions

Do all the following while logged into classes.cs.uchicago.edu.

  1. Create an empty directory called hw4.
  2. Put all your program source files and Makefile in this directory.
  3. main.cc should begin with a comment along the lines of
       /*********************************************
        *                                           *   
        *  I, Joe Schmoe, a.k.a. jschmoe@cs, wrote  *
        *  and tested this program myself, without  *
        *  assistance or collaboration.             *
        *                                           *  
        *********************************************/
    
    Of course, this does not preclude you from asking general questions about C++ (to anyone), or from asking the instructor or TA's for clarification of the assignment.
  4. Compile your code by typing make at the command line. This should be set up to produce an executable file called fractions.
  5. Do a sample run of your program, and copy the results, including the user input, into a text file called trial.txt. Choose the trial input carefully to demonstrate that your program functions correctly even on "hard" cases. The contents of this file must agree with the actual behavior of your program.
  6. Delete all compiled, object, and junk files. Make sure all original source files and your Makefile are still present. If your Makefile is set up right, typing make distclean should take care of this for you.
  7. cd ..   to change to the parent directory.
  8. Submit your assignment with the command
    hwsubmit 11600 hw4
    
    You should get a message indicating that the correct files are being copied. If you get error messages or no message, then send email to one of the TA's before the submission deadline.
  9. Remark: Although very little credit will be awarded to programs which do not compile, still less will be given to programs turned in late. Please allow enough time to do all the submission steps before the deadline. Also, note that there is no penalty for repeat submissions (up to the deadline), so if you submit early, then discover a hidden bug or add another feature, you may fix it and resubmit without penalty. In this case, you should include a comment explaining the change.