What type of data should I use to represent a complex number? (C language)
This question already has an answer here:
A complex number should be a struct
in C.
struct complex {
double real;
double imaginary;
}
Because C does not support operator overloading (like, for example, C++ does), you cannot use operators "+" and "-", but instead need to implement functions like add
and sub
.
struct complex add (struct complex c1, struct complex c2) {
struct complex result;
result.real = c1.real + c2.real;
result.imaginary = c1.imaginary + c2.imaginary;
return result;
}
That answers the question that you asked - Starting with c99, there is, however, a C99 type that supports complex numbers more directly.
链接地址: http://www.djcxy.com/p/24384.html