我作为一个初学者,刚开始有很多的不懂,跟一张白纸一样,在学到复数也就是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;
}
这才成功的。
希望对初学者有帮助。
今天的文章关于vs2013如何使用complex头文件分享到此就结束了,感谢您的阅读,如果确实帮到您,您可以动动手指转发给其他人。
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://bianchenghao.cn/26741.html