【问题标题】:multi dimensional arrays, type and format多维数组、类型和格式
【发布时间】:2013-12-10 00:32:50
【问题描述】:

下面我有一个我写的多维数组,它输出一个 8x8 块并显示国际象棋游戏中所有棋子的起始位置。

我想知道两个问题:

  1. 如何将数字 1 到 8 从两侧相加,字母 A-H 从左到右相加?

  2. 当打印到控制台时,数组中所有未填充的空格保持空白。如果我想手动移动一个棋子说皇后典当一个,我怎样才能再次显示屏幕但这次将典当的原始位置保留为空白,就像棋盘的其余部分一样?

感谢您的宝贵时间,

标记

    #include <iostream>
    #include <string>
    #include<iomanip>

    using namespace std;

    int main()
    {
        string chess[8][8];

         for (int j=0;j<8;j++)
         {
           chess [1][j]= "P";
           chess [6][j]= "P";
           chess [0][0]="R";
           chess [7][0]="R";
           chess [0][7]="R";
           chess [7][7]="R";
           chess [0][1]="Kn";
           chess [7][1]="Kn";
           chess [0][6]="Kn";
           chess [7][6]="Kn"; 
           chess [0][2]="B";
           chess [7][2]="B";
           chess [0][5]="B";
           chess [7][5]="B"; 
           chess [0][3]="Q";
           chess [7][3]="Q";
           chess [0][4]="Ki";
           chess [7][4]="Ki"; 
        }  
        for (int a=0;a<8;a++)
        {
                for (int b=0;b<8;b++)
                {
                   cout<<setw(4)<<chess[a][b];
                }
            cout<<endl;
        }       
        system ("pause");
    return 0;
    }    

【问题讨论】:

  • chess 第 3-6 行尚未初始化。 C/C++ 不会自动在堆栈变量中放置空格。你必须这样做。
  • 我相信 C++ 会调用堆栈变量的默认构造函数,即使在数组中也是如此。 std::string(相对于 POD 类型)很可能在字符串 chess[8][8] 中正确初始化;

标签: c++ types multidimensional-array


【解决方案1】:

首先,将初始化代码和显示代码移出main 并放入两个单独的函数中。 (请注意,初始化不需要第二个循环和所有那些if 语句;它只是对数组元素进行赋值,并且不需要所有额外的东西)。然后,要移动皇后的棋子,只需执行swap(chess[1][3], chess[2][3]) 并再次调用display()

要显示排名和文件标记,只需在开始绘制棋盘之前写一行文件标记即可。排名标记应写在显示代码的外部for 循环内。

【讨论】:

    【解决方案2】:

    这是我在使用 Nyrl 的建议后提出的问题第一部分的答案(根据 Pete Becker 的建议编辑了原始帖子)

        #include <iostream>
        #include <string>
        #include<iomanip>
    
        using namespace std;
    
        int main()
        {
            string chess[8][8];
    
             for (int j=0;j<8;j++)
             {
               chess [1][j]= "P";
               chess [6][j]= "P";
               chess [0][0]="R";
               chess [7][0]="R";
               chess [0][7]="R";
               chess [7][7]="R";
               chess [0][1]="Kn";
               chess [7][1]="Kn";
               chess [0][6]="Kn";
               chess [7][6]="Kn"; 
               chess [0][2]="B";
               chess [7][2]="B";
               chess [0][5]="B";
               chess [7][5]="B"; 
               chess [0][3]="Q";
               chess [7][3]="Q";
               chess [0][4]="Ki";
               chess [7][4]="Ki"; 
            }  
    //below is the line to add the Letters for each column manually at the top.
    
            cout<<setw(4)<<" "<<setw(4)<<'A'<<setw(4)<<'B'<<setw(4)<<'C'<<setw(4)<<'D'<<setw(4)<<'E'<<setw(4)<<'F'<<setw(4)<<'G'<<setw(4)<<'H'<<setw(4)<<endl;
            cout<<endl;
            for (int a=0;a<8;a++)
            {            
                cout<<setw(4)<<a+1; `//adds the numbers before each row`
                for (int b=0;b<8;b++)
                    {
                      cout<<setw(4)<<chess[a][b];
                    }
                cout<<setw(4)<<a+1; `//adds the numbers after each row`
                cout<<endl;
            } 
    //below is the line to add the Letters for each column manually at the bottom.
            cout<<endl;
            cout<<setw(4)<<" "<<setw(4)<<'A'<<setw(4)<<'B'<<setw(4)<<'C'<<setw(4)<<'D'<<setw(4)<<'E'<<setw(4)<<'F'<<setw(4)<<'G'<<setw(4)<<'H'<<setw(4)<<endl;
    
            system ("pause");
        return 0;
        }    
    

    【讨论】:

      【解决方案3】:

      1) 为列添加额外的循环范围:

      cout << "    ";
      for (int b=0;b<8;b++)
          cout << setw(4) << static_cast<char>('A'+b);
      cout << endl;
      

      为行添加额外的输出:

      for (int a=0;a<8;a++)
      {
          cout << setw(4) << a;
          for (int b=0;b<8;b++)
             cout<<setw(4)<<chess[a][b];
          cout<<endl;
      }       
      

      根据要求,工作示例是:

      #include <iostream>
      #include <iomanip>
      #include <string>
      
      using namespace std;
      string chess[8][8];
      
      int main()
      {
        cout << "    ";
        for (int c=0; c<8; ++c)
          cout << setw(4) << static_cast<char>('A'+c);
        cout << endl;
      
        for (int r=0; r<8; ++r)
        {
          cout << setw(4) << r;
          for (int c=0; c<8; ++c)
            cout << setw(4) << chess[r][c];
          cout << endl;
        }
      }
      

      2) 没有简单的方法,但您可能会幸运地使用 ESC 代码 (http://en.wikipedia.org/wiki/ANSI_escape_code),它允许您在某些终端上打印之前设置列/行。

      【讨论】:

      • 我不完全确定前两个添加会做什么,您是否有机会澄清或展示您希望它的整体外观。谢谢。
      • @Markrwithak 这不是你想要的吗:我怎样才能将数字 1-8 从两侧相加,字母 A-H 从左到右相加?第一个循环在第 1..8 列打印 A..H(在第 0 列留出空间),第二个加法在第 0 列打印数字 1..8。它将打印带有标题和 8x8 数据的 9x9 矩阵。我是不是把你的问题弄错了?
      • 好的,我可以通过添加行“cout
      • @Markrwithak 为什么不看提供的示例代码,即cout &lt;&lt; setw(4) &lt;&lt; ('A'+b);
      • @Markrwithak 值得注意的是,字母(字符)只是另一种整数类型,因为您可以添加/减去它们,甚至可以相乘。它们通常仅在输出时变为字符,即在您的情况下为cout::operator&lt;&lt;()
      猜你喜欢
      • 1970-01-01
      • 2021-05-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-28
      • 1970-01-01
      • 2017-05-12
      • 2018-05-21
      相关资源
      最近更新 更多