tags:

views:

148

answers:

0

You are to write a program that requests that the user inputs a fraction of the form n/d with d nonzero. Your program then prints out the fraction in reduced form. The program then asks if the user would like to process another fraction and continues to do so as long as the user replies with Y.

A fraction n/d is in reduced form provided: 1. If n is 0, then d is 1; 2. d > 0; 3. the greatest common divisor of n and d is 1.

The contents of the incomplete file reduceFraction.c are shown on the next page. You are not to modify the functions gcd and main. Instead, you are to supply the definitions for the functions:

void inputFraction(int *numerator, int *denominator);
void reduceFraction(int *numerator, int *denominator);
void printReducedFraction(int numerator, int denominator);


#include <stdio.h>
#include <string.h>

/* put any other #includes you think you may need here*/

void inputFraction(int *numerator, int *denominator); /* Prompts for and inputs a fraction with input verification */

void reduceFraction(int *numerator, int *denominator);
/* Modifies the fraction so that it is in reduced form */

void printReducedFraction(int numerator, int denominator);
/* Reduces the fraction numerator/denominator and prints the result.
If the denominator is 1, it just prints the numerator,
otherwise prints the fraction in the form numerator/denominator.
Calls reduceFraction.
*/

int gcd(int x, int y)
{
   if ( x % y == 0)
   return y;
   return gcd(y,x%y);
}


int main()
{
   int num, denom;
   char response;

   printf("\nThis function inputs fractions and prints the equivalent fraction in reduced form.\n\n");

   do {
      inputFraction(&num, &denom);

      printf("\n");
      printReducedFraction(num,denom);

      /* First clear the buffer, especially of any residual newline! */
      while (getchar() != '\n');

      printf("\nDo you wish to process another fraction (Y for yes, N for no): ");
      response = getchar();

      printf("\n");

   } while (toupper(getOneChar()) = = 'Y');

   return 0;
}