Advertisement
Guest User

Untitled

a guest
Jul 21st, 2017
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.51 KB | None | 0 0
  1. /*
  2. 1- You are developing a Fraction structure for Teacher's Pet Software. The structure contains three data fields for whole number, numerator, and denominator. Using the same structure, write the functions described below.
  3.  
  4. An enterFractionValue() function that declares a local Fraction structure and prompts the user to enter values for the Fraction. Do not allow the user to enter a value of 0 for the denominator of any Fraction; continue to prompt the user for a denominator value until a valid one is entered. The function returns a data-filled Fraction structure to the calling function.
  5.  
  6. A reduceFraction() function that accepts a Fraction structure and reduces it to proper form, returning the reduced Fraction. For example, a Fraction entering the function as 0 2/6 would be reduced to 0 1/3, and a Fraction entered as 4 18/4 would be reduced to 8 ½.
  7.  
  8. A displayFraction() function that displays any Fraction structure passed to it. This function displays a fraction with a slash between the numerator and denominator.
  9. A main() function that declares a Fraction structure and continues to get Fraction values from the user until the user enters a Fraction with value 0 (both the whole number and the numerator are 0). For each Fraction entered, display the Fraction, reduce the Fraction, and display the Fraction again.
  10.  
  11.  
  12. Due date: February 28.*/
  13.  
  14. #include<stdio.h>
  15.  
  16.  
  17. struct Fraction {
  18.     int wholeNumber;
  19.     int numerator;
  20.     int denominator;
  21. };
  22.  
  23. struct Fraction enterFractionValue(){
  24.     struct Fraction fraction;
  25.  
  26.     do {
  27.         printf("Enter the numerator / the denominator of the fraction. Example: 1/2\n");
  28.         scanf("%d/%d", &fraction.numerator, &fraction.denominator);
  29.         if (fraction.denominator < 0)
  30.             printf("Enter a postive demoninator, if you want the fraction to be negetive make the numerator positive\n");
  31.     } while (fraction.denominator < 0);
  32.     fraction.wholeNumber= fraction.numerator/fraction.denominator;
  33.     return fraction;
  34. }
  35.  
  36. struct Fraction reduceFraction(struct Fraction fraction) {
  37.     for (int i = 1; i < 10; i++) {
  38.         if (fraction.numerator%i==0 && fraction.denominator%i ==0){
  39.             fraction.numerator/=i;
  40.             fraction.denominator/=i;
  41.         }
  42.     }
  43.     return fraction;
  44. }
  45.  
  46. void displayFraction(struct Fraction fraction) {
  47.     printf("%d/%d\n", fraction.numerator, fraction.denominator);
  48. }
  49.  
  50. int main (){
  51.     struct Fraction fraction;
  52.     fraction = enterFractionValue();
  53.     displayFraction(fraction);
  54.     printf("Now trying to reduce the fraction\n");
  55.     fraction = reduceFraction(fraction);
  56.     displayFraction(fraction);
  57.  
  58.     return 0;
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement