c++显示虚部为I的复数
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++新手
如何像-2+5i
一样显示i
?或者我必须添加i
字符与虚部?
您可以使用std::real()
和std::imag()
按照自己的喜好进行格式化,参见此处的complex。
当然,您必须自己检查是否有符号。
像这样:
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兼容格式),你可以专门为你的类型的放置操作符之前,包括<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";
}
以适合复制/粘贴到octave/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]
编辑:为苹果clang库添加宏
相关文章:
- 比较并显示使用最小值(a,b)和最大值(a、b)升序排列的4个数字
- C++,OpenCV,尝试显示图像时"OpenCV(4.3.0) Error: Assertion failed (size.width>0 && size.height>0)"此错误
- 字符串-C++后显示的随机字符
- 继承期间显示未知行为的子类
- 仅使用绝对值对数组进行排序,并在C++中显示实际值
- 程序崩溃并显示"std::out_of_range"错误
- 为什么虚函数不能是静态的和全局的?
- 如何在C++中用std::cout正确显示带十六进制的字符串文本
- 为什么在C的循环中使用printf的Rust代码不显示输出,而在C++的循环中显示std::cout
- 有没有办法修改'operator->',以便'z->im'返回复数的虚部
- 是否可以保证输入<Type>实数时复杂对象的虚部设置为零?
- 如何在 Eigen3 库中高效提取复矩阵的实部/虚部?
- FFTW和OpenCV的C++接口,Mat输出中的实部和虚部
- 如何将实部和虚部放入一个变量中,就像在matlab中一样
- 在c++的复杂类中,为实部和虚部指定了多少位
- 复数的实部和虚部
- FFTW3:解释fftw_plan_r2c_1d输出并访问输出的虚部
- c++显示虚部为I的复数
- 在c++中获取指向复向量实部和虚部的指针
- 用C++中的[]运算符可以访问复数的实部和虚部吗