我应该用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打印出来。如果可以,请查看您是否能找到我一直遗漏的错误。这是我所有文件的代码:
#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); }
#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;
}
complex_t mandelbrot(int n, complex_t 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);
答案 0 :(得分:1)
您的标题complex.h
声明:
complex_t abs_complex(complex_t c);
您的实施complex.c
定义:
double abs_complex(complex_t c) {
所发生的事情是您的代码无法编译,因为标头文件中的abs_complex
表示它返回complex_t
而不是double
由于定义/声明不匹配,推测complex.c
也不会编译。
答案 1 :(得分:0)
你有一句话:
if (abs_complex(mandelbrot(15,c)) > 100)
您正在尝试将int
与complex_t
进行比较,而这是不允许的。你需要重新思考你的逻辑。您无法简单地比较>
(或<
或...)的复数。您需要将abs_complex()
的复杂结果转换为标量(double
或int
)或将100
转换为complex_t
并调用自定义比较功能。
mandelbrot.c
中存在类似的错误。