UMBC CS 201, Fall 98
UMBC CMSC 201 & 201H Fall '98 CSEE | 201 | 201 F'98 | lectures | news | help

Coding the struct implementation of fraction

/************************************************\ * Filename: fraction.c * * Author: Sue Bogar * * Date Written: 4/15/98 * * 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" /****************** * 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) { frac 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; } /****************** * 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) { frac 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; } /****************** * 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) { frac 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; } /****************** * 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) { frac 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; } /****************** * 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) { 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; } /****************** * 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) { fPtr -> whole = 0; fPtr -> numerator = 0; fPtr -> denominator = 1; } /****************** * AssignFraction takes one argument of type * frac * and three arguments of type int, * which are whole, num, and denom. The * members of the frac struct are assigned * these values. ******************/ void AssignFraction (frac * fPtr, int whole, int num, int denom) { fPtr -> whole = whole; fPtr -> numerator = num; fPtr -> denominator = denom; } /****************** * 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) { printf("%d %d/%d", f.whole, f.numerator, f.denominator); } /****************** * 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) { int remainder; remainder = a % b; while (remainder != 0) { a = b; b = remainder; remainder = a % b; } return b; } /* Driver to test fractions (struct implementation) */ /* Bogar, 4/16/98 */ #include <stdio.h> #include "fraction.h" main () { int whole, num, denom; frac 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"); }

Output

retriever[102] a.out 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 retriever[103]


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

Sunday, 15-Nov-1998 17:10:35 EST