我正在做类似的事情:
int real_part, imaginary_part;
cout<<"Please enter realpart and imaginary part"<<endl;
cin>>real_part;
cin>>imaginary_part;
complex<double> mycomplex (real_part, imaginary_part);
cout<<mycomplex<<endl; // I want to display like -2+5i but I get (-2, 5)
我对c++很陌生
如何使用 i
显示 -2+5i
?或者我必须添加带有虚部的 i
字符?
最佳答案
您可以使用std::real()
和std::imag()
来格式化,参见complex here .
当然,您必须自己检查签名。
像这样:
std::cout
<< std::real(mycomplex)
<< (std::imag(mycomplex) >= 0.0 ? "+" : "")
<< std::imag(mycomplex)
<< " i"
<< std::endl;
关于c++显示虚数部分为i的复数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40245110/