complex在c语言用法,C++ complex abs()用法及代码示例
时间: 2023-12-28 14:05:06 浏览: 160
C++调用Cplex例子.zip_cplex_整数规划
5星 · 资源好评率100%
在 C 语言中,complex 是一种复数类型,可以使用 <complex.h> 头文件中的函数来操作它。具体用法如下:
```c
#include <stdio.h>
#include <complex.h>
int main() {
double complex z1 = 1.0 + 2.0 * I;
double complex z2 = 3.0 - 4.0 * I;
double complex z3 = z1 + z2;
double complex z4 = z1 * z2;
printf("z1 = %f + %f I\n", creal(z1), cimag(z1));
printf("z2 = %f + %f I\n", creal(z2), cimag(z2));
printf("z1 + z2 = %f + %f I\n", creal(z3), cimag(z3));
printf("z1 * z2 = %f + %f I\n", creal(z4), cimag(z4));
return 0;
}
```
在 C++ 中,complex 也是一种复数类型,使用 <complex> 头文件中的函数来操作它。其中,abs() 函数用于计算复数的模长,具体用法如下:
```c++
#include <iostream>
#include <complex>
using namespace std;
int main() {
complex<double> z1(1, 2);
complex<double> z2(3, -4);
complex<double> z3 = z1 + z2;
complex<double> z4 = z1 * z2;
cout << "z1 = " << z1 << endl;
cout << "z2 = " << z2 << endl;
cout << "z1 + z2 = " << z3 << endl;
cout << "z1 * z2 = " << z4 << endl;
cout << "abs(z1) = " << abs(z1) << endl;
return 0;
}
```
阅读全文