我对如何使用标准库 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 标准。如何绕过限制? 谢谢
最佳答案
可悲的是,早期的微软并不急于提供 C99 库支持,因为它的用户群相对较小,而且在将基于现代 gcc 的代码移植到 MSVC 时会很痛苦。这是一个优先事项。
但是,应开发者的需求,他们在 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 CC 或 gcc,并使用 Eclipse 作为您的编程环境。代码在 Windows-Linux-Solaris-AIX 等平台上的可移植性通常很重要,不幸的是,MS 工具根本不支持这一点。
关于c - c中复数的算术运算,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43013967/