关于vs2013如何使用complex头文件

我作为一个初学者,刚开始有很多的不懂,跟一张白纸一样,在学到复数也就是complex的时候,照着书上抄了一段


编译了下面一段代码,结果显示出错。(书名《C语言入门经典第四版》)

代码如下:

#include <iostream>

#include <complex>



int main()

{

using namespace std;

double complex z1  = 1.0 + 2.0 * I;;

double real_part = creal(z1);

double imag_part = cimag(z1);

cout<<real_part<<"\t"

<<imag_part<<endl;


}

运行结果当然是出错了。

然后我废了好大功夫才找到方法,是书写方面的不规范,

代码如下:

// complex_complex.cpp  
// compile with: /EHsc  
#include <complex>  
#include <iostream>  


int main()
{
using namespace std;
double pi = 3.14159265359;


// The first constructor specifies real & imaginary parts  
complex <double> c1(4.0, 5.0);
cout << "Specifying initial real & imaginary parts,"
<< "c1 = " << c1 << endl;


// The second constructor initializes values of the real &  
// imaginary parts using those of another complex number  
complex <double> c2(c1);
cout << "Initializing with the real and imaginary parts of c1,"
<< " c2 = " << c2 << endl;


// Complex numbers can be initialized in polar form  
// but will be stored in Cartesian form  
complex <double> c3(polar(sqrt((double)8), pi / 4));
cout << "c3 = polar ( sqrt ( 8 ) , pi / 4 ) = " << c3 << endl;


// The modulus and argument of a complex number can be recovered  
double absc3 = abs(c3);
double argc3 = arg(c3);
cout << "The modulus of c3 is recovered from c3 using: abs ( c3 ) = "
<< absc3 << endl;
cout << "Argument of c3 is recovered from c3 using:\n arg ( c3 ) = "
<< argc3 << " radians, which is " << argc3 * 180 / pi
<< " degrees." << endl;
}

这才成功的。

希望对初学者有帮助。吐舌头