大致如下:
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
typedef struct
{
long long int molecular;
long long int denominator;
}FRACTION;
int change_to_proper_fraction(FRACTION *fraction);
void fraction_add(FRACTION *frac_sum, FRACTION *frac_num);
main()
{
FRACTION frac[4];
int ret = 0;
long long int quo = 0;
frac[0].molecular = 1;
frac[0].denominator = 2;
frac[1].molecular = 1;
frac[1].denominator = 3;
frac[2].molecular = 1;
frac[2].denominator = 6;
frac[3].molecular = 0;
frac[3].denominator = 0;
if (change_to_proper_fraction(&frac[0])<0 || change_to_proper_fraction(&frac[1])<0
|| change_to_proper_fraction(&frac[2])<0)
{
printf("please check the fraction[3]!!!\n");
}
else
{
fraction_add(&frac[3], &frac[0]);
fraction_add(&frac[3], &frac[1]);
fraction_add(&frac[3], &frac[2]);
if (frac[3].molecular%frac[3].denominator == 0)
{
printf("%lld/%lld+%lld/%lld+%lld/%lld=%lld\n", frac[0].molecular, frac[0].denominator,
frac[1].molecular, frac[1].denominator, frac[2].molecular, frac[2].denominator, frac[3].molecular/frac[3].denominator);
}
else
{
printf("%lld/%lld+%lld/%lld+%lld/%lld=%lld/%lld\n", frac[0].molecular, frac[0].denominator,
frac[1].molecular, frac[1].denominator, frac[2].molecular, frac[2].denominator, frac[3].molecular, frac[3].denominator);
}
}
exit(1);
}
int change_to_proper_fraction(FRACTION *fraction)
{
if (fraction->denominator == 0)
return -1;
if (fraction->molecular == 0)
{
fraction->denominator = 0;
return 0;
}
if ((fraction->molecular%fraction->denominator) == 0)
{
fraction->molecular = fraction->molecular/fraction->denominator;
fraction->denominator = 1;
return 0;
}
long long int diff = 0;
long long int larger = 0;
long long int smaller = 0;
long long int mole_posi = 0;
long long int deno_posi = 0;
mole_posi = fraction->molecular > 0 ? fraction->molecular : (0 - fraction->molecular);
deno_posi = fraction->denominator > 0 ? fraction->denominator : (0 - fraction->denominator);
smaller = mole_posi<deno_posi ? mole_posi : deno_posi;
diff = deno_posi + mole_posi - 2 * smaller;
larger = diff>smaller ? diff : smaller;
smaller = diff + smaller - larger;
while (larger%smaller != 0)
{
diff = larger - smaller;
larger = diff>smaller ? diff : smaller;
smaller = diff + smaller - larger;
}
fraction->molecular = fraction->molecular/smaller;
fraction->denominator = fraction->denominator/smaller;
return 0;
}
void fraction_add(FRACTION *frac_sum, FRACTION *frac_num)
{
if (frac_sum->molecular == 0)
{
frac_sum->molecular = frac_num->molecular;
frac_sum->denominator = frac_num->denominator;
return;
}
else if (frac_num->molecular == 0)
{
return;
}
else
{
frac_sum->molecular = frac_sum->molecular*frac_num->denominator + frac_num->molecular*frac_sum->denominator;
frac_sum->denominator = frac_sum->denominator * frac_num->denominator;
change_to_proper_fraction(frac_sum);
return;
}
}
[ 本帖最后由 yyqd2013 于 2013-5-10 00:30 编辑 ]
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
typedef struct
{
long long int molecular;
long long int denominator;
}FRACTION;
int change_to_proper_fraction(FRACTION *fraction);
void fraction_add(FRACTION *frac_sum, FRACTION *frac_num);
main()
{
FRACTION frac[4];
int ret = 0;
long long int quo = 0;
frac[0].molecular = 1;
frac[0].denominator = 2;
frac[1].molecular = 1;
frac[1].denominator = 3;
frac[2].molecular = 1;
frac[2].denominator = 6;
frac[3].molecular = 0;
frac[3].denominator = 0;
if (change_to_proper_fraction(&frac[0])<0 || change_to_proper_fraction(&frac[1])<0
|| change_to_proper_fraction(&frac[2])<0)
{
printf("please check the fraction[3]!!!\n");
}
else
{
fraction_add(&frac[3], &frac[0]);
fraction_add(&frac[3], &frac[1]);
fraction_add(&frac[3], &frac[2]);
if (frac[3].molecular%frac[3].denominator == 0)
{
printf("%lld/%lld+%lld/%lld+%lld/%lld=%lld\n", frac[0].molecular, frac[0].denominator,
frac[1].molecular, frac[1].denominator, frac[2].molecular, frac[2].denominator, frac[3].molecular/frac[3].denominator);
}
else
{
printf("%lld/%lld+%lld/%lld+%lld/%lld=%lld/%lld\n", frac[0].molecular, frac[0].denominator,
frac[1].molecular, frac[1].denominator, frac[2].molecular, frac[2].denominator, frac[3].molecular, frac[3].denominator);
}
}
exit(1);
}
int change_to_proper_fraction(FRACTION *fraction)
{
if (fraction->denominator == 0)
return -1;
if (fraction->molecular == 0)
{
fraction->denominator = 0;
return 0;
}
if ((fraction->molecular%fraction->denominator) == 0)
{
fraction->molecular = fraction->molecular/fraction->denominator;
fraction->denominator = 1;
return 0;
}
long long int diff = 0;
long long int larger = 0;
long long int smaller = 0;
long long int mole_posi = 0;
long long int deno_posi = 0;
mole_posi = fraction->molecular > 0 ? fraction->molecular : (0 - fraction->molecular);
deno_posi = fraction->denominator > 0 ? fraction->denominator : (0 - fraction->denominator);
smaller = mole_posi<deno_posi ? mole_posi : deno_posi;
diff = deno_posi + mole_posi - 2 * smaller;
larger = diff>smaller ? diff : smaller;
smaller = diff + smaller - larger;
while (larger%smaller != 0)
{
diff = larger - smaller;
larger = diff>smaller ? diff : smaller;
smaller = diff + smaller - larger;
}
fraction->molecular = fraction->molecular/smaller;
fraction->denominator = fraction->denominator/smaller;
return 0;
}
void fraction_add(FRACTION *frac_sum, FRACTION *frac_num)
{
if (frac_sum->molecular == 0)
{
frac_sum->molecular = frac_num->molecular;
frac_sum->denominator = frac_num->denominator;
return;
}
else if (frac_num->molecular == 0)
{
return;
}
else
{
frac_sum->molecular = frac_sum->molecular*frac_num->denominator + frac_num->molecular*frac_sum->denominator;
frac_sum->denominator = frac_sum->denominator * frac_num->denominator;
change_to_proper_fraction(frac_sum);
return;
}
}
[ 本帖最后由 yyqd2013 于 2013-5-10 00:30 编辑 ]