Sederhanakan pecahan c
#include <stdio.h>
/** Struct describing a Fraction */
typedef struct Fraction
{
int num; // numerator
int den; // denominator
} Fraction;
/** Returns the gcd of the fraction */
int frc_gcd (Fraction f)
{
int reminder;
while ( f.num != 0 )
{
reminder = f.num;
f.num = f.den % f.num;
f.den = reminder;
}
return f.den;
}
/** Simplify the given fraction
* Both numerator and denominator gets divided by the gcd of them
**/
void frc_simplify(Fraction* f) {
int gcdValue = frc_gcd(*f);
f->num = f->num / gcdValue;
f->den = f->den / gcdValue;
}
/** Prints the fraction formatted
* If denominator is 1 it's omitted
**/
void frc_print(Fraction f){
f.den != 1 ?
printf("%d/%d", f.num, f.den) :
printf("%d", f.num);
}
int main (int argc, const char * argv[]) {
Fraction f = {6,3};
frc_simplify(&f);
printf("In lowest terms:");
frc_print(f);
}
Davido Tonante