我在为分配需要做的以下事情上遇到了很大的困难
:声明一个包含有理数的数据结构。
湾。写 f'xns 将 +, -, *, / 有理数。
所有 f'xns 都必须传递 3 个参数,每个参数都指向我在 a 部分声明的类型的数据结构;参数中的 2 个 = 操作数,第 3 个 = 结果。
C。编写一个 f'xn,它将指向您的数据结构的指针作为参数并返回数字的 GCD。& 面额。
d。使用 c 部分中的 f'xn 编写一个 f'xn,它将分数(有理数)减少到最低项。传入一个指向分数的指针,并让 f'xn 修改分数。
e.编写输入和输出函数,以便用户可以输入 1/5 形式的分数。
应该允许用户输入任意数量的问题,并且程序应该以最低的形式输出答案。
我在正确的轨道上吗?我相信我有 ac,但没有 d,尤其是 e。有人可以指导我或帮助我更正我的脚本吗?
int GCD (int numer, int denom)
{
int result;
while (denom > 0) {
result = numer % denom;
numer = denom;
denom = result;
}
return numer;
}
int getLCM (int numer, int denom)
{
int max;
max = (numer > denom) ? numer : denom;
while (1) {
if (max % numer == 0 && max % denom == 0)
break;
++max;
}
return max;
}
struct Fraction
{
int numer;
int denom;
};
typedef struct
{
int numer;
int denom;
};
Fraction
Fraction add_fractions (Fraction a, Fraction b)
{
Fraction sum;
sum.numer = (a.numer * b.denom) + (b.numer * a.denom);
sum.denom = a.denom * b.denom;
return sum;
}
Fraction subtract_fractions (Fraction a, Fraction b)
{
Fraction sum;
sum.numer = (a.numer * b.denom) - (b.numer * a.denom);
sum.denom = a.denom * b.denom;
return sum;
}
Fraction multiply_fractions (Fraction a, Fraction b)
{
Fraction sum;
sum.numer = (a.denom * b.denom);
sum.denom = (a.numer * b.numer);
return sum;
}
Fraction divide_fractions (Fraction a, Fraction b)
{
Fraction sum;
sum.numer = (a.denom * b.numer);
sum.denom = (a.numer * b.denom);
return sum;
}
int main ()
{
char response;
printf ("FRACTION ARITHMETIC PROGRAM\n");
printf ("Enter your problem (example 2/3 + 1/5):\n");
scanf (, &problem);
if (denom == 0 || denom < 0) {
printf ("Illegal input!!\n");
printf ("Another problem (y/n)? ");
scanf ("%c%*c", &response);
} else {
printf ("The answer is ");
printf ("Another problem (y/n)? ");
scanf ("%c%*c", &response);
}
while ((response == 'y') || (response == 'Y')) {
printf ("\nWould you like to play again?\n");
scanf ("%c%*c", &response);
}
while ((response == 'n') || (response == 'N'))
printf ("Goodbye and thank you");
return 0;
}
由于评论回复,删除 typedef 后进行编辑:
struct Fraction {
int numer;
int denom;
};
struct Fraction add_fractions (struct Fraction a, struct Fraction b)
{
struct Fraction sum;
sum.numer = (a.numer * b.denom) + (b.numer * a.denom);
sum.denom = a.denom * b.denom;
return sum;
}
struct Fraction subtract_fractions (struct Fraction a, struct Fraction b)
{
struct Fraction sum;
sum.numer = (a.numer * b.denom) - (b.numer * a.denom);
sum.denom = a.denom * b.denom;
return sum;
}
struct Fraction multiply_fractions (struct Fraction a, struct Fraction b)
{
struct Fraction sum;
sum.numer = (a.denom * b.denom);
sum.denom = (a.numer * b.numer);
return sum;
}
struct Fraction divide_fractions (struct Fraction a, struct Fraction b)
{
struct Fraction sum;
sum.numer = (a.denom * b.numer);
sum.denom = (a.numer * b.denom);
return sum;
}