如何水平打印更多块?

时间:2014-10-18 12:02:21

标签: c++

#include <iostream>

using namespace std;

void TimesTable(int count)
{
    for (int j = 1; j < 13; j++)
    {
        cout << count << " x " << j << " = " << count*j << " " << endl;
    }
}

int main()
{
    for (int count = 1; count < 13; count++)
    {
        TimesTable(count);
        cout << endl;
    }

        system("Pause");
}

打印

"1 x 1 = 1
 1 x 2 = 2

 2x1 = 2
 2x2 = 4

 3x1 = 3
 3x2 = 6"

等等。但我想做的事情就是这样,而不是让它们全部向下打印,我想把它放在块中,所以,

 "1 x 1 = 1   2 x 1 = 2   3 x 1  = 3  4 x  1 = 4  5 x 1  = 5
  1 x 2  = 2  2 x 2 = 4   3 x 2 = 6   4 x 2 = 8   5 x 2 = 10"

等等。但显然它会更好看,如果它没有水平转到12或者你必须抓住控制台窗口底部的栏并拖动以查看其他时间稳定。我喜欢它,所以它在块1号线上变为5,然后在第2行块上变为6到10,最后一行变为10到12。

是的它是随机的,它只能达到12但这是一个练习,我只是将它改编成其他怪物。

我想我可能在for循环中只有一个以上的cout但是我发誓过去我曾经看过有人用一种聪明的方式来传递一个变量,这样它就会打印出一个星号块而不是假装去做。

期望的结果(使用符号更容易):

" ***** ^^^^^ &&&&&
  ***** ^^^^^ &&&&&
  ***** ^^^^^ &&&&& "

截至目前的结果:

" *****
  *****
  *****

  ^^^^^
  ^^^^^
  ^^^^^

  &&&&&
  &&&&&
  &&&&& "

2 个答案:

答案 0 :(得分:2)

要格式化列,请使用<iomanip>

中的std::setw
  

设置要在输出操作中使用的字段宽度。

现在。

外面,迭代线。

在里面,迭代列。

CODE

#include <iostream>
#include <iomanip>

using namespace std;

int main()
{
    int cols_per_line = 5;
    for (int line = 1; line < 13; line++)
    {
        for (int col = 1; col <= cols_per_line; col++)
        {
            cout << setw(2) << col << " x " << setw(2) << line << " = " << setw(3) << line*col << "   ";
        }
        cout << endl;
    }
}

输出

[root@rnrlabs ~]# ./a.out
 1 x  1 =   1    2 x  1 =   2    3 x  1 =   3    4 x  1 =   4    5 x  1 =   5
 1 x  2 =   2    2 x  2 =   4    3 x  2 =   6    4 x  2 =   8    5 x  2 =  10
 1 x  3 =   3    2 x  3 =   6    3 x  3 =   9    4 x  3 =  12    5 x  3 =  15
 1 x  4 =   4    2 x  4 =   8    3 x  4 =  12    4 x  4 =  16    5 x  4 =  20
 1 x  5 =   5    2 x  5 =  10    3 x  5 =  15    4 x  5 =  20    5 x  5 =  25
 1 x  6 =   6    2 x  6 =  12    3 x  6 =  18    4 x  6 =  24    5 x  6 =  30
 1 x  7 =   7    2 x  7 =  14    3 x  7 =  21    4 x  7 =  28    5 x  7 =  35
 1 x  8 =   8    2 x  8 =  16    3 x  8 =  24    4 x  8 =  32    5 x  8 =  40
 1 x  9 =   9    2 x  9 =  18    3 x  9 =  27    4 x  9 =  36    5 x  9 =  45
 1 x 10 =  10    2 x 10 =  20    3 x 10 =  30    4 x 10 =  40    5 x 10 =  50
 1 x 11 =  11    2 x 11 =  22    3 x 11 =  33    4 x 11 =  44    5 x 11 =  55
 1 x 12 =  12    2 x 12 =  24    3 x 12 =  36    4 x 12 =  48    5 x 12 =  60

答案 1 :(得分:0)

只需删除TimeTable末尾的endl为内部&#34;为&#34; 并为拆分器添加另一个计数器:

编辑:用计数更改j。

 int splitter = 0;
 for (int j = 1; j < 13; j++, splitter++)
 {
    cout << j << " x " << count  << " = " << count*j << "\t";
    if (splitter == 5)
    {
       cout << endl;
       splitter=-1;
    }
 }


1 x 1 = 1   2 x 1 = 2   3 x 1 = 3   4 x 1 = 4   5 x 1 = 5   6 x 1 = 6   
7 x 1 = 7   8 x 1 = 8   9 x 1 = 9   10 x 1 = 10 11 x 1 = 11 12 x 1 = 12 

1 x 2 = 2   2 x 2 = 4   3 x 2 = 6   4 x 2 = 8   5 x 2 = 10  6 x 2 = 12  
7 x 2 = 14  8 x 2 = 16  9 x 2 = 18  10 x 2 = 20 11 x 2 = 22 12 x 2 = 24 

1 x 3 = 3   2 x 3 = 6   3 x 3 = 9   4 x 3 = 12  5 x 3 = 15  6 x 3 = 18  
7 x 3 = 21  8 x 3 = 24  9 x 3 = 27  10 x 3 = 30 11 x 3 = 33 12 x 3 = 36 

1 x 4 = 4   2 x 4 = 8   3 x 4 = 12  4 x 4 = 16  5 x 4 = 20  6 x 4 = 24  
7 x 4 = 28  8 x 4 = 32  9 x 4 = 36  10 x 4 = 40 11 x 4 = 44 12 x 4 = 48 

1 x 5 = 5   2 x 5 = 10  3 x 5 = 15  4 x 5 = 20  5 x 5 = 25  6 x 5 = 30  
7 x 5 = 35  8 x 5 = 40  9 x 5 = 45  10 x 5 = 50 11 x 5 = 55 12 x 5 = 60 

1 x 6 = 6   2 x 6 = 12  3 x 6 = 18  4 x 6 = 24  5 x 6 = 30  6 x 6 = 36  
7 x 6 = 42  8 x 6 = 48  9 x 6 = 54  10 x 6 = 60 11 x 6 = 66 12 x 6 = 72 

1 x 7 = 7   2 x 7 = 14  3 x 7 = 21  4 x 7 = 28  5 x 7 = 35  6 x 7 = 42  
7 x 7 = 49  8 x 7 = 56  9 x 7 = 63  10 x 7 = 70 11 x 7 = 77 12 x 7 = 84 

1 x 8 = 8   2 x 8 = 16  3 x 8 = 24  4 x 8 = 32  5 x 8 = 40  6 x 8 = 48  
7 x 8 = 56  8 x 8 = 64  9 x 8 = 72  10 x 8 = 80 11 x 8 = 88 12 x 8 = 96 

1 x 9 = 9   2 x 9 = 18  3 x 9 = 27  4 x 9 = 36  5 x 9 = 45  6 x 9 = 54  
7 x 9 = 63  8 x 9 = 72  9 x 9 = 81  10 x 9 = 90 11 x 9 = 99 12 x 9 = 108    

1 x 10 = 10 2 x 10 = 20 3 x 10 = 30 4 x 10 = 40 5 x 10 = 50 6 x 10 = 60 
7 x 10 = 70 8 x 10 = 80 9 x 10 = 90 10 x 10 = 100   11 x 10 = 110   12 x 10 = 120   

1 x 11 = 11 2 x 11 = 22 3 x 11 = 33 4 x 11 = 44 5 x 11 = 55 6 x 11 = 66 
7 x 11 = 77 8 x 11 = 88 9 x 11 = 99 10 x 11 = 110   11 x 11 = 121   12 x 11 = 132   

1 x 12 = 12 2 x 12 = 24 3 x 12 = 36 4 x 12 = 48 5 x 12 = 60 6 x 12 = 72 
7 x 12 = 84 8 x 12 = 96 9 x 12 = 108    10 x 12 = 120   11 x 12 = 132   12 x 12 = 144