UMBC CS 201, Spring 02
UMBC CMSC 201 Spring '02 CSEE | 201 | 201 S'02 | lectures | news | help

Coding the struct implementation of fraction

Besides the standard operation on fractions, we must also consider that we can no longer print this type by giving a simple print specifier, like %d or %c. It's just more complicated than that. So we need to add a function that can print a fraction in a manner that is standard.

It might be necessary for us to initialize a fraction to 0. So we'll need an InitFraction() function.

We would also like to provide a function that allows us to assign some value to a fraction. So we should write this as well.

These three functions are probably the first that should be written.

/************************************************\ * Filename: fraction.c * * Author: Sue Bogar * * Date Written: 4/15/98 * * Section: 101 * * SSN: 123-45-6789 * * EMail: bogar@cs.umbc.edu * * * * Description: This file contains the functions * * necessary to work with the structure * * implementation of the fraction ADT defined in * * in fraction.h. This set of functions provide * * the operations needed to work with this type, * * and include initialization, assignment, and * * printing of fractions. The standard aritmetic * * operations on fractions and some supporting * * supporting functions, for finding the greatest * * common divisor of two integers and a function * * for reducing fractions to lowest terms. * \************************************************/ #include <stdio.h> #include "fraction.h" /************************************************ ** Function: AddFractions ** Input: f1 and f2 are FRACTIONs to be added ** Output: returns a FRACTION (result) which is the ** sum of f1 and f2, ** reduced to lowest terms *******************************************/ FRACTION AddFractions (FRACTION f1, FRACTION f2) { FRACTION result; result.whole = f1.whole + f2.whole; result.denominator = f1.denominator * f2.denominator; result.numerator = f1.denominator * f2.numerator + f2.denominator * f1.numerator; RedToLowTerms(&result); return result; } /*********************************************** ** Function: SubFractions ** Input: f1 and f2 are FRACTIONs to be subtracted ** Output: returns a FRACTION (result) which is the ** the difference, f1 - f2 reduced to ** lowest terms **************************************/ FRACTION SubFractions (FRACTION f1, FRACTION f2) { FRACTION result; result.whole = f1.whole - f2.whole; result.denominator = f1.denominator * f2.denominator; result.numerator = f2.denominator * f1.numerator - f1.denominator * f2.numerator ; RedToLowTerms(&result); return result; } /************************************** ** Function: MultFractions ** Input: f1 and f2 are FRACTIONs to be multiplied ** Output: a FRACTION (result) which is the product, ** f1 * f2, reduced to lowest terms ** **************************************/ FRACTION MultFractions (FRACTION f1, FRACTION f2) { FRACTION result; f1.numerator = f1.denominator * f1.whole + f1.numerator; f1.whole = 0; f2.numerator = f2.denominator * f2.whole + f2.numerator; f2.whole = 0; result.whole = 0; result.numerator = f1.numerator * f2.numerator; result.denominator = f1.denominator * f2.denominator; RedToLowTerms(&result); return result; } /************************************** ** Function: DivFractions ** Input: 2 FRACTIONS (f1 and f2) to be divided ** Output: a FRACTION (result) which is the quotient ** of f1 divided by f2, in lowest terms ** Description: ** DivFrace inverts f2 then calls MultFractions **************************************/ FRACTION DivFractions (FRACTION f1, FRACTION f2) { FRACTION result; int numer, denom; numer = f2.whole * f2.denominator + f2.numerator; denom = f2.denominator; f2.whole = 0; f2.numerator = denom; f2.denominator = numer; result = MultFractions(f1, f2); return result; } /************************************** ** Function: RedToLowTerms ** Input: a pointer to a FRACTION ** Output: the FRACTION being pointed to is reduced ** to lowest terms ** there is no return value ** Description: ** 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) { int gcd, numer, denom; denom = fPtr -> denominator; numer = fPtr -> whole * denom + fPtr -> numerator; gcd = GCD (numer, denom); numer /= gcd; denom /= gcd; fPtr -> whole = numer/denom; fPtr -> numerator = numer % denom; fPtr -> denominator = denom; } /************************************** ** Function: InitFraction ** Input: a pointer to a FRACTION ** Output: the FRACTION pointed to is initialized ** whole = num = 0 ** denominator = 1 ** ** Note: ** denominator is set to 1 because division ** by 0 is undefined. **************************************/ void InitFraction (FRACTION *fPtr) { fPtr -> whole = 0; fPtr -> numerator = 0; fPtr -> denominator = 1; } /************************************** * Function: AssignFraction * Input: a pointer to a FRACTION * an integer whole part * an integer numerator * an integer denominator * Output: the corresponding members of the FRACTION * are initialized to the whole,numerator & * denominator provided by the caller **************************************/ void AssignFraction (FRACTION *fPtr, int whole, int num, int denom) { fPtr -> whole = whole; fPtr -> numerator = num; fPtr -> denominator = denom; } /************************************** * Function: PrintFraction * Input: a FRACTION, f * Ouput: f is printed according to the format * specified within this code * Example: 1 1/4 * there is no return value * **************************************/ void PrintFraction (FRACTION f) { printf("%d %d/%d", f.whole, f.numerator, f.denominator); } /************************************** * Function: GCD (Greatest Common Divisor) * Input: two integers * Output: the greatest common divisor * of the integers * Description: * GCD takes two integer arguments and * calculates the greatest common divisor of * those numbers using Euclid's GCD algorithm. **************************************/ int GCD (int a, int b) { int remainder; remainder = a % b; while (remainder != 0) { a = b; b = remainder; remainder = a % b; } return b; } /********************************************* ** File: testFrac.c ** Author: S. Bogar ** Date: 4/16/98 ** Section: 101 ** SSN: 123-45-6789 ** EMail: bogar@cs.umbc.edu ** ** Driver to test fractions (struct implementation) *********************************************/ #include <stdio.h> #include "fraction.h" int main () { int whole, num, denom; FRACTION f1, f2, answer; /* Get two fractions from the user */ /* Fraction 1 */ printf("Enter first fraction:\n"); printf("Enter whole number: "); scanf("%d", &whole); printf("Enter numerator: "); scanf("%d", &num); printf("Enter denominator: "); scanf("%d", &denom); AssignFraction(&f1, whole, num, denom); /* Fraction 2 */ printf("Enter second fraction:\n"); printf("Enter whole number: "); scanf("%d", &whole); printf("Enter numerator: "); scanf("%d", &num); printf("Enter denominator: "); scanf("%d", &denom); AssignFraction(&f2, whole, num, denom); /* Print the fractions */ printf("f1 = "); PrintFraction(f1); printf ("\n"); printf("f2 = "); PrintFraction(f2); printf ("\n\n"); /* Test addition */ answer = AddFractions(f1, f2); printf("answer to addition = "); PrintFraction(answer); printf ("\n\n"); /* Test subtraction */ answer = SubFractions(f1, f2); printf("answer to subtraction = "); PrintFraction(answer); printf ("\n"); /* Test multiplication */ answer = MultFractions(f1, f2); printf("answer to multiplication = "); PrintFraction(answer); printf ("\n"); /* Test division */ answer = DivFractions(f1, f2); printf("answer to division = "); PrintFraction(answer); printf ("\n"); return 0; }

Output

Enter first fraction: Enter whole number: 3 Enter numerator: 1 Enter denominator: 2 Enter second fraction: Enter whole number: 1 Enter numerator: 2 Enter denominator: 3 f1 = 3 1/2 f2 = 1 2/3 answer to addition = 5 1/6 answer to subtraction = 1 5/6 answer to multiplication = 5 5/6 answer to division = 2 1/10


CSEE | 201 | 201 S'02 | lectures | news | help

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