/* A Program testing for rational number*/
#include <stdio.h>
#include <stdlib.h>
typedef struct rational_t {
int numer;
int denom;
} rational_t;
int gcd(int a, int b)
{
/* To reduce the rational number to its lowest form. */
if(b==0)
return a;
else
return gcd(b,a%b);
}
/* r_init: Creates rational number representing numer/denom and returns it*/
rational_t r_init(int numer, int denom)
{
rational_t z;
z.numer = numer;
z.denom = denom;
return z;
}
/* r_add: Returns a rational_t representing x+y */
rational_t r_add(rational_t x, rational_t y)
{
rational_t z;
int numer_x = x.numer;
int denom_x = x.denom;
int numer_y = y.numer;
int denom_y = y.denom;
int numer_z = (numer_x * denom_y) + (numer_y * denom_x);
int denom_z = denom_y * denom_x;
int sd = gcd(numer_z,denom_z);
z.numer = numer_z/sd;
z.denom = denom_z/sd;
return z;
}
/* r_sub: Returns a rational_t representing x-y */
rational_t r_sub(rational_t x, rational_t y)
{
rational_t z;
int numer_x = x.numer;
int denom_x = x.denom;
int numer_y = y.numer;
int denom_y = y.denom;
int numer_z = (numer_x * denom_y) - (numer_y * denom_x);
int denom_z = denom_y * denom_x;
int sd = gcd(numer_z,denom_z);
z.numer = numer_z/sd;
z.denom = denom_z/sd;
return z;
}
/* r_mul: Returns a rational_t representing x*y */
rational_t r_mul(rational_t x, rational_t y)
{
rational_t z;
int numer_x = x.numer;
int denom_x = x.denom;
int numer_y = y.numer;
int denom_y = y.denom;
int numer_z = (numer_x * numer_y);
int denom_z = denom_y * denom_x;
int sd = gcd(numer_z,denom_z);
z.numer = numer_z/sd;
z.denom = denom_z/sd;
return z;
}
/* r_div: Returns a rational_t representing x/y */
rational_t r_div(rational_t x, rational_t y)
{
rational_t z;
int numer_x = x.numer;
int denom_x = x.denom;
int numer_y = y.numer;
int denom_y = y.denom;
int numer_z = (numer_x * denom_y);
int denom_z = numer_y * denom_x;
int sd = gcd(numer_z,denom_z);
z.numer = numer_z/sd;
z.denom = denom_z/sd;
return z;
}
/* r_toreal: Returns a double representing x */
double r_toreal(rational_t x)
{
return x.numer/x.denom;
}
/* r_print: prints rational as x/y (using printf */
void r_print(rational_t x)
{
printf("%d/%d",x.numer,x.denom);
}
int main(void)
{
printf("Rational Arithmetic test program\n");
do {
int u,v, oper;
rational_t x, y, z;
printf("x - numerator: ");
scanf("%d", &u);
printf("x - denominator: ");
HERE: scanf("%d", &v);
if(v==0)
{
printf("Don't use 0 \n");
goto HERE;
}
x = r_init(u,v);
printf("y - numerator: ");
scanf("%d", &u);
printf("y - denominator: ");
H:scanf("%d", &v);
if(v==0)
{
printf("Don't use 0 \n");
goto H;
}
y = r_init(u,v);
printf("x is: ");
r_print(x);
printf("\n");
printf("y is: ");
r_print(y);
printf("\n");
printf("Operation (1=Add,2=Sub,3=Mul,4=Div,0=End): ");
scanf("%d", &oper);
switch (oper) {
case 1:
z = r_add(x,y);
break;
case 2:
z = r_sub(x,y);
break;
case 3:
z = r_mul(x,y);
break;
case 4:
z = r_div(x,y);
break;
case 0:
return 0;
default:
printf("Unknown operation!\n");
break;
}
printf("z is: ");
r_print(z);
printf("\n");
} while(1);
return 0;
}
given the code above,hw can i make x and y give me the lowest form of the rational numbers