QTextStream 用于控制台输出
我面临着用于控制台输出的 QTextStream 这个非常烦人的问题。
QTextStream cout(stdout, QIODevice::WriteOnly);
cout.setRealNumberPrecision(1);
cout.setPadChar('.');
// some code generating values of f[i] [...]
for (int i = 10; i >= 0; i--)
{
if (f[i] < -0.04 || f[i] > 0.04 || 1)
{
cout.setRealNumberNotation(QTextStream::FixedNotation);
cout.setFieldAlignment(QTextStream::AlignRight);
cout.setFieldWidth(8);
cout << f[i];
cout.setFieldAlignment(QTextStream::AlignLeft);
cout.setFieldWidth(3);
cout << "*x^";
cout.setFieldAlignment(QTextStream::AlignLeft);
cout.setNumberFlags(cout.numberFlags() & ~QTextStream::ForceSign);
cout << i << endl;
}
}
结果看起来像这样 找到的多项式是:
.....0.0*x^10.
......-0.0*x^9..
.......0.0*x^8..
......-0.0*x^7..
.......0.0*x^6..
.......1.0*x^5..
.....-36.0*x^4..
.....397.0*x^3..
...-1674.0*x^2..
....2753.0*x^1..
...-1440.0*x^0..
..
我无法摆脱第一行中的这种奇怪的转变,而且我不知道 ..
来自哪里。我认为对齐标志可能存在一些问题,但不知道到底是什么。
感谢您的帮助。
I'm facing this really annoying problem with QTextStream used for console output.
QTextStream cout(stdout, QIODevice::WriteOnly);
cout.setRealNumberPrecision(1);
cout.setPadChar('.');
// some code generating values of f[i] [...]
for (int i = 10; i >= 0; i--)
{
if (f[i] < -0.04 || f[i] > 0.04 || 1)
{
cout.setRealNumberNotation(QTextStream::FixedNotation);
cout.setFieldAlignment(QTextStream::AlignRight);
cout.setFieldWidth(8);
cout << f[i];
cout.setFieldAlignment(QTextStream::AlignLeft);
cout.setFieldWidth(3);
cout << "*x^";
cout.setFieldAlignment(QTextStream::AlignLeft);
cout.setNumberFlags(cout.numberFlags() & ~QTextStream::ForceSign);
cout << i << endl;
}
}
The results look like this with
the found polynomial is:
.....0.0*x^10.
......-0.0*x^9..
.......0.0*x^8..
......-0.0*x^7..
.......0.0*x^6..
.......1.0*x^5..
.....-36.0*x^4..
.....397.0*x^3..
...-1674.0*x^2..
....2753.0*x^1..
...-1440.0*x^0..
..
I cannot get rid of this strange shift in the first line, and I don't know where ..
comes from. I think that there might be some problem with alignment flags, but have no idea what exactly may it is.
Thanks for the help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果我猜的话,我会说
endl
左对齐并填充到 3 个字符,额外的两个字符出现在 return 后,以便它们显示在下一行的开头。第一行没有,最后一行只有一个。在输出endl
之前,尝试将字段宽度设置回 1。If I were to guess, I'd say the
endl
is being left aligned and padded to 3 characters, with the extra two characters appearing after the return so that they show up at the beginning of the next line. None on the first line and all alone on the last line. Try setting the field width back to 1 before outputting theendl
.