/ ************************************************************************ * Author: M.J. Leslie * Purpose: Add two fractions together. * Date: 04-Oct-94 ************************************************************************/ /******************* Structures *****************************************/ /* Define a structure to hold the * fraction */ struct fract { int num; /* Numerator */ int den; /* Denominator */ }; /******************* Declare Functions **********************************/ void print_title(void); /* O/P the title information. */ /* Get the fraction from the * user. */ void read_fract(struct fract *); /* Add fractions by passing the * whole structure to the function. */ struct fract add_fraction1(struct fract, struct fract); /* Add fractions by passing * pointers to the structures. */ void add_fraction2(struct fract *, struct fract *, struct fract *); /* Print fractions */ void print_fraction(char *, struct fract fraction, char *); /****************** Main ************************************************/ main () { /* Declare the structures that * hold the fractions */ struct fract fract1, fract2, out_fract; print_title(); /* airy-fairy title */ /* Get the fraction values * from the user */ read_fract(&fract1); read_fract(&fract2); puts("\n Passing structure to function gives:"); print_fraction(" ",fract1, " + "); print_fraction("",fract2, " = "); /* add the fractions together */ out_fract = add_fraction1(fract1, fract2); print_fraction("",out_fract, "\n"); puts("\n Passing structure pointers to function gives:"); print_fraction(" ",fract1, " + "); print_fraction("",fract2, " = "); add_fraction2(&fract1, &fract2, &out_fract); print_fraction("",out_fract, "\n"); return(1); } /************************************************************************/ void print_title(void) { puts("\n This is a C education program that will add fractions."); puts(" It demonstrates the technic of copying structures to "); puts(" functions and copying pointers to structures to functions."); puts("\n As a by-product the program will ask for two fractions"); puts(" that are added together and the result put on the screen"); puts(" "); } /************************************************************************/ void read_fract(struct fract *fract) { char value[80]; puts(" "); printf(" Please enter the numerator ===> "); gets(value); fract->num = atoi(value); printf(" Please enter the denominator => "); gets(value); fract->den = atoi(value); } /************************************************************************/ struct fract add_fraction1(struct fract f1, struct fract f2) { struct fract answer; /* get the comman denominator */ answer.den = f1.den * f2.den; /* sort out the numerators */ f1.num = (answer.den / f1.den) * f1.num; f2.num = (answer.den / f2.den) * f2.num; /* Add up the numerators */ answer.num = f1.num + f2.num; return (answer); } /************************************************************************/ void add_fraction2(struct fract *f1, struct fract *f2, struct fract *answer) { /* Use temps so we do not corrupt * The original values of f1 and * f2 */ int temp1, temp2; /* get the comman denominator */ answer->den = f1->den * f2->den; /* sort out the numerators */ temp1 = (answer->den / f1->den) * f1->num; temp2 = (answer->den / f2->den) * f2->num; /* Add up the numerators */ answer->num = temp1 + temp2; } /************************************************************************/ void print_fraction(char * string1, struct fract fraction, char * string2) { /* Format the fraction with * information strings around it */ printf("%s%d/%d%s", string1, fraction.num, fraction.den, string2); }
file: /Techref/language/ccpp/CREF/PROBLEMS/fractions.c, 3KB, , updated: 1997/4/21 09:53, local time: 2024/8/14 17:19,
3.143.0.65:LOG IN
|
©2024 These pages are served without commercial sponsorship. (No popup ads, etc...).Bandwidth abuse increases hosting cost forcing sponsorship or shutdown. This server aggressively defends against automated copying for any reason including offline viewing, duplication, etc... Please respect this requirement and DO NOT RIP THIS SITE. Questions? <A HREF="http://piclist.com/techref/language/ccpp/CREF/PROBLEMS/fractions.c"> language ccpp CREF PROBLEMS fractions</A> |
Did you find what you needed? |
PICList 2024 contributors:
o List host: MIT, Site host massmind.org, Top posters @none found - Page Editors: James Newton, David Cary, and YOU! * Roman Black of Black Robotics donates from sales of Linistep stepper controller kits. * Ashley Roll of Digital Nemesis donates from sales of RCL-1 RS232 to TTL converters. * Monthly Subscribers: Gregg Rew. on-going support is MOST appreciated! * Contributors: Richard Seriani, Sr. |
Ashley Roll has put together a really nice little unit here. Leave off the MAX232 and keep these handy for the few times you need true RS232! |
.