正如标题所说,我正在制作一个程序来找到一小部分的GCD。该程序有一个找到它的功能,但我遇到了一些问题。
我希望函数接受两个结构变量(分母和分子)并返回GCD(整数)
这是我的代码:
#include <stdio.h>
struct fraction{
int numerator,denominator;
};
int find_gcd(struct fraction s1, struct fraction s1);
int main(void)
{
struct fraction d1;
int gcd;
d1.numerator= 20;
d1.denominator= 100;
printf("Fraction: %d/%d\n",d1.numerator,d1.denominator);
gcd= find_gcd(d1.numerator, d1.denominator);
printf("In lowest terms: %d/%d",d1.numerator/gcd,d1.denominator/gcd);
}
int find_gcd(struct fraction s1, struct fraction s1)
{
int remainder=0;
while (s1.denominator !=0)
{
remainder = s1.numerator/s1.denominator;
s1.numerator= s1.denominator;
s1.denominator= remainder;
}
return s1.numerator; //this is the gcd
}
我收到以下错误:
GCD_structures.c:8:50: error: redefinition of parameter 's1'
int find_gcd(struct fraction s1, struct fraction s1);
^
GCD_structures.c:8:30: note: previous definition of 's1' was here
int find_gcd(struct fraction s1, struct fraction s1);
^
GCD_structures.c: In function 'main':
GCD_structures.c:20:10: error: incompatible type for argument 1 of 'find_gcd'
gcd= find_gcd(d1.numerator, d1.denominator);
任何帮助都将不胜感激。
答案 0 :(得分:0)
在函数的签名中,两个参数都被命名为s1
修复此问题,可能是将其中一个参数重命名为s2