Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

ADT_Data Strc. Create a rational number ADT with data (rational number in the fo

ID: 3549620 • Letter: A

Question

ADT_Data Strc.

Create a rational number ADT with data (rational number in the form of a/b, a and b are integer and b is not zero) and operations(creation of a rational number from two integers, show_rational_value, addition, subtraction, multiplication, division). Then implement it as a data structure in C program that create two rational numbers and do addition, subtraction, multiplication, division.

Addition:

n1/d1 + n2/d2 = (n1*d2+n2*d1) / (d1*d2)

Multiplication:

n1/d1 * n2/d2 = (n1*n2) / (d1*d2)

Negation:

-(n/d) = (-n)/d

Inverse:

1 / (n/d) = d/n

The following operations can be implemented in terms of the above:

Subtraction:

r1 - r2 = r1 + -r2

Division:

r1 / r2 = r1 * (1/r2)

Sample output:

Explanation / Answer

/*

* Adds, subtracts, multiplies and divides common fractions, displaying

* results in reduced form.

*/


#include <stdio.h>

#include <stdlib.h> /* provides function abs */



/* Function prototypes */

void scan_fraction(int *nump, int *denomp);


char get_operator(void);

void add_fractions(int n1, int d1, int n2, int d2,

int *n_ansp, int *d_ansp);


void multiply_fractions(int n1, int d1, int n2, int d2,

int *n_ansp, int *d_ansp);


int find_gcd (int n1, int n2);


void reduce_fraction(int *nump, int *denomp);


void print_fraction(int num, int denom);


int

main(void)

{

int n1, d1; /* numerator, denominator of first fraction */

int n2, d2; /* numerator, denominator of second fraction */

char op; /* arithmetic operator + - * or / */

char again; /* y or n depending on user's desire to continue */

int n_ans, d_ans; /* numerator, denominator of answer */


/* While the user wants to continue, gets and solves arithmetic

problems with common fractions */

do {

/* Gets a fraction problem */

scan_fraction(&n1, &d1);

op = get_operator();

scan_fraction(&n2, &d2);


/* Computes the result */

switch (op) {

case '+':

add_fractions(n1, d1, n2, d2, &n_ans, &d_ans);

break;


case '-':

add_fractions(n1, d1, -n2, d2, &n_ans, &d_ans);

break;


case '*':

multiply_fractions(n1, d1, n2, d2, &n_ans, &d_ans);

break;


case '/':

multiply_fractions(n1, d1, d2, n2, &n_ans, &d_ans);

}

reduce_fraction(&n_ans, &d_ans);


/* Displays problem and result */

printf(" ");

print_fraction(n1, d1);

printf(" %c ", op);

print_fraction(n2, d2);

printf(" = ");

print_fraction(n_ans, d_ans);


/* Asks user about doing another problem */

printf(" Do another problem? (y/n)> ");

scanf(" %c", &again);

} while (again == 'y' || again == 'Y');

return (0);

}


/* Insert function scan_fraction from Fig. 6.9 here. */


/*

* Gets and returns a valid arithmetic operator. Skips over newline

* characters and permits re-entry of operator in case of error.

*/

char

get_operator(void)

{

char op;


printf("Enter an arithmetic operator (+,-,*, or /) > ");

for (scanf("%c", &op);

op != '+' && op != '-' &&

op != '*' && op != '/';

scanf("%c", &op)) {

if (op != ' ')

printf("%c invalid, reenter operator (+,-, *,/) > ", op);

}

return (op);

}


/*

* Adds fractions represented by pairs of integers.

* Pre: n1, d1, n2, d2 are defined;

* n_ansp and d_ansp are addresses of type int variables.

* Post: sum of n1/d1 and n2/d2 is stored in variables pointed

* to by n_ansp and d_ansp. Result is not reduced.

*/

void

add_fractions(int n1, int d1, /* input - first fraction */

int n2, int d2, /* input - second fraction */

int *n_ansp, int *d_ansp) /* output - sum of 2 fractions*/

{

int denom, /* common denominator used for sum (may not be least) */

numer, /* numerator of sum */

sign_factor; /* -1 for a negative, 1 otherwise */


/* Finds a common denominator */

denom = d1 * d2;


/* Computes numerator */

numer = n1 * d2 + n2 * d1;


/* Adjusts sign (at most, numerator should be negative) */

if (numer * denom >= 0)

sign_factor = 1;

else

sign_factor = -1;


numer = sign_factor * abs(numer);

denom = abs(denom);


/* Returns result */

*n_ansp = numer;

*d_ansp = denom;

}


/*

***** STUB *****

* Multiplies fractions represented by pairs of integers.

* Pre: n1, d1, n2, d2 are defined;

* n_ansp and d_ansp are addresses of type int variables.

* Post: product of n1/d1 and n2/d2 is stored in variables pointed

* to by n_ansp and d_ansp. Result is not reduced.

*/

void

multiply_fractions(int n1, int d1, /* input - first fraction */

int n2, int d2, /* input - second fraction */

int *n_ansp, /* output - */

int *d_ansp) /* product of 2 fractions */

{

/* Displays trace message */

printf(" Entering multiply_fractions with ");

printf("n1 = %d, d1 = %d, n2 = %d, d2 = %d ", n1, d1, n2, d2);


/* Defines output arguments */

*n_ansp = 1;

*d_ansp = 1;

}


/*

***** STUB *****

* Finds greatest common divisor of two integers

*/

int

find_gcd (int n1, int n2) /* input - two integers */

{

int gcd;


/* Displays trace message */

printf(" Entering find_gcd with n1 = %d, n2 = %d ", n1, n2);


/* Asks user for gcd */

printf("gcd of %d and %d?> ", n1, n2);

scanf("%d", &gcd);


/* Displays exit trace message */

printf("find_gcd returning %d ", gcd);

return (gcd);

}



/*

* Reduces a fraction by dividing its numerator and denominator by their

* greatest common divisor.

*/

void

reduce_fraction(int *nump, /* input/output - */

int *denomp) /* numerator and denominator of fraction */

{

int gcd; /* greatest common divisor of numerator & denominator */


gcd = find_gcd(*nump, *denomp);

*nump = *nump / gcd;

*denomp = *denomp / gcd;

}



/*

* Displays pair of integers as a fraction.

*/

void

print_fraction(int num, int denom) /* input - numerator & denominator */

{

printf("%d/%d", num, denom);

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote