打印网格

时间:2018-02-08 03:29:18

标签: c++

我想打印一个游戏板网格,它的大小会根据用户的输入而变化。我也想知道使用数组制作同一个网格是最好还是可能,或者我需要某种类型的某种2D数组吗?

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 << "-";
    }
  }
}

我得到了嵌套循环部分。只是有一个问题,告诉它打印一个新的行,并逐个产生。谢谢 Here is the final product I am going for

5 个答案:

答案 0 :(得分:2)

  

我得到了嵌套循环部分。只是有一个问题,告诉它打印一个新行并逐个产生。

你确定吗?这似乎不会产生任何接近最终产品的东西

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

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

要使用列标题,分隔符,行标签,页脚分隔符和页脚完成构建输出,只需逐个拍摄即可。例如,对于列标题,您只需循环遍历列,以适当的间距输出loop counter + 1

    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))分别处理第一列间距)

对于标题分隔符,您可以以相同的方式循环遍历列,使用相同的检查处理第一列的方式不同。

    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 */

几乎就是这样。您可以使用class board将一个简短示例放在一起,以保存rowscols值以及一个构造函数和一些成员函数来更新或请求新行/列值的输入,例如以下内容仅输出您的4x6网格,然后提示输入新的rowscols值,最后输出5x7示例:

#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或更少,或者您将用完大写字母。。这些检查和多位数的调整标题,留给你)

示例使用/输出

$ ./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

仔细看看,如果您有其他问题,请告诉我。

答案 1 :(得分:1)

通过在 userRows for循环完成整个过程后添加cout << endl;,它将继续下一行,直到i < userColumns

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

    for(int y = 0; y < userRows; y++){
      cout << "-";
    }
    cout << endl;  //end of every-line
}

答案 2 :(得分:0)

使用cout << endl;行,您希望它在哪里创建新行。

答案 3 :(得分:0)

当你说:

  

使用数组

制作相同的网格

我猜你在问如何处理可用于填充网格的值。有很多方法可以存储可变数量的值。以下是一些(我假设值类型为char):

  1. 一维std::vector

    std::vector<char> gridValues;
    gridValues.resize(userColumns * userRows);
    

    然后,在for循环中:

    cout << gridValues.at( i + y*userColumns );
    
  2. 二维std::vector

    std::vector<std::vector<char>> gridValues;
    gridValues.resize(userRows);
    for( int i = 0; i < userRows; ++i )
        gridValues.at(i).resize(userColumns);
    

    然后,在for循环中:

    cout << gridValues.at( y ).at( i );
    
  3. 一维数组

    char* gridValues = new char[ userColumns*userRows ];
    

    然后,在for循环中:

    cout << gridValues[ i + y*userColumns ];
    
  4. 二维数组

    char** gridValues = new char[ userRows ];
    for( int i = 0; i < userRows; ++i )
        gridValues[i] = new char[ userColumns ];
    

    然后,在for循环中:

    cout << gridValues[ y ][ i ];
    
  5. 我会使用一维std::vector,因为它设置简单,我不必担心忘记删除最后的指针。

    作为备注,您可能想要检查for循环。使用userColumns = 3userRows = 2,您提供的代码将打印:|--|--|--。这些变量正在向后使用。

    一旦你修复了循环的顺序,JaxLee的答案将帮助你把换行符放在正确的位置。

答案 4 :(得分:0)

问题相当容易,只需了解模式!这是带有实例的工作代码。

#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;
}

Live Code

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