[英]How are complex numbers(and the imaginary number i) represented in Native C as opposed to C++?
[英]c++ display complex number with i in imaginary part
我正在做类似的事情:
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()
随意设置格式,请参见此处的复杂 。
当然,您将需要亲自检查是否签名。
像这样:
std::cout
<< std::real(mycomplex)
<< (std::imag(mycomplex) >= 0.0 ? "+" : "")
<< std::imag(mycomplex)
<< " i"
<< std::endl;
您可以简单地写:
cout<< mycomplex.real << std::showpos << mycomplex.imag << "i" << endl;
为了完整起见,另一个答案。 您可以使用std::showpos
更轻松地将输出格式化为已签名的内容
cout << real(mycomplex) << std::showpos << imag(mycomplex) << "i";
如果您真的很偷偷摸摸,或者正在使用您不想在任何地方都不能修改的库(即,我使用它以八度/ matlab兼容格式打印本征矩阵),则可以为您的类型专门设置put-to运算符在包含<complex>
之前。 我怀疑这违反了标准,因为它在std::
乱码了,但是在g ++(7.3.1)和clang ++(5.0)中有效:
/*
* this stuff can go in a header to make std::complex<> available
*/
typedef double real_t;
#include <iostream>
#define SPECIALIZED_COMPLEX_PUTTO
#ifdef SPECIALIZED_COMPLEX_PUTTO
#ifdef _LIBCPP_BEGIN_NAMESPACE_STD // for clang++ libs
_LIBCPP_BEGIN_NAMESPACE_STD
#else
namespace std {
#endif
template <typename T> class complex;
template <class T, class CharT, class Traits>
std::basic_ostream<CharT, Traits>&
operator<<(std::basic_ostream<CharT, Traits>& os,
const std::complex<T>& x);
// specialization for real_t, instantiate later
template<>
basic_ostream<char>&
operator<<(basic_ostream<char> & o, const complex<real_t> & x);
#ifdef _LIBCPP_END_NAMESPACE_STD // for clang++
_LIBCPP_END_NAMESPACE_STD
#else
}
#endif
#include <complex>
/*
* below here can go in a .cpp file
*/
#ifdef SPECIALIZED_COMPLEX_PUTTO
#ifdef _LIBCPP_BEGIN_NAMESPACE_STD // for clang++ libs
_LIBCPP_BEGIN_NAMESPACE_STD
#else
namespace std {
#endif
template<>
basic_ostream<char>&
operator<<(basic_ostream<char> & o, const complex<real_t> & x)
{
basic_ostringstream<char> s;
s.flags(o.flags());
s.imbue(o.getloc());
s.precision(o.precision());
s << x.real() << std::showpos << x.imag() << 'i';
return o << s.str();
}
#ifdef _LIBCPP_END_NAMESPACE_STD // for clang++
_LIBCPP_END_NAMESPACE_STD
#else
}
#endif
int main(int argc, char * argv[])
{
std::complex<real_t> x(1.1,-2.2);
std::cout << x << "\n";
}
输出
1.1-2.2i
或者,针对本征案例的示例main()
:
#include <Eigen/Dense>
int main(int argc, char * argv[])
{
Eigen::Matrix<std::complex<real_t>, 3,3> x;
Eigen::IOFormat OctaveFmt(Eigen::StreamPrecision, 0, ", ", ";\n", "", "", "[", "]");
//srand((unsigned int) time(0));
x.setRandom();
std::cout << x.format(OctaveFmt) << "\n";
}
以适合复制/粘贴到八度/ matlab的格式输出矩阵:
[ 0.680375-0.211234i, -0.329554+0.536459i, -0.270431+0.0268018i;
0.566198+0.59688i, -0.444451+0.10794i, 0.904459+0.83239i;
0.823295-0.604897i, -0.0452059+0.257742i, 0.271423+0.434594i]
编辑:添加了苹果铛库的宏。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.