以两列显示数据 c++ [英] Display data in two columns c++

查看:31
本文介绍了以两列显示数据 c++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图显示相同的数据,但在两列中,所以在第 19 周后,数据应显示在表格右侧的列中,直到达到总学周数,即 36.以下是代码:

I am trying to display the same data but in two columns, so after school week 19, the data should be displayed in the columns to the right of the table until the total school weeks is reached i.e. 36. Below is the code:

#include "stdafx.h"
#include <iostream>
#include <string>
#include <iomanip>

using namespace std;
int const schoolWeeks = 37;
string week[] = { "A", "B", "C", "D" };
int num = 0;

int main()
{
cout << left << setw(9) << setfill(' ') << "Week " << left << setw(9) << setfill(' ')<< "Menu" << left << setw(9) << setfill(' ') << "Week " << "Menu" << endl;
for (int i = 1; i < 20; i++)
{   
    cout << left << setw(9) << setfill(' ')<< i << week[num] << endl;

    num = num + 1;
    if (num == 4)
    {
        num = 0;
    }

}
for (int i = 20; i < schoolWeeks; i++)
{
    cout << left << setw(9) << setfill(' ') << i << week[num] << endl;

    num = num + 1;
    if (num == 4)
    {
        num = 0;
    }

}

}

推荐答案

使用 std::cout 的标准输出基于 数据流 的想法.请记住,它被称为iostream"是有原因的.这意味着一旦你向 std::cout 写了一些东西,你就不能返回"几行并添加一些东西.

Standard output with std::cout is based on the idea of a stream of data. Remember, it's called "iostream" for a reason. This means once you have written something to std::cout, you simply cannot "go back" a few lines and add something.

考虑在 Linux shell 或 Windows 命令行中进行管道传输.可以将程序的输出重定向到另一个程序的输入,然后它可以用它做任何可以想象的事情,例如通过互联网发送:

Consider piping in a Linux shell or on the Windows command line. It's possible to redirect the output of your program to be the input of another program, which could then do any imaginable thing with it, e.g. sending it over the internet:

myprogram.exe > otherprogram.exe

在这种情况下,你怎么可能在已经写好的行中添加一些东西?

How could you possibly add something to an already written line in such a scenario?

考虑到所有这些,很明显唯一可行的解​​决方案是提前知道每一行的内容.你不能等到第 19 行再考虑第二列,你必须马上去做.换句话说,首先打印第 1 周和第 19 周,然后是第 2 周和第 20 周,然后是第 3 周和第 21 周,依此类推.看到图案了吗?它总是第一列第 X 周,第二列第 X+18 周".这几乎就是解决方案.

Considering all of this, it becomes clear that the only viable solution is to know in advance the contents of every line. You cannot wait until line 19 before thinking about the second column, you must do it right away. In other words, first print week 1 and week 19, then week 2 and week 20, then week 3 and week 21, and so on. See the pattern? It's always "week X in column one, week X+18 in column two". And that's pretty much the solution.

这是一个快速编写的循环,遵循现有代码的风格:

Here's a quickly written loop adhering to the style of your existing code:

for (int i = 1; i < 19; i++)
{   
    cout << left << setw(9) << setfill(' ')<< i << week[num]
         << left << setw(9) << setfill(' ') << " " << (i + 18) << " "
         << left << setw(9) << setfill(' ') << " " << week[num]
         << endl;

    num = num + 1;
    if (num == 4)
    {
        num = 0;
    }
}

std::setwstd::setfill 之类的操纵器可能会使用一些改进,但重点是:

The manipulators like std::setw or std::setfill could probably use some improvement, but the point is that:

  1. 循环只从 1 到 18 计数.
  2. 主体为 i i + 18 执行输出.
  1. The loop only counts from 1 to 18.
  2. The body performs the output for i and for i + 18.

这篇关于以两列显示数据 c++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆