UMBC CS 201, Fall 05
UMBC CMSC 201 Fall '05
CSEE | 201 | 201 F'05 | lectures | news | help

ADT Example

Continued example: Fractions

Fractions have many operations on them, which could all be written as functions. It would be standard practice, if we wrote an interface (the .h file) called fraction.h that contained both the definition of the fraction structure and prototypes for all of these functions (the operations on fractions) with detailed explanations of the functions.
/************************************************\
* Filename:      fraction.h                      *
* Author:        Sue Bogar                       *
* Section:       201staff                        *
* email:         bogar@cs.umbc.edu               *
* Date Written:  4/14/98                         *
* Last Modified: 11/20/05                        *
*                                                *
* Description:  This header file contains the    *
* structure definition for a fraction type       *
* called FRACTION, and the function prototypes   *
* for the functions defined in fraction.c, the   *
* implementation of the ADT fraction.            *
\************************************************/

#ifndef _fraction_h
#define _fraction_h

/*****************
 * The fraction ADT is implemented as a structure
 * that has three members, all of type int.
 * The first member is the whole number part,
 * the second member is the numerator and the
 * last member is the denominator.
 *****************/
typedef struct fraction
{
    int whole;
    int numerator;
    int denominator;
} FRACTION;

typedef FRACTION* FRACTPTR;

/******************
* Name:   AddFractions 
* Usage:  AddFractions (&f1, &f2, & sum);
* Input:  three arguments of type FRACTPTR
* Output: nothing is returned, however sum is modified
*
* AddFractions takes three arguments of type FRACTPTR, 
* adds the FRACTIONs pointed to by the first two FRACTPTRs, 
* and modifies the FRACTION pointed to by the last FRACTPTR, 
* to hold their sum, which has been reduced to lowest terms.
******************/
void AddFractions (FRACTPTR fPtr1, FRACTPTR fPtr2, FRACTPTR sumPtr);

/******************
* Name:   SubFractions 
* Usage:  SubFractions (&f1, &f2, &difference);
* Inputs: three arguments of type FRACTPTR, where the
*         first argument points to the minuend and the 
*         second argument points to the subtrahend
* Output: nothing is returned, however difference is modified
* 
* SubFractions takes three arguments of type FRACTPTR, 
* subtracts the second FRACTION (pointed to by fPtr2) from the
* first FRACTION (pointed to by fPtr1).  The difference, f1 - f2 is 
* calculated, reduced to lowest terms, and stored in the FRACTION 
* pointed to by the last FRACTPTR differencePtr.
* 
******************/
void SubFractions (FRACTPTR fPtr1, FRACTPTR fPtr2, FRACTPTR differencePtr);

/******************
* Name:   MultFractions
* Usage:  MultFractions (&f1, &f2, &product);
* Input:  three arguments of type FRACTPTR
* Output: nothing is returned, however product is modified
*
* MultFractions takes three arguments of type FRACTPTR, 
* multiplies the FRACTIONs pointed to by the first two FRACTPTRs, 
* and modifies the FRACTION pointed to by the last FRACTPTR, to
* hold their product, which has been reduced to lowest terms.
******************/
void MultFractions (FRACTPTR fPtr1, FRACTPTR fPtr2, FRACTPTR productPtr);

/******************
* Name:   DivFractions 
* Usage:  DivFractions (&f1, &f2, "ient);
* Input:  three arguments of type FRACTPTR
* Output: nothing is returned, however quotient is modified
*
* DivFractions takes three arguments of type FRACTPTR, 
* divides the FRACTION pointed to by the first FRACTPTR by the
* FRACTION pointed to by the second FRACPTR, and modifies the 
* FRACTION pointed to by the last FRACTPTR, to hold the quotient, 
* which has been reduced to lowest terms.
******************/
void DivFractions  (FRACTPTR fPtr1, FRACTPTR fPtr2, FRACTPTR quotientPtr);

/******************
* Name:   RedToLowTerms
* Usage:  RedToLowTerms (&f);
* Input:  the address of a FRACTION variable which is
*         is to be reduced to lowest terms
* Output: none, but the FRACTION which is passed in
*         by reference will be modified to be in 
*         lowest terms.
*
* RedToLowTerms takes one argument of type FRACTPTR and
* reduces that fraction to lowest terms by finding the
* greatest common divisor of the numerator and denominator 
* and dividing through by that value.
******************/
void RedToLowTerms (FRACTPTR fPtr);   

/******************
* Name:   InitFraction 
* Usage:  InitFraction (&f);
* Input:  the address of a FRACTION variable which is
*         to be initialized.
* Output: none, but the FRACTION which is passed in
*         by reference  will be modified to have the
*         value 0.
*
* InitFraction takes one argument of type FRACTPTR 
* and initializes its members to:
* whole = 0, numerator = 0, denominator = 1
* denominator is set to 1 because division
* by 0 is undefined.
******************/
void InitFraction (FRACTPTR fPtr);

/******************
* Name:   AssignFraction
* Usage:  AssignFraction (&f, whole, num, denom);
* Input:  the address of a fraction variable, the whole
*         number, the numerator and the denominator to
*         be assigned.
* Output: none, but the variable whose address was passed
*         to the function will be modified to hold the
*         whole number, numerator and denominator passed in.
*
* AssignFraction takes one argument of type FRACTPTR*
* and three arguments of type int, which are whole, num 
* and denom.  The members of the FRACTION struct are then
* assigned these values.
******************/
void AssignFraction (FRACTPTR fPtr, int whole, int num, int denom); 

/******************
* Name:   PrintFraction 
* Usage:  PrintFraction (&f);
* Input:  One argument of type FRACTPTR
* Output: None, but the fraction whose address is passed in 
*         will be printed
*
* PrintFraction takes one argument of type FRACTPTR 
* and prints the fraction it points to in this format:  
* whole numerator/denominator
* Example: 1 1/4
******************/
void PrintFraction (FRACTPTR fPtr);

/******************
* Name:   GCD
* Usage:  gcd = GCD (num1, num2);
* Input:  two integer agrguments
* Output: the greatest common divisor of the
*         two integers passed in, type int
*
* GCD takes two integer arguments and calculates 
* the greatest common divisor of the those numbers 
* using Euclid's GCD algorithm.  It returns the
* result, which is an int.
******************/
int GCD (int a, int b);


#endif


CSEE | 201 | 201 F'05 | lectures | news | help

Sunday, 20-Nov-2005 18:07:37 EST