gpt4 book ai didi

c++ - 打印网格

转载 作者:行者123 更新时间:2023-11-27 23:48:27 26 4
gpt4 key购买 nike

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

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

最佳答案

I got the nested loop part down pat. Just having an issue telling it to print a new line and to spawn one after the other.

你确定吗?这似乎没有产生任何接近我想要的最终产品的东西?

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

在回答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 values 和一个构造函数以及几个成员函数来更新或请求输入新的行/列值,例如以下只是输出你的 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

检查一下,如果您还有其他问题,请告诉我。

关于c++ - 打印网格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48677066/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com