C++ 如何仅向二维数组中的第一列和最后一行添加空格字符?
我可能有个简单的问题, 这就是我得到的: 我的观点是要有一个表,它在第一列和最后一列以及第一行和最后一行中包含空格字符,在所有其他地方包含随机值,即o或空格字符。 另外,我想在每一行的末尾用|标记来关闭这块板,只是为了让它看起来整洁 在函数generaujpole中有4条线,例如极点[i][0]='';我认为这可以解决问题,但在最后一篇专栏文章中,它不起作用 什么是错误的,应该如何正确书写 守则:C++ 如何仅向二维数组中的第一列和最后一行添加空格字符?,c++,C++,我可能有个简单的问题, 这就是我得到的: 我的观点是要有一个表,它在第一列和最后一列以及第一行和最后一行中包含空格字符,在所有其他地方包含随机值,即o或空格字符。 另外,我想在每一行的末尾用|标记来关闭这块板,只是为了让它看起来整洁 在函数generaujpole中有4条线,例如极点[i][0]='';我认为这可以解决问题,但在最后一篇专栏文章中,它不起作用 什么是错误的,应该如何正确书写 守则: #include <iostream> #include <ctime>
#include <iostream>
#include <ctime>
using namespace std;
const int width = 62;
const int height = 22;
void generujpole(char pole[][height])
{
for(int i=0; i<width; i++)
{
for(int j=0; j<height; j++)
{
pole[i][0] = ' ';
pole[i][61]= ' ';
pole[0][j] = ' ';
pole[21][j] = ' ';
int maluj = rand()%100;
if(maluj < 90) pole[i][j] = ' ';
else pole[i][j] = 'o';
}
}
}
void wypiszpole(char pole[][height])
{
cout <<endl;
cout<< " GAME OF LIFE";
cout << endl << endl;
cout << "+------------------------------------------------------------+"
<<endl;
for(int i=0; i<height; i++)
{
cout << "|";
for(int j=0; j<width; j++)
{
cout << pole[i][j];
}
cout << "|" << endl;
}
cout << "+------------------------------------------------------------+"<<endl;
cout << endl;
cout << " R - reset, Q - quit, K - step " << endl;
}
您可以在顶部和底部栏中添加两个“-”,例如:
cout << "+--------------------------------------------------------------+"
几个示例维度的输出:
人生游戏
+-------+
|o o|
|o|
| |
|o o|
+-------+
R-复位,Q-退出,K-步
人生游戏
+---------------------+
|欧欧欧欧|
|欧欧欧欧|
|欧欧欧欧|
|欧欧欧|
|欧欧欧欧|
|欧欧欧欧|
|o o|
|欧欧欧欧欧|
|欧欧欧欧|
|哦哦|
|o o o o o o o o|
|噢|
|哦哦哦|
|欧欧欧欧|
|o|
+---------------------+
R-复位,Q-退出,K-步
人生游戏
+-----------------------------------------+
|o o o o o o o o o|
|o o o o o o o o o|
|欧欧欧欧欧欧欧欧欧|
|欧欧欧欧欧欧欧欧|
|欧欧欧欧欧欧欧欧欧欧欧欧欧欧欧欧欧欧欧欧欧欧欧欧欧欧欧欧欧欧欧欧欧欧欧欧欧欧欧欧欧欧欧欧欧欧欧欧欧欧欧欧欧欧欧欧欧欧欧欧|
|欧欧欧欧欧欧欧欧欧欧欧欧欧|
|oo o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o|
|欧欧欧欧欧欧欧欧|
|o o o o o o o o o|
|欧欧欧欧欧|
|欧欧欧欧欧|
|欧欧欧欧欧欧欧欧欧欧欧欧欧欧欧欧欧欧欧欧欧欧欧欧欧欧欧欧欧欧欧欧欧欧欧欧欧欧欧欧欧欧欧欧欧欧欧欧欧欧欧欧欧欧欧欧欧欧欧欧|
|o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o|
|哦哦哦|
|欧欧欧欧欧|
|欧欧欧欧欧|
|o o o o o o o o o o o o|
|欧欧欧欧欧欧欧欧欧欧欧欧欧欧欧欧欧欧欧欧欧欧欧欧欧欧欧欧欧欧欧欧欧欧欧欧欧欧欧欧欧欧欧欧欧欧欧欧欧欧欧欧欧欧欧欧欧欧欧欧欧欧|
|欧欧欧欧欧欧欧欧欧欧欧欧欧欧欧欧欧欧欧欧欧欧欧欧欧欧欧欧欧欧欧欧欧欧欧欧欧欧欧欧欧欧欧欧欧欧欧欧欧欧欧欧欧欧欧欧欧欧欧欧|
|欧欧欧欧欧欧欧欧欧欧欧欧欧欧欧欧欧欧欧欧欧欧欧欧欧欧欧欧欧欧欧欧欧欧欧欧欧欧欧欧欧欧欧欧欧欧欧欧欧欧欧欧欧欧欧欧欧欧|
|欧欧欧欧欧欧欧欧欧欧欧|
|欧欧欧欧欧欧欧欧欧欧欧欧欧欧欧欧欧欧欧欧欧欧欧欧欧欧欧欧欧欧欧欧欧欧欧欧欧欧欧欧欧欧欧欧欧欧欧欧欧欧欧欧欧欧欧欧欧欧欧|
|欧欧欧欧|
|o o o o o o o o o o o o|
|o o o o o o o o|
+-----------------------------------------+
R-复位,Q-退出,K-步
@obeey:在函数generaujpol中,必须将for loo拆分为两个单独的循环,如下所示:
// We fill the first and last rows with 'space' character
for(int i=0; i < HEIGHT; i++)
{
pole[0][i] = ' ';
pole[width-1][i] = ' ';
}
//Now, we fill the first and last column with 'space character
for(int i=0; i < width; i++)
{
pole[i][0] = ' ';
pole[i][height-1] = ' ';
}
// Create a string of length 'height' filled with '-'
string line(height, '-');
// then add '+' at the back and front
line = "+" + line + "+";
// now you can comfortably write.
cout << line << endl;
用“+--+”填充一行的适当方法是按以下步骤进行:
// We fill the first and last rows with 'space' character
for(int i=0; i < HEIGHT; i++)
{
pole[0][i] = ' ';
pole[width-1][i] = ' ';
}
//Now, we fill the first and last column with 'space character
for(int i=0; i < width; i++)
{
pole[i][0] = ' ';
pole[i][height-1] = ' ';
}
// Create a string of length 'height' filled with '-'
string line(height, '-');
// then add '+' at the back and front
line = "+" + line + "+";
// now you can comfortably write.
cout << line << endl;
请不要发布指向外部资源的链接。它们可能会在将来消失,使资源无法访问。谢谢,但最后一列仍然不是空的。你知道还有什么问题吗?看看我的更新,如果我遗漏了任何其他问题,请告诉我。非常感谢你的回答。这真的很有帮助,它简化了我已经拥有的东西。问题是我还没有使用指针和动态数组,所以这对我来说是相当混乱的。有没有办法不用这个?再次感谢。当然,你可以对尺寸进行硬编码,使用我答案的第一部分和打印程序中添加的空格,并在将来记住动态内存。如果需要,请随意接受答案
它解决了你的问题。谢谢!这是一个简单的解决方案,非常有用。不幸的是,添加两个for循环没有帮助,或者我做得不对。你能扩展这个想法吗?我应该如何将这些循环与随机值连接起来?更新:我在生成随机值的循环之后,在函数generaujpole中添加了这两个循环,现在它可以完美地工作了。
// Create a string of length 'height' filled with '-'
string line(height, '-');
// then add '+' at the back and front
line = "+" + line + "+";
// now you can comfortably write.
cout << line << endl;