函数题:Reduction of a fraction
A fraction is presented as "numerator/denominator", like 2/3 as $\frac{2}{3}$。
A struct Fraction is defined for a fraction, as:
typedef struct {
int integer;
int numerator;
int denominator;
} Fraction;
where integer is the real part in front of the fraction, like the “two” in “two and three over five” ($2 \cfrac{3}{5}$).
Now, your job is to write a fraction_reduce() function that takes a Fraction and reduce it into the simplest format. For example, given 4/16, the result sould be 1/4.
### Function prototype:
c
Fraction fraction_reduce(Fraction f);
### Test code:
c
#include <stdio.h>
typedef struct {
int integer;
int numerator;
int denominator;
} Fraction;
Fraction fraction_reduce(Fraction f);
int main()
{
Fraction f = {0};
scanf("%d/%d", &f.numerator, &f.denominator);
f = fraction_reduce(f);
if ( f.integer ) {
printf("%d", f.integer);
if ( f.numerator ) {
printf(" ");
}
}
if ( f.numerator ) {
printf("%d", f.numerator);
if ( f.denominator != f.numerator ) {
printf("/%d", f.denominator);
}
}
printf("\n");
}
/* Put your code here. */
### Sample Input:
in
4/16
### Sample Output:
out
1/4
### Sample Input:
in
20/14
### Sample Output:
out
1 3/7
答案:若无答案欢迎评论
A struct Fraction is defined for a fraction, as:
typedef struct {
int integer;
int numerator;
int denominator;
} Fraction;
where integer is the real part in front of the fraction, like the “two” in “two and three over five” ($2 \cfrac{3}{5}$).
Now, your job is to write a fraction_reduce() function that takes a Fraction and reduce it into the simplest format. For example, given 4/16, the result sould be 1/4.
### Function prototype:
c
Fraction fraction_reduce(Fraction f);
### Test code:
c
#include <stdio.h>
typedef struct {
int integer;
int numerator;
int denominator;
} Fraction;
Fraction fraction_reduce(Fraction f);
int main()
{
Fraction f = {0};
scanf("%d/%d", &f.numerator, &f.denominator);
f = fraction_reduce(f);
if ( f.integer ) {
printf("%d", f.integer);
if ( f.numerator ) {
printf(" ");
}
}
if ( f.numerator ) {
printf("%d", f.numerator);
if ( f.denominator != f.numerator ) {
printf("/%d", f.denominator);
}
}
printf("\n");
}
/* Put your code here. */
### Sample Input:
in
4/16
### Sample Output:
out
1/4
### Sample Input:
in
20/14
### Sample Output:
out
1 3/7
答案:若无答案欢迎评论