2014-10-06 90 views
0

我正在寻找一种方法来输出我正在试图制作的Yahtzee程序中定义的结构的元素。格式:如何水平打印容器?

我的结构是:

struct card_cell 
{ 
    std::string name; 
    int points; 
    int state; 
} 

我想打印格式:

name1 name2 name3... 
    points1 points2 points3... 
    state1 state2 state3... 

在屏幕上像这样。

不同类型的结构存储在std::vector<card_cell>中,所以我可以通过迭代遍历矢量的成员并输出名称,然后指向,然后依次指定状态来完成此操作。

不过,我有足够的card_cell S IN的载体,当我打印出来的所有这样的条目开始自己制作的新线路,并打乱格式:

name1 name2 name3... 
    nameN... 
    points1 points2 points3... 
    pointsN... 
    state1 state2 state3... 
    stateN... 

我希望能够要声明const int CELLS_PER_LINE = 6;之类的东西,并且当我的迭代打印该数字name's时,它将停止,开始一个新行并打印下一个值。当它终于到达states的末尾时,它将形成一条新线并开始印刷下一组names,并在此处停止。

我可以蛮力和硬编码,是的,但我想知道如果有人有一个想法如何以一种可爱的方式做到这一点?

谢谢!

回答

0

首先,您可能想知道终端有多宽,以确保不会溢出。为此,我们可以使用ioctl()TIOCGWINSZ如下所示:https://stackoverflow.com/a/8529354/4323

然后,我们可以使用std::ostream::tellp()知道我们有多少个字符至今写:http://www.cplusplus.com/reference/ostream/ostream/tellp/

全部放在一起,我的计划将是获得窗口宽度,打印一列,检查它的宽度(或者你可能已经知道),并且很好地猜测下一列是否过宽。如果您接近行尾,只是推迟打印该列,直到打印出列中的所有行为止。

0

下面是一些尝试:

void printCells(std::vector<card_cell> const& cells) 
{ 
    // Compute the number of screens you would need to write 
    // the cells. 
    int numScreens = (cells.size()+CELLS_PER_LINE-1)/CELLS_PER_LINE; 

    // Iterators to iterate over all the cells. 
    std::vector<card_cell>::const_iterator iter = cells.begin(); 
    std::vector<card_cell>::const_iterator end = cells.end(); 

    // Loop over the number of screens needed. 
    for (int i = 0; i != numScreens; ++i) 
    { 
     // While printing the cells in a screen we have to make sure that: 
     // 1. We don't print more than CELLS_PER_LINE. 
     // 2. We stop when we are at the end of the cells. 

     int count = 0; 
     std::vector<card_cell>::const_iterator iter2 = iter; 

     // Iterate over the cells for the names and print the names. 
     for (; 
      iter2 != end && count != CELLS_PER_LINE; 
      ++iter2, ++count) 
     { 
     // Figure out how much width to use for each name. 
     // Hard coded to 10 here. 
     std::cout << std::setw(10) << iter2->name; 
     } 
     std::cout << std::endl; 

     // Iterate over the cells for the points and print the points. 
     for (count = 0, iter2 = iter; 
      iter2 != end && count != CELLS_PER_LINE; 
      ++iter2, ++count) 
     { 
     // Figure out how much width to use for each point. 
     // Hard coded to 10 here. 
     std::cout << std::setw(10) << iter2->points; 
     } 
     std::cout << std::endl; 

     // Iterate over the cells for the states and print the states. 
     for (count = 0, iter2 = iter; 
      iter2 != end && count != CELLS_PER_LINE; 
      ++iter2, ++count) 
     { 
     // Figure out how much width to use for each state. 
     // Hard coded to 10 here. 
     std::cout << std::setw(10) << iter2->state; 
     } 
     std::cout << std::endl; 
     std::cout << std::endl; 

     iter = iter2; 
    } 
} 
0

我假设你的屏幕足够长。

  • 首先,获得第一列的最长长度并记住它。为下一个CELLS_PER_LINE - 1元素做同样的事情。
  • 其次,打印第一个CELLS_PER_LINE元素的name。如果某件物品的长度小于您记忆的longest length,请填写空白。然后按照上述规则打印points,state

现在你已经漂亮的印第一CELLS_PER_LINE元素。输出换行符并执行相同的操作。

例如,

约翰爱丽丝添

10.000000 9.98 8.1

好坏好

第一列的最长长度为lengthof( “10.000000”)= 9你应该记住。

所述第二列中的一个是lengthof( “爱丽丝”)= 5。

和第三列中的一个是lengthof( “好”)= 4

然后漂亮地打印他们...

John______Alice_Time

10.000000_9.98__8.1

good______bad___good

这里`_'表示空白。