QTextStream 用于控制台输出

发布于 2024-10-05 21:58:57 字数 1107 浏览 2 评论 0原文

我面临着用于控制台输出的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

鲜肉鲜肉永远不皱 2024-10-12 21:58:57

如果我猜的话,我会说 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 the endl.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文