C ++在虚部显示带i的复数 [英] c++ display complex number with i in imaginary part
本文介绍了C ++在虚部显示带i的复数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在做类似的事情:
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)
I对c ++来说是非常新的
I am very new to c++
如何使用 i
来显示,例如 -2 + 5i
?还是我必须在虚构部分添加 i
char?
How can I display with i
like -2+5i
? Or I have to add i
char with imagginary part ?
推荐答案
您可以使用 std :: real()
和 std :: imag()
进行格式化,请参见这里很复杂。
You can use std::real()
and std::imag()
to format as you like, see complex here.
当然,您必须自己检查符号。
Of course, you will have to check for sign yourself.
像这样的事情:
std::cout
<< std::real(mycomplex)
<< (std::imag(mycomplex) >= 0.0 ? "+" : "")
<< std::imag(mycomplex)
<< " i"
<< std::endl;
这篇关于C ++在虚部显示带i的复数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文