【问题标题】:Correctly display grid map when two digits involve?涉及两位数时正确显示网格图?
【发布时间】:2020-07-25 04:46:21
【问题描述】:

我有以下代码:

#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 位输入。如果有人可以就此事提出建议,将不胜感激。谢谢!

【问题讨论】:

    标签: c++ for-loop c++11 if-statement io


    【解决方案1】:

    您可以使用标题 &lt;iomanip&gt; 中的 std::setw()std::left 等操纵器,如下所示

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

    Live

    另一件事Why is "using namespace std;" considered bad practice?

    【讨论】:

      猜你喜欢
      • 2016-08-15
      • 2020-12-08
      • 2021-01-18
      • 2010-11-29
      • 2014-06-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-22
      相关资源
      最近更新 更多