我对如何使用win32标准库在c语言中执行复数的算术运算感兴趣。例如:
#include <stdio.h>
#include <math.h>
#include <complex.h>
int main(int argc, char **argv)
{
_Dcomplex a;
_Dcomplex b = 2. + 3. * _Complex_I;
_Dcomplex c = a + b;
return 0;
}
据我了解,Microsoft不支持C99标准。如何绕过限制? 感谢
答案 0 :(得分:1)
但是,根据开发人员的需求,他们在Visual Studio 2013及更高版本中实现了许多C99库,这些库在这里提到:
https://blogs.msdn.microsoft.com/vcblog/2013/07/19/c99-library-support-in-visual-studio-2013/
因此,C中的代码可以写成:
#include <stdio.h> /* Standard Library of Input and Output */
#include <complex.h> /* Standard Library of Complex Numbers */
int main() {
double complex z1 = 1.0 + 3.0 * I;
double complex z2 = 1.0 - 4.0 * I;
printf("Initial values: Z1 = %.2f + %.2fi \nZ2 = %.2f %+.2fi\n", creal(z1), cimag(z1), creal(z2), cimag(z2));
double complex sum = z1 + z2;
printf("Sum: Z1 + Z2 = %.2f %+.2fi\n", creal(sum), cimag(sum));
double complex diff = z1 - z2;
printf("Diff: Z1 - Z2 = %.2f %+.2fi\n", creal(difference), cimag(difference));
double complex product = z1 * z2;
printf("Product: Z1 x Z2 = %.2f %+.2fi\n", creal(product), cimag(product));
double complex quotient = z1 / z2;
printf("Quotient: Z1 / Z2 = %.2f %+.2fi\n", creal(quotient), cimag(quotient));
double complex conjugate = conj(z1);
printf("Conjugate of Z1 = %.2f %+.2fi\n", creal(conjugate), cimag(conjugate));
return 0;
}
像cos(),exp()和sqrt()这样的函数必须用它们的复杂形式替换,例如ccos(),cexp(),csqrt(),它们工作正常。其他解决方法可能是完全刮掉MS编译器并使用在Visual C中工作的intel编译器(更加开明)。
更明智的方法是转移到Intel CC或gcc,并将Eclipse用于您的编程环境。跨Windows-Linux-Solaris-AIX-etc的代码的可移植性通常很重要,不幸的是MS工具完全不支持。