在 C 曼德布洛特程序中获取无效操作数到二进制错误



我应该用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遇到了很多问题,以便使其成为曼德布洛特印刷的替身。如果您可以查看是否可以找到我一直丢失的错误。这是我所有文件的代码:

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); }

曼德布罗特:

#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);

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);
}

复杂.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);

您的标头complex.h声明:

complex_t abs_complex(complex_t c);

您的实现complex.c定义:

double abs_complex(complex_t c) {

所以正在发生的事情是你的代码无法编译,因为头文件中的abs_complex说它返回一个complex_t而不是一个double

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

你有一行:

if (abs_complex(mandelbrot(15,c)) > 100)

您正在尝试将intcomplex_t进行比较,这是不允许的。 你需要重新思考你的逻辑。 你不能简单地比较>(或<或......)的复数。 您需要将abs_complex()的复杂结果转换为标量(doubleint),或者将100转换为complex_t并调用自定义比较函数。

mandelbrot.c中也有类似的错误。

相关内容

最新更新