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