// FILE: fractions.c // Code for testing fraction functions scan, display, add // Definition of a new type for fractions typedef struct { // defines a fraction type int numer; // numerator int denom; // denominator } fractType // Function prototypes // Function to display a fraction void displayFract (fractType f); // IN: fraction to be displayed // Function to add two fractions and return the sum void addFract (fractType &fResult, // OUT: result of adding two fractions fractType fLeftop, // IN: left operand for addition fractType fRightop); // IN: right operand for addition // Function to scan in a fraction f in form a / b // Stores results in numer and denom components of actual arg // Returns -1 if / not present // Returns 0 if b = 0 // Returns 1 if ok int scanFract (fractType *f); // OUT: fraction to be scanned in int main () { fractType f1, f2, f3; int k1, k2; // flags returned from scanFract // Scan in f1 and f2 k1 = scanFract (&f1); k2 = scanFract (&f2); if(k1 != 1 || k2 != 1) { printf(" Either fraction f1 or f2 entered incorrectly\n"); Printf(" Program exits immediately. \n"); return (-1); //exit if either fraction read fails } // Display f1 and f2; printf ("The value of f1 is :"); displayFract (f1); printf ("The value of f2 is :"); displayFract (f2); // Add together f1 and f2 and display result addFract (&f3, f1, f2); printf ("The value of f3 (f1 + f2) is :"); displayFract (f3); return (0); } // Function to display a fraction void displayFraction (fractType f) // IN: fraction to be displayed { printf("The fraction is %d / %d : \n", f.numer, f.denom); // OR you could write the fraction in math notation -- // printf("The fraction is (%d, %d): \n", f.numer, f.denom); } // Function to reduce a fraction to lowest terms // Returns original fraction reduced to lowest terms fractType simplify (fResult) // fraction to be simplified { // Not written - example of a function stub printf ("simplify function called. Not yet written."); return (fResult); } // Function to add two fractions and return the sum void addFraction (fractType &fResult, // OUT: result of adding two fractions fractType fLeftop, // IN: left operand for addition fractType fRightop) // IN: right operand for addition { (*fResult).numer = fLeftop.numer * fRightop.denom + fLeftop.denom * fRightop.numer; (*fResult).denom = fLeftop.denom * fRightop.denom; // simplifies result to lowest terms (*fResult) = simplify (fResult); return (0); } // Function to scan in a fraction f in form a / b // Stores results in numer and denom components of actual arg // Returns -1 if / not present // Returns 0 if b = 0 // Returns 1 if ok int scanFraction (fractType *f) // OUT: fraction to be scanned in { char symbol; // used to store the symbol between a and b int tempdenom; // temp cell for denom - simplifies code printf("Enter a fraction in the form a / b:"); scanf("%d", &(*f).numer); scanf("%c", symbol); if (symbol != '/') return (-1); scanf("%d", &tempdenom) if (tempdenom == 0) return (0); (*f).denom = tempdenom; return (1); }