Project02
Home ] Up ]

 

CSIS 250 Summer 2000 Assignment #2

Date Assigned:            July 13, 2000

Date Due:                     July 18, 2000 at 10:00 AM

Create a class called CRational for performing arithmetic with fractions.  Information about fractions can be obtained at the following Web site.

http://www.sosmath.com/algebra/fraction/frac3/frac3.html

Write a main driver program and several test functions (one for each function member or friend function) to test your class.

Use integer variables to represent the private data of the class-the numerator and 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 (numerator = 1 and denominator  = 1) in case no initializers are provided and should store the fraction in reduced form (i.e., the fraction 2/4 would be stored in the object as 1 in the numerator and 2 in the denominator).  Provide the following functions:

a)     Constructor function with given values for numerator and denominator with default values

b)     constructor function to create a random fraction

c)      simplify function to simplify Crational numbers

d)     gcd function  to calculate the greatest common divisor of two integers

e)     add function for two CRational numbers.  The result should be stored in reduced form.

f)        subtract function for two CRational numbers.  The result should be stored in reduced form

g)     multipliply function for two CRational numbers.  The result should be stored in reduced form

h)     divide function for two CRational numbers.  The result should be stored in reduced form

i)        display function for printing CRational numbers in the form a/b where a is the numerator and b is the denominator

j)        displayValue function for printing CRational numbers in floating-point format

 
class Crational
  {
  private:
    int den;
    int num;
  public:
    CRational (int den=1, int num=1);   //  constructor
    CRational (char ch);                //  constructor
    void simplify(void);                //  member function to simplify
    CRational add(CRational rat);       //  member function to add
    CRational subtract(CRational rat);  //  member function to subtract
    CRational multiply(CRational rat);  //  member function to multiply
    CRational divide(CRational rat);    //  member function to divide
    void display(void);                 //  member function to display
    void displayValue(void);            //  member function to display float
   };

int gcd(int x, int y);                  //  global function for gcdt num;

Self-Evaluation Sheet Project #2

Component

Max Score

Your Score

Default constructor

4

 

A constructor with given values for numerator and denominator

4

 

A constructor to create random fraction

4

 

Function add

4

 

Function subtract

4

 

Function multiply

4

 

Function divide

4

 

Function display

4

 

Function displayValue

4

 

function gcd

4

 

Total points (1 point for function source code, 1 point for algorithm, 1 point for test function, and 1 point for test run make 4 points for each function).  A function should work all the time in order to get any credit for it.

40

 

 

Submit the following:

  1. Properly commented printed source code with algorithms
  2. Properly labeled printed output from test runs
  3. Self-evaluation sheet