gpt4 book ai didi

c - C Mandelbrot 程序中的二进制错误导致无效操作数

转载 作者:行者123 更新时间:2023-11-30 15:20:50 26 4
gpt4 key购买 nike

我应该用 C 语言编写一个程序来创建 Mandelbrot。我有具体的指示要遵循,并且我很确定我已正确遵循它们。我的问题是,当我尝试使用 gcc(使用 make 命令)编译代码时,出现错误:

mason> make
gcc -c main.c
main.c: In function ‘main’:
main.c:13:40: error: invalid operands to binary < (have ‘complex_t’ and ‘int’)

我的 complex_t 遇到了很多问题,为了使其成为 Mandelbrot 打印的 double 值。如果你可以的话,请看看你是否能找到我一直遗漏的错误。这是我所有文件的代码:

main.c(错误所在):

#include <stdio.h>
#include "complex.h"
#include "mandelbrot.h"

int main (void)

{
complex_t c;
for (c.imag = -1.2; c.imag < 2.8; c.imag+= 0.05) {
for (c.real = -2.1; c.real < -0.66; c.real+= 0.032)
{
if (abs_complex(mandelbrot(15,c)) > 100)
{
printf("-");
}
else
printf("#");
}
printf("\n"); }
return (0); }

曼德尔布罗特.c:

#include "complex.h"
#include "mandelbrot.h"

complex_t mandelbrot(int n, complex_t c)
{

complex_t m = {100,100};
complex_t tmp;

if (n == 0)
{
return c;
}

else

if (abs_complex(mandelbrot(n-1,c) > 1000))
{
return m;
}

else
{

tmp = mandelbrot(n-1,c);
tmp = cmult(tmp, tmp);
tmp = cadd(tmp,c);

}
return tmp;
}

曼德尔布罗特.h:

complex_t mandelbrot(int n, complex_t c);

complex.c:

#include <stdio.h>
#include <math.h>
#include "complex.h"


/*
* Complex number input function returns standard scanning error code
* 1 => valid scan, 0 => error, negative EOF value => end of file
*/
int
scan_complex(complex_t *c) /* output - address of complex variable to
fill */
{
int status;

status = scanf("%lf%lf", &c->real, &c->imag);
if (status == 2)
status = 1;
else if (status != EOF)
status = 0;

return (status);
}

/*
* Complex output function displays value as (a + bi) or (a - bi),
* dropping a or b if they round to 0 unless both round to 0
*/
void
print_complex(complex_t c) /* input - complex number to display */
{
double a, b;
char sign;

a = c.real;
b = c.imag;

printf("(");

if (fabs(a) < .005 && fabs(b) < .005) {
printf("%.2f", 0.0);
} else if (fabs(b) < .005) {
printf("%.2f", a);
} else if (fabs(a) < .005) {
printf("%.2fi", b);
} else {
if (b < 0)
sign = '-';
else
sign = '+';
printf("%.2f %c %.2fi", a, sign, fabs(b));
}

printf(")");
}

/*
* Returns sum of complex values c1 and c2
*/
complex_t
add_complex(complex_t c1, complex_t c2) /* input - values to add */
{
complex_t csum;

csum.real = c1.real + c2.real;
csum.imag = c1.imag + c2.imag;

return (csum);
}

/*
* Returns difference c1 - c2
*/
complex_t
subtract_complex(complex_t c1, complex_t c2) /* input parameters */
{
complex_t cdiff;
cdiff.real = c1.real - c2.real;
cdiff.imag = c1.imag - c2.imag;

return (cdiff);
}

/* ** Stub **
* Returns product of complex values c1 and c2
*/
complex_t
multiply_complex(complex_t c1, complex_t c2) /* input parameters */
{
complex_t cmul;
double a, b, c, d;
a = c1.real;
b = c1.imag;
c = c2.real;
d = c2.imag;

if (( b > 0 && d < 0) || (b < 0 && d > 0))
{
cmul.real - (a*c) + (fabs(b)*fabs(d));
cmul.imag = (a*d) + (b*c);
}
else if (( b>0 && d>0) || (b<0 && d<0))
{
cmul.real = (a*c) - (b*d);
cmul.imag = (a*d) + (b*c);
}
return (cmul);
}

/* ** Stub **
* Returns quotient of complex values (c1 / c2)
*/
complex_t
divide_complex(complex_t c1, complex_t c2) /* input parameters */
{
complex_t cdiv;
double a, b, c, d;
a = c1.real;
b = c1.imag;
c = c2.real;
d = c2.imag;

if ( b > 0 && d < 0)
{
cdiv.real = (a*c) - (fabs(b)*fabs(d)) / ((c*c) + (d*d));
cdiv.imag = (a*d) + (b*c) / ((c*c) + (d*d));
}
else if ( b>0 && d>0)
{
cdiv.real = (a*c) - (fabs(b)*fabs(d)) / ((c*c) + (d*d));
cdiv.imag = ((-1*a*d) + (b*c)) / ((c*c) + (d*d));
}
else if (b<0 && d<0)
{
cdiv.real = (a*c) + (fabs(b)*fabs(d)) / ((c*c) + (d*d));
cdiv.imag = ((-1*a*d) + (b*c)) / ((c*c) + (d*d));
}
else if (b<0 && d<0)
{
cdiv.real = (a*c) + (fabs(b)*fabs(d)) / ((c*c) + (d*d));
cdiv.imag = ((a*fabs(d)) + (b*c)) / ((c*c) + (d*d));
}
return (cdiv);
}
/*
* Returns absolute value of complex number c
*/
double
abs_complex(complex_t c) /* input parameter */
{
complex_t cabs;

cabs.real = sqrt(c.real * c.real + c.imag * c.imag);

return (cabs.real);
}

complex.h:

typedef struct {
double real, imag;
} complex_t;

int scan_complex(complex_t *c);
void print_complex(complex_t c);
complex_t add_complex(complex_t c1, complex_t c2);
complex_t subtract_complex(complex_t c1, complex_t c2);
complex_t multiply_complex(complex_t c1, complex_t c2);
complex_t divide_complex(complex_t c1, complex_t c2);
complex_t abs_complex(complex_t c);

最佳答案

您的 header complex.h 声明:

complex_t abs_complex(complex_t c);

您的实现complex.c定义:

double abs_complex(complex_t c) {

所以发生的情况是您的代码无法编译,因为头文件中的 abs_complex 表示它返回 complex_t 而不是 double.

由于定义/声明不匹配,complex.c 也可能无法编译。

关于c - C Mandelbrot 程序中的二进制错误导致无效操作数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29859870/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com