如何使用C中的复数?

如何使用C中的复数?

如何使用C中的复数?我看到有一个complex.h头文件,但它没有给我很多关于如何使用它的信息。如何以有效的方式访问实部和虚部?是否有本机功能来获取模块和阶段?



慕勒3428872
浏览 828回答 3
3回答

手掌心

这段代码对你有帮助,而且相当不言自明:#include <stdio.h>&nbsp; &nbsp; &nbsp; /* Standard Library of Input and Output */#include <complex.h>&nbsp; &nbsp; /* Standard Library of Complex Numbers */int main() {&nbsp; &nbsp; double complex z1 = 1.0 + 3.0 * I;&nbsp; &nbsp; double complex z2 = 1.0 - 4.0 * I;&nbsp; &nbsp; printf("Working with complex numbers:\n\v");&nbsp; &nbsp; printf("Starting values: Z1 = %.2f + %.2fi\tZ2 = %.2f %+.2fi\n", creal(z1), cimag(z1), creal(z2), cimag(z2));&nbsp; &nbsp; double complex sum = z1 + z2;&nbsp; &nbsp; printf("The sum: Z1 + Z2 = %.2f %+.2fi\n", creal(sum), cimag(sum));&nbsp; &nbsp; double complex difference = z1 - z2;&nbsp; &nbsp; printf("The difference: Z1 - Z2 = %.2f %+.2fi\n", creal(difference), cimag(difference));&nbsp; &nbsp; double complex product = z1 * z2;&nbsp; &nbsp; printf("The product: Z1 x Z2 = %.2f %+.2fi\n", creal(product), cimag(product));&nbsp; &nbsp; double complex quotient = z1 / z2;&nbsp; &nbsp; printf("The quotient: Z1 / Z2 = %.2f %+.2fi\n", creal(quotient), cimag(quotient));&nbsp; &nbsp; double complex conjugate = conj(z1);&nbsp; &nbsp; printf("The conjugate of Z1 = %.2f %+.2fi\n", creal(conjugate), cimag(conjugate));&nbsp; &nbsp; return 0;}&nbsp; 有:creal(z1):得到真正的部分(浮动crealf(z1),长双creall(z1))cimag(z1):得到虚部(浮动cimagf(z1),长双cimagl(z1))另外要记住很重要的一点,当使用复数的工作是一样的功能cos(),exp()并且sqrt()必须与他们复杂的形式,例如更换ccos(),cexp(),csqrt()。

天涯尽头无女友

Complex.h#include <stdio.h>&nbsp; &nbsp; &nbsp; /* Standard Library of Input and Output */#include <complex.h>&nbsp; &nbsp; /* Standart Library of Complex Numbers */int main()&nbsp;{&nbsp; &nbsp; double complex z1 = 1.0 + 3.0 * I;&nbsp; &nbsp; double complex z2 = 1.0 - 4.0 * I;&nbsp; &nbsp; printf("Working with complex numbers:\n\v");&nbsp; &nbsp; printf("Starting values: Z1 = %.2f + %.2fi\tZ2 = %.2f %+.2fi\n",&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;creal(z1),&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;cimag(z1),&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;creal(z2),&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;cimag(z2));&nbsp; &nbsp; double complex sum = z1 + z2;&nbsp; &nbsp; printf("The sum: Z1 + Z2 = %.2f %+.2fi\n", creal(sum), cimag(sum));}
打开App,查看更多内容
随时随地看视频慕课网APP