c - c中复数的算术运算

标签 c windows visual-studio-2015

我对如何使用标准库 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/

相关文章:

对结构的 C 代码引用

在没有管理员权限或无法访问命令行的情况下使用 MinGW 进行编译

c++ - 适用于 Visual Studio 的 OpenMP 3.0

c - C 程序中整数的奇怪行为

c - 在子进程中使用信号

java - 无法定位符号 "__android_log_write"- Android 原生日志记录

windows - Windows 上的 GIT 问题(文件名或扩展名太长)

c# - 如何在 C# 中使用带有 ProtectSection 方法的 RsaProtectedConfigurationProvider 创建可导出的 RSA key

xaml - 无法让 Xamarin Xaml Intellisense 在 VS 2015 中工作

c++ - 从非静态成员初始化静态数据成员