简体   繁体   English

如何正确格式化此表?

[英]How do I properly format this table?

I'm trying to properly format a table so that it prints out values below each column, beginning with the first character of that column. 我正在尝试正确设置表格的格式,以使其在每列下方打印出值,并从该列的第一个字符开始。 I've had success formatting three columns, but I'm unable to figure out how to format the K Count and LM Count columns so that they are printed out in a neat fashion. 我已经成功格式化了三列,但是我无法弄清楚如何格式化K Count和LM Count列,以便它们以整洁的方式打印出来。

What are some corrections I can make to the while loop portion of the code so that the K count and LM count columns are printed out neatly? 我可以对代码的while循环部分进行哪些更正,以使K计数和LM计数列整齐地打印出来?

void printTable(const vector<int>& z, const vector<long>& x, const 
vector<long>& y,
const vector<int>& a, const vector<int>& b)
{
ostringstream ss;
ss << "\n\n\n" << setw(10) << left << "Digits" << "Input Numbers       "
    << setw(11) << right << "K Output   " << setw(6)
    << right << "K Count     " << setw(10) << right << "LM Output   " << setw(6)
    << right << "LM Count" << endl;


int i = 0, n = 0;
while (i < 5)
{
    string q = to_string(z[2*n]) + " x " + to_string(z[abs(2*n + 1)]);
    string r = to_string(x[i]);
    string s = to_string(a[i]);
    string t = to_string(y[i]);
    string u = to_string(b[i]);

    ss << setw(10) << left << (i+1) <<  q
        << setw(16) << right << r
        << setw(11) << right << s
        << setw(12) << right << t
        << setw(10) << right << u << endl;

    i++;
    n++;
}

string r = ss.str();
cout << r;
}

在此处输入图片说明

You're printing the following: 您正在打印以下内容:
- Digits left aligned over 10 chars -剩余的数字对齐超过10个字符
- Directly followed by the input number without specifying the width -直接输入数字,不指定宽度
- K output right aligned over 16 chars -K输出右对齐超过16个字符
You should instead specify the width of the input numbers as well. 您也应该指定输入数字的宽度。
Try this: 尝试这个:

ss << setw(10) << left  << (i+1) <<
      setw(16) << left  << q <<
      setw(10) << right << s <<
      setw(12) << right << t <<
...

The exact width of each column may not be correct. 每列的确切宽度可能不正确。 Try it out yourself. 自己尝试一下。

To fix K Count output, you should reset the cursor to align left after setting the width for the previous result, then shift back right. 要固定K计数输出,应在设置上一个结果的宽度后将光标重置为左对齐,然后再右移。 Something like this should do what you want. 这样的事情应该做你想要的。

ss << setw(10) << left  << (i+1) << right
    setw(16) << left << q << right <<
    setw(11) << left << r << right <<
    setw(12) << left << s << right <<
    setw(12) << left << t << right <<
    setw(10) << left << u << right << endl;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM