首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >打印栅格

打印栅格
EN

Stack Overflow用户
提问于 2018-02-08 03:29:18
回答 4查看 8.2K关注 0票数 0

我想打印一个棋盘网格,它的大小将取决于用户的输入。我还想知道,使用数组制作这个网格是最好的,甚至是可能的,还是需要某种类型的2d数组?

代码语言:javascript
复制
void printGrid(int &userRows, int &userColumns){
  cout << "Enter the number of rows -> ";
  cin >> userRows;
  cout << "Enter the number of columns -> ";
  cin >> userColumns;

  for(int i = 0; i < userColumns; i++){
    cout << "|";

    for(int y = 0; y < userRows; y++){
      cout << "-";
    }
  }
}

我把嵌套的循环部分拍下来了。只是有一个问题,告诉它打印一个新的行,并产生一个接一个。谢谢这是我想要的最终产品

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2018-02-08 05:32:40

问题很容易解决,只要了解模式就行了!下面是带有实例化的工作代码。

代码语言:javascript
复制
#include <iostream>
using namespace std;

void printGrid(int &userRows, int &userColumns){
  cout<<endl;
  cout<<" ";
  int i=1,j;
  for(j = 0; j <= 4*userColumns; j++){
    if(j%4==2)
        cout<<i++;
    else cout<<" ";
  }
  cout<<endl;
  for(i = 0; i <= 2*userRows; i++){
    if(i%2!=0)
      cout<<(char)(i/2 +'A');
    for(j = 0; j <= 2*userColumns; j++){
      if(i%2==0)
      {
        if(j==0)
            cout<<" ";
        if(j%2==0)
            cout<<" ";
        else cout<<"---";
      }
      else{
        if(j%2==0)
            cout<<"|";
        else cout<<"   ";
      }
    }
    if(i%2!=0)
      cout<<(char)(i/2 +'A');
    cout<<endl;
  }
  cout<<" ";
  for(j = 0, i = 1; j <= 4*userColumns; j++){
    if(j%4==2)
        cout<<i++;
    else cout<<" ";
  }
  cout<<endl;
}

int main() {
  int userRows, userColumns;
  cout << "Enter the number of rows -> ";   
  cin >> userRows;   
  cout << "Enter the number of columns -> ";
  cin >> userColumns; 
  printGrid(userRows, userColumns);
  return 0;
}

活码

代码语言:javascript
复制
Output:

Enter the number of rows -> 4
Enter the number of columns -> 6
   1   2   3   4   5   6  
  --- --- --- --- --- --- 
A|   |   |   |   |   |   |A
  --- --- --- --- --- --- 
B|   |   |   |   |   |   |B
  --- --- --- --- --- --- 
C|   |   |   |   |   |   |C
  --- --- --- --- --- --- 
D|   |   |   |   |   |   |D
  --- --- --- --- --- --- 
   1   2   3   4   5   6  
票数 1
EN

Stack Overflow用户

发布于 2018-02-08 04:43:54

,我把嵌套的循环部分放下来了。只是有一个问题告诉它打印一个新行,然后一个接一个地生成。

真的吗?这似乎不会产生任何接近最终产品的产品,我想要的是

虽然这可能会产生"|""-"输出,但它忽略了列标题、页眉分隔符、行标签、页脚分隔符和列页脚。

在回答问题的newline部分时,您有两个选项,既可以输出带有宏endl的换行符,也可以输出文字换行符"\n"。如果声明了using namespace std;,则只需使用cout << endl;cout << "\n";即可。否则,您需要显式指定std名称空间,例如std::cout << endl;std::cout << "\n";

要用列标题、分隔符、行标签、页脚分隔符和页脚创建输出,只需逐个进行。例如,对于列标题,您可以简单地遍历列,用适当的间距输出loop counter + 1

代码语言:javascript
复制
    for (int i = 0; i < cols; i++)      /* output column headings */
        if (!i)
            std::cout <<"    " << i+1;
        else
            std::cout <<"   " << i+1;
    std::cout << "\n";

(注意事项:使用if (!i) (等效于if (i == 0))分别处理第一列间距)

对于标题分隔符,可以以相同的方式再次遍历列,以相同的方式以不同的方式处理第一列。

代码语言:javascript
复制
    for (int i = 0; i < cols; i++)      /* output header separators */
        if (!i)
            std::cout <<"   ---";
        else
            std::cout <<" ---";
    std::cout << "\n";

然后,实际的网格部分需要在网格的每一行输出之前和之后的行标签。在这里,您只需添加一个嵌套循环来循环每一行,但在每一列上使用类似的循环,并进行第一行检查,然后为每一行最后输出行标签:

代码语言:javascript
复制
    for (int i = 0; i < rows; i++) {    /* output labeled grid rows */
        for (int j = 0; j < cols; j++)
            if (!j)
                std::cout << (char)('A' + i) << " |   |";
            else
                std::cout << "   |";
        std::cout << " " << (char)('A' + i) << "\n";
    }

最后,您只需对页脚分隔符和页脚按相反顺序重复对列标题和页眉分隔符所做的操作,首先输出分隔符行,然后是列页脚。

代码语言:javascript
复制
    for (int i = 0; i < cols; i++)      /* output footer separators */
        if (!i)
            std::cout <<"   ---";
        else
            std::cout <<" ---";
    std::cout << "\n";
    for (int i = 0; i < cols; i++)      /* output column footer */
        if (!i)
            std::cout <<"    " << i+1;
        else
            std::cout <<"   " << i+1;
    std::cout << "\n";                  /* tidy up with new line */

差不多就是这样。您可以使用class board来保存rowscols值,以及构造函数和两个成员函数来更新或请求新行/列值的输入,例如,下面只是输出4x6网格,然后提示输入新的rowscols值,最后输出一个5x7示例:

代码语言:javascript
复制
#include <iostream>

class board {
    int rows, cols;
  public:
    board() {};
    board (int x, int y) { rows = x; cols = y; }
    void prngrid ();
    void setsize (int x, int y) { rows = x; cols = y; }
    void setsize ();
};

void board::prngrid ()
{
    std::cout << "\n";                  /* output new line before grid */
    for (int i = 0; i < cols; i++)      /* output column headings */
        if (!i)
            std::cout <<"    " << i+1;
        else
            std::cout <<"   " << i+1;
    std::cout << "\n";
    for (int i = 0; i < cols; i++)      /* output header separators */
        if (!i)
            std::cout <<"   ---";
        else
            std::cout <<" ---";
    std::cout << "\n";
    for (int i = 0; i < rows; i++) {    /* output labeled grid rows */
        for (int j = 0; j < cols; j++)
            if (!j)
                std::cout << (char)('A' + i) << " |   |";
            else
                std::cout << "   |";
        std::cout << " " << (char)('A' + i) << "\n";
    }
    for (int i = 0; i < cols; i++)      /* output footer separators */
        if (!i)
            std::cout <<"   ---";
        else
            std::cout <<" ---";
    std::cout << "\n";
    for (int i = 0; i < cols; i++)      /* output column footer */
        if (!i)
            std::cout <<"    " << i+1;
        else
            std::cout <<"   " << i+1;
    std::cout << "\n";                  /* tidy up with new line */
}

void board::setsize ()
{
    std::cout << "\nenter the number of rows -> ";
    std::cin >> rows;
    std::cout << "enter the number of cols -> ";
    std::cin >> cols;
}

int main (void) {

    board board1 (4, 6);
    board1.prngrid();

    board1.setsize();
    board1.prngrid();

    board1.setsize (5,7);
    board1.prngrid();

    return 0;
}

(注意:您应该添加rowscols值为非负值的验证检查(或选择unsigned类型),并检查它们对于屏幕输出是否合理(例如,小于20或至少26或更少,否则大写字母将用完)。这些检查,以及多位数标题的调整,由你自己决定)

示例使用/输出

代码语言:javascript
复制
$ ./bin/board_grid

    1   2   3   4   5   6
   --- --- --- --- --- ---
A |   |   |   |   |   |   | A
B |   |   |   |   |   |   | B
C |   |   |   |   |   |   | C
D |   |   |   |   |   |   | D
   --- --- --- --- --- ---
    1   2   3   4   5   6

enter the number of rows -> 5
enter the number of cols -> 5

    1   2   3   4   5
   --- --- --- --- ---
A |   |   |   |   |   | A
B |   |   |   |   |   | B
C |   |   |   |   |   | C
D |   |   |   |   |   | D
E |   |   |   |   |   | E
   --- --- --- --- ---
    1   2   3   4   5

    1   2   3   4   5   6   7
   --- --- --- --- --- --- ---
A |   |   |   |   |   |   |   | A
B |   |   |   |   |   |   |   | B
C |   |   |   |   |   |   |   | C
D |   |   |   |   |   |   |   | D
E |   |   |   |   |   |   |   | E
   --- --- --- --- --- --- ---
    1   2   3   4   5   6   7

仔细考虑一下,如果你还有其他问题,请告诉我。

票数 2
EN

Stack Overflow用户

发布于 2018-02-08 03:36:56

通过在userRows for-循环完成后添加,它将继续执行下一行,直到i < userColumns为止。

代码语言:javascript
复制
for(int i = 0; i < userColumns; i++){
    cout << "|";

    for(int y = 0; y < userRows; y++){
      cout << "-";
    }
    cout << endl;  //end of every-line
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48677066

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档