假设已经给出了这些功能:
include <stdio.h> /* printf */
include "fractions.h" /* struct FRACTION, add_fractions */
struct FRACTION make_fraction(int numerator, int denominator)
{
struct FRACTION f;
f.numerator = numerator;
f.denominator = denominator;
return f;
}
void test_fraction(int numerator1, int denominator1,
int numerator2, int denominator2)
{
struct FRACTION a = make_fraction(numerator1, denominator1);
struct FRACTION b = make_fraction(numerator2, denominator2);
struct FRACTION c = add_fractions(&a, &b);
printf("%i/%i + %i/%i = %i/%i\n", a.numerator, a.denominator,
b.numerator, b.denominator,
c.numerator, c.denominator);
}
void testGCD(void)
{
int m, n;
m = 15; n = 18; /* GCD is 3 */
printf("GCD of %i and %i is %i\n", m, n, GCD(m, n));
printf("GCD of %i and %i is %i\n", n, m, GCD(n, m));
m = 80; n = 20; /* GCD is 20 */
printf("GCD of %i and %i is %i\n", m, n, GCD(m, n));
printf("GCD of %i and %i is %i\n", n, m, GCD(n, m));
m = 21; n = 47; /* GCD is 1 */
printf("GCD of %i and %i is %i\n", m, n, GCD(m, n));
printf("GCD of %i and %i is %i\n", n, m, GCD(n, m));
m = 68; n = 153; /* GCD is 17 */
printf("GCD of %i and %i is %i\n", m, n, GCD(m, n));
printf("GCD of %i and %i is %i\n", n, m, GCD(n, m));
}
int main(void)
{
testGCD();
test_fraction(2, 3, 1, 6);
test_fraction(1, 5, 4, 9);
test_fraction(3, 7, 12, 21);
test_fraction(5, 8, 3, 16);
test_fraction(7, 8, 3, 12);
test_fraction(0, 8, 3, 16);
test_fraction(1, 1, 3, 16);
test_fraction(5, 8, -3, 16);
test_fraction(1, 5, -4, 9);
test_fraction(-1, 5, -4, 9);
return 0;
}
我的任务是写GCD()
和add_fractions()
,这就是我写的:
include "fractions.h"
struct FRACTION add_fractions(const struct FRACTION *a, const struct FRACTION *b)
{
struct FRACTION c ; /*result struct*/
/* int GCD_a = GCD(a.numerator, a.denominator); GCD of the fraction a*/
/*int GCD_b = GCD(b.numerator, b.denominator); GCD of the fraction b*/
c.numerator = (a.numerator) + (b.numerator);
c.denominator = a.denominator ;
return c;
/* struct FRACTION empty;*/
/*return empty;*/
}
int GCD(int a, int b)
{
/*Variables*/
int remainder = 0; /*remainder*/
int larger = a;
int smaller = b;
remainder = larger % smaller;
while (remainder != 0)
{
larger = smaller;
smaller = remainder;
remainder = larger % smaller;
}
return smaller;
}
假设目前两个分母都是平等的,为什么我不能用Cygwin运行呢?我用这个命令编译
gcc -Wall -Wextra -ansi -pedantic -Wno-unused-parameters main.c fractions.c -o fractions.exe
我有两个错误:( Cygwin在我的电脑上是西班牙语,所以我不确定我要写的是确切的翻译):
error: trying to put "numerator" in something which is not a struct
(分母也一样)
有什么问题?
答案 0 :(得分:5)
const struct FRACTION *a, const struct FRACTION *b
所以a
和b
是指向常量struct FRACTION
的指针。然后你写下:
c.numerator = (a.numerator) + (b.numerator);
您不使用.
访问结构指针的成员,但使用->
,这应该是
c.numerator = a->numerator + b->numerator;
P上。秒。 1:不需要括号,不要把它们放在多余的地方,它们会降低可读性。
P上。秒。 2:你的添加公式被破坏,使用
c.numerator = a->numerator * b->denominator + b->numerator * a->denominator;
c.denominator = a->denominator * b->denominator;
代替。
答案 1 :(得分:1)
在add_fractions
函数中,您已将a
和b
声明为指针至struct FRACTION
:
struct FRACTION add_fractions(const struct FRACTION *a, const struct FRACTION *b)
由于a
和b
是指针,而不是结构,因此您无法编写a.numerator
之类的内容。
在使用a
运算符之前,您需要取消引用 b
和.
:
c.numerator = ((*a).numerator) + ((*b).numerator);
c.denominator = (*a).denominator ;
或使用->
组件选择运算符,这是执行相同操作的简便方法:
c.numerator = (a->numerator) + (b->numerator);
c.denominator = a->denominator ;
答案 2 :(得分:1)
除了user529758
之外,你的加法算法应该是
n1 * d2 + n2 * d1
= ------------------
d1 * d2
我相信Jonathan需要强调的是,您需要将结果的分子和分母除以GCD
以减少resulting fraction to its lowest form。
例如,您可以实现一个新功能来进行缩减:
struct FRACTION reduce_fraction(const struct FRACTION *frac)
{
struct FRACTION reduced;
int gcd = GCD(frac->numerator, frac->denominator);
reduced.numerator = frac->numerator / gcd;
reduced.denominator = frac->denominator / gcd;
return reduced;
}
您的add_fraction
功能现在看起来像这样:
struct FRACTION add_fractions(const struct FRACTION *a, const struct FRACTION *b)
{
struct FRACTION c;
c.numerator = (a->numerator * b->denominator) + (b->numerator * a->denominator);
c.denominator = a->denominator * b->denominator;
return reduce_fraction(&c);
}
另外,鉴于您已经完成了确定评论结果的艰苦工作,而不是printf
stdout
,为什么不assert
这些 - 这为自动化单元测试提供了基础:
#include <assert.h>
...
m = 15; n = 18;
assert(GCD(m, n) == 3);
assert(GCD(m, n) == GCD(n,m));
m = 80; n = 20;
assert(GCD(m, n) == 20);
assert(GCD(m, n) == GCD(n,m));
同样适用于add_fractions
和reduce_fraction
:)