UMBC CS 201, Spring 02
UMBC CMSC 201 Spring '02 CSEE | 201 | 201 S'02 | 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 * * SSN: 123-45-65789 * * Section: 201staff * * email: bogar@cs.umbc.edu * * Date Written: 4/14/98 * * * * 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; /****************** * Name: AddFractions * Usage: sum = AddFractions (f1, f2); * Input: two arguments of type FRACTION * Output: the sum of the two input fractions, * also of type FRACTION * * AddFractions takes two arguments of type FRACTION, * adds them, and returns a FRACTION, which is their * sum that has been reduced to lowest terms. ******************/ FRACTION AddFractions (FRACTION f1, FRACTION f2); /****************** * Name: SubFractions * Usage: difference = SubFractions (f1, f2); * Inputs: two arguments of type FRACTION, where the * first argument is the minuend and the second * argument is the subtrahend. * Output: the result of the subtraction, difference * * SubFractions takes two arguments of type FRACTION, * f1 and f2. The difference, f1 - f2 is calculated * and the resulting fraction which has been reduced * to lowest terms is returned. ******************/ FRACTION SubFractions (FRACTION f1, FRACTION f2); /****************** * Name: MultFractions * Usage: product = MultFractions (f1, f2); * Input: two arguments of type FRACTION * Output: the product of the the two fractions * that were passed in. * * MultFractions takes two arguments of type FRACTION, * f1 and f2, and multiplies them. The resulting * product, which is of type FRACTION, is reduced to * lowest terms and returned. ******************/ FRACTION MultFractions (FRACTION f1, FRACTION f2); /****************** * Name: DivFractions * Usage: quotient = DivFractions (f1, f2); * Input: two arguments of type FRACTION where the * first argument is the dividend and the second * argument is the divisor. * Output: the result of dividing f1 by f2, the quotient * * DivFractions takes two arguments of type FRACTION, * f1 and f2, and divides f1 by f2. The resulting * FRACTION is reduced to lowest terms and returned. ******************/ FRACTION DivFractions (FRACTION f1, FRACTION f2); /****************** * 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 FRACTION * * 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 (FRACTION * 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 FRACTION * * and initializes its members to: * whole = 0, numerator = 0, denominator = 1 * denominator is set to 1 because division * by 0 is undefined. ******************/ void InitFraction (FRACTION * 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 FRACTION * * 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 (FRACTION * fPtr, int whole, int num, int denom); /****************** * Name: PrintFraction * Usage: PrintFraction (f); * Input: One argument of type FRACTION * Output: None, but the fraction will be printed * * PrintFraction takes one argument of type FRACTION * and prints it in the following format: * whole numerator/denominator * Example: 1 1/4 ******************/ void PrintFraction (FRACTION f); /****************** * 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 S'02 | lectures | news | help

Thursday, 17-Jan-2002 13:52:31 EST