UMBC CS 201, Fall 98
UMBC CMSC 201 & 201H Fall '98 CSEE | 201 | 201 F'98 | 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 * * Date Written: 4/14/98 * * Description: This header file contains the * * structure definition for a fraction type * * called frac, and the function prototypes for * * the functions defined in frac.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 tag { int whole; int numerator; int denominator; } frac; /****************** * AddFractions takes two arguments of type * frac, adds them, and returns a frac, * which is their sum that has been reduced * to lowest terms. ******************/ frac AddFractions (frac f1, frac f2); /****************** * SubFractions takes two arguments of type * frac, f1 and f2. The difference, f1 - f2 * is calculated and the resulting fraction * that has been reduced to lowest terms is * returned. ******************/ frac SubFractions (frac f1, frac f2); /****************** * MultFractions takes two arguments of type * frac, f1 and f2, and multiplies them. The * resulting product, which is of type frac, * is reduced to lowest terms and returned. ******************/ frac MultFractions (frac f1, frac f2); /****************** * DivFractions takes two arguments of type * frac, f1 and f2, and divides f1 by f2. The * resulting fraction is reduced to lowest * terms and returned. ******************/ frac DivFractions (frac f1, frac f2); /****************** * RedToLowTerms takes one argument of type * frac * 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 (frac * fPtr); /****************** * InitFraction takes one argument of type * frac * and initializes its members to: * whole = 0, numerator = 0, denominator = 1 * denominator is set to 1 because division * by 0 is undefined. ******************/ void InitFraction (frac * fPtr); /****************** * PrintFraction takes one argument of type * frac and prints it in the following * format: whole numerator/denominator * Example: 1 1/4 ******************/ void PrintFraction (frac f); /****************** * GCD takes two integer arguments and * returns the greatest common divisor * of the those numbers. It returns * the result, which is an int. ******************/ int GCD (int a, int b); #endif


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

Sunday, 15-Nov-1998 17:09:09 EST