Interesting bits: using a struct typedef to pre-define the 'rational' datatype, also the use of recursion in the 'reduce' function to further reduce the fractional part of a rational number which has already been reduced from being top-heavy.
Caveats - this version doesn't play nice when values fall below zero - However, the latest update (scroll down the page to the second reply) does.
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
typedef struct {
int integer;
int numerator;
int denominator;
} rational;
rational add ( rational *r, rational *r2 );
rational subtract ( rational *r, rational *r2 );
rational multiply ( rational *r, rational *r2);
rational divide ( rational *r, rational *r2);
rational reduce ( rational *r );
void display ( rational *r );
int main( int argc, char *argv[]){
rational rational1, rational2, result;
printf("nenter first numerator: ");
scanf("%d", &rational1.numerator);
printf("nenter first denominator: ");
scanf("%d", &rational1.denominator);
printf("nenter second numerator: ");
scanf("%d", &rational2.numerator);
printf("nenter second denominator: ");
scanf("%d", &rational2.denominator);
result = add( &rational1, &rational2 );
result = reduce( &result );
printf("nsum : ");
display( &result);
result = multiply( &rational1, &rational2 );
result = reduce( &result );
printf("nproduct : ");
display( &result);
result = subtract( &rational1, &rational2 );
result = reduce( &result );
printf("nsubtracted : ");
display( &result);
result = divide( &rational1, &rational2 );
result = reduce( &result );
printf("ndivided : ");
display( &result);
printf("nn");
}
void display ( rational *pr ){
if( pr->integer > 0 ){
printf("%d ",pr->integer);
}
if( pr->numerator > 0 && pr->denominator > 0){
printf("%d/%d",pr->numerator, pr->denominator);
}
}
rational add ( rational *pr, rational *pr2 ){
rational result;
int hcd;
int nom1, nom2, tot;
hcd = pr->denominator * pr2->denominator;
nom1 = pr2->denominator * pr->numerator;
nom2 = pr->denominator * pr2->numerator;
tot = nom1 + nom2;
result.numerator = tot;
result.denominator = hcd;
return result;
}
rational subtract ( rational *pr, rational *pr2 ){
rational result;
int hcd;
int nom1, nom2, tot;
hcd = pr->denominator * pr2->denominator;
nom1 = pr2->denominator * pr->numerator;
nom2 = pr->denominator * pr2->numerator;
tot = nom1 - nom2;
result.numerator = tot;
result.denominator = hcd;
return result;
}
rational multiply ( rational *pr, rational *pr2 ){
rational result;
result.numerator = pr->numerator * pr2->numerator;
result.denominator = pr->denominator * pr2->denominator;
return result;
}
rational divide ( rational *pr, rational *pr2 ){
rational result;
result.numerator = pr->numerator * pr2->denominator;
result.denominator = pr->denominator * pr2->numerator;
return result;
}
rational reduce ( rational *pr ){
int i;
rational result, fraction, reduced;
/* fraction is fractional part of a proper function. This will recurse to further reduce the fraction */
if( pr->numerator > pr->denominator ){
if( (pr->numerator % pr->denominator) ==0 ){
/* this is a whole number */
result.integer = pr->numerator / pr->denominator;
result.numerator = 0;
result.denominator = 0;
return result;
}
result.integer = (int)(pr->numerator / pr->denominator);
result.numerator = pr->numerator - (pr->denominator * result.integer);
result.denominator = pr->denominator;
fraction.numerator = result.numerator;
fraction.denominator = result.denominator;
reduced = reduce (&fraction);
result.numerator = reduced.numerator;
result.denominator = reduced.denominator;
return result;
}else{
if( pr->numerator == pr->denominator ){
result.integer = 1;
result.numerator = 0;
result.denominator = 0;
return result;
}
i = pr->numerator;
while(i>0){
if(((pr->numerator % i) == 0 ) && ((pr->denominator %i ) == 0)){
result.integer = 0;
result.numerator = ( pr->numerator / i );
result.denominator = ( pr->denominator / i );
return result;
}
i--;
}
return result;
}
}
christo