涉及两位数字时正确显示网格图?
Correctly display grid map when two digits involve?
我有以下代码:
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
int length, height;
int counter = 0;
int counter1 = 0;
cout << "enter length : ";
cin >> length;
cout << "enter height : ";
cin >> height;
for (int row = 0; row <= height+3; row++)
{
for(int col = 0; col <= length+3; col++)
{
if ((col == 0) && (row == 0) || (col == 0) && (row == height+2))
cout << "h ";
else if ((col == 0) && row >= 1 && row < height+3)
cout << counter++ << " ";
else if ((col == 1) && (row == height+3) || (col == length+3) && (row == height+3))
cout << "q ";
else if ((col >= 2) && row == height+3)
cout << counter1++ << " ";
else if ((row == 0) || (row == (height+2)) || (col == 1) || (col == (length+3)))
cout << "x ";
else
cout << " ";
}
cout << endl;
}
}
当我插入长度和高度为20时,由于2位数的值,网格地图无法正常显示。结果变成这样:
enter length : 13
enter height : 13
h x x x x x x x x x x x x x x x x
0 x x
1 x x
2 x x
3 x x
4 x x
5 x x
6 x x
7 x x
8 x x
9 x x
10 x x
11 x x
12 x x
13 x x
h x x x x x x x x x x x x x x x x
q 0 1 2 3 4 5 6 7 8 9 10 11 12 13 q
预期结果应该是:
enter length : 13
enter height : 13
h x x x x x x x x x x x x x x x x
0 x x
1 x x
2 x x
3 x x
4 x x
5 x x
6 x x
7 x x
8 x x
9 x x
10 x x
11 x x
12 x x
13 x x
h x x x x x x x x x x x x x x x x
q 0 1 2 3 4 5 6 7 8 9 10 11 12 13 q
我不确定如何处理 2 位数字输入。如果有人可以就此事提出建议,将不胜感激。谢谢!
可以使用像std::setw()
and std::left
from the header <iomanip>
这样的操纵符,如下
#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;
int main()
{
int length, height;
int counter = 0;
int counter1 = 0;
cout << "enter length : ";
cin >> length;
cout << "enter height : ";
cin >> height;
for (int row = 0; row <= height+3; row++)
{
for(int col = 0; col <= length+3; col++)
{
if ((col == 0) && (row == 0) || (col == 0) && (row == height+2))
cout << "h ";
else if ((col == 0) && row >= 1 && row < height+3)
cout << setw(3)<< left << counter++;
else if ((col == 1) && (row == height+3) || (col == length+3) && (row == height+3))
cout << "q ";
else if ((col >= 2) && row == height+3)
cout << setw(3) << left << counter1++;
else if ((row == 0) || (row == (height+2)) || (col == 1) || (col == (length+3)))
cout << setw(3) << left << "x";
else
cout << " ";
}
cout << endl;
}
}
另一件事Why is "using namespace std;" considered bad practice?
我有以下代码:
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
int length, height;
int counter = 0;
int counter1 = 0;
cout << "enter length : ";
cin >> length;
cout << "enter height : ";
cin >> height;
for (int row = 0; row <= height+3; row++)
{
for(int col = 0; col <= length+3; col++)
{
if ((col == 0) && (row == 0) || (col == 0) && (row == height+2))
cout << "h ";
else if ((col == 0) && row >= 1 && row < height+3)
cout << counter++ << " ";
else if ((col == 1) && (row == height+3) || (col == length+3) && (row == height+3))
cout << "q ";
else if ((col >= 2) && row == height+3)
cout << counter1++ << " ";
else if ((row == 0) || (row == (height+2)) || (col == 1) || (col == (length+3)))
cout << "x ";
else
cout << " ";
}
cout << endl;
}
}
当我插入长度和高度为20时,由于2位数的值,网格地图无法正常显示。结果变成这样:
enter length : 13
enter height : 13
h x x x x x x x x x x x x x x x x
0 x x
1 x x
2 x x
3 x x
4 x x
5 x x
6 x x
7 x x
8 x x
9 x x
10 x x
11 x x
12 x x
13 x x
h x x x x x x x x x x x x x x x x
q 0 1 2 3 4 5 6 7 8 9 10 11 12 13 q
预期结果应该是:
enter length : 13
enter height : 13
h x x x x x x x x x x x x x x x x
0 x x
1 x x
2 x x
3 x x
4 x x
5 x x
6 x x
7 x x
8 x x
9 x x
10 x x
11 x x
12 x x
13 x x
h x x x x x x x x x x x x x x x x
q 0 1 2 3 4 5 6 7 8 9 10 11 12 13 q
我不确定如何处理 2 位数字输入。如果有人可以就此事提出建议,将不胜感激。谢谢!
可以使用像std::setw()
and std::left
from the header <iomanip>
这样的操纵符,如下
#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;
int main()
{
int length, height;
int counter = 0;
int counter1 = 0;
cout << "enter length : ";
cin >> length;
cout << "enter height : ";
cin >> height;
for (int row = 0; row <= height+3; row++)
{
for(int col = 0; col <= length+3; col++)
{
if ((col == 0) && (row == 0) || (col == 0) && (row == height+2))
cout << "h ";
else if ((col == 0) && row >= 1 && row < height+3)
cout << setw(3)<< left << counter++;
else if ((col == 1) && (row == height+3) || (col == length+3) && (row == height+3))
cout << "q ";
else if ((col >= 2) && row == height+3)
cout << setw(3) << left << counter1++;
else if ((row == 0) || (row == (height+2)) || (col == 1) || (col == (length+3)))
cout << setw(3) << left << "x";
else
cout << " ";
}
cout << endl;
}
}
另一件事Why is "using namespace std;" considered bad practice?