【问题标题】:Turn 1 C++ Loop into 2将 1 个 C++ 循环变为 2 个
【发布时间】:2010-12-06 03:58:02
【问题描述】:

我需要一点编程帮助来上课。我写了下面的代码,但是我的老师说我在读取数据文件时需要使用 2 个 for 循环。我不知道该怎么做...有什么建议吗?

我的代码:

ifstream infile;
infile.open("dive.txt");
for(int i = 0; i <= 6; i++)
    infile >> contestantNames[i][0] >> contestantNames[i][1] >> judgeScores[i][0] >> judgeScores[i][1] >> judgeScores[i][2] >> judgeScores[i][3] >> judgeScores[i][4] >> judgeScores[i][5] >> judgeScores[i][6] >> judgeScores[i][7];
infile.close();

【问题讨论】:

  • 您的老师可能希望您使用两个嵌套循环,而不是手动从 0 变为 7。
  • 能否请您为 100 位参赛者发布您的示例? (与contestantNames[100][100]

标签: c++ loops for-loop


【解决方案1】:

不要重复写

infile >> contestantNames[0][0] >> contestantNames[0][1] >> judgeScores[0][0] >> judgeScores[0][1] >> judgeScores[0][2] >> judgeScores[0][3] >> judgeScores[0][4] >> judgeScores[0][5] >> judgeScores[0][6] >> judgeScores[0][7];
infile >> contestantNames[1][0] >> contestantNames[1][1] >> judgeScores[1][0] >> judgeScores[1][1] >> judgeScores[1][2] >> judgeScores[1][3] >> judgeScores[1][4] >> judgeScores[1][5] >> judgeScores[1][6] >> judgeScores[1][7];
infile >> contestantNames[2][0] >> contestantNames[2][1] >> judgeScores[2][0] >> judgeScores[2][1] >> judgeScores[2][2] >> judgeScores[2][3] >> judgeScores[2][4] >> judgeScores[2][5] >> judgeScores[2][6] >> judgeScores[2][7];
infile >> contestantNames[3][0] >> contestantNames[3][1] >> judgeScores[3][0] >> judgeScores[3][1] >> judgeScores[3][2] >> judgeScores[3][3] >> judgeScores[3][4] >> judgeScores[3][5] >> judgeScores[3][6] >> judgeScores[3][7];
infile >> contestantNames[4][0] >> contestantNames[4][1] >> judgeScores[4][0] >> judgeScores[4][1] >> judgeScores[4][2] >> judgeScores[4][3] >> judgeScores[4][4] >> judgeScores[4][5] >> judgeScores[4][6] >> judgeScores[4][7];
infile >> contestantNames[5][0] >> contestantNames[5][1] >> judgeScores[5][0] >> judgeScores[5][1] >> judgeScores[5][2] >> judgeScores[5][3] >> judgeScores[5][4] >> judgeScores[5][5] >> judgeScores[5][6] >> judgeScores[5][7];
infile >> contestantNames[6][0] >> contestantNames[6][1] >> judgeScores[6][0] >> judgeScores[6][1] >> judgeScores[6][2] >> judgeScores[6][3] >> judgeScores[6][4] >> judgeScores[6][5] >> judgeScores[6][6] >> judgeScores[6][7];

你写了一个for循环。

哪里还有重复?

【讨论】:

    【解决方案2】:

    你的两个 for 循环是

    for (int i = 0; i <= 6; i++) {
        infile >> contestantNames[i][0] >> contestantNames[i][1];
        for (int j = 0; j <= 7; j++) {
            infile >> judgeScores[i][j];
        }
    }
    

    您的教师希望您将连续的judgeScores 简化为一个更简单、更易于阅读的for 循环。

    【讨论】:

      【解决方案3】:
      ifstream infile;
      infile.open("dive.txt");
      
      for (int i = 0; i <= 6; i++)
      {
        infile >> contestantNames[i][0] >> contestantNames[i][1];
      
        // The next part is what your teacher was talking about.
        // You wrote judgeScores[i][0] >> judgeScores[i][1] >> ...
        // seven times, which is pretty redundant. Programmers are *extremely* lazy
        // so we loop constantly, wherever possible:
      
        for (int j = 0; j <= 7; j++)
        {
           infile >> judgeScores[i][j];
        }
      }
      
      infile.close();
      

      【讨论】:

        【解决方案4】:

        如果有两个以上的参赛者,您的老师可能希望您考虑这种情况。 类似于

        nc = 6;  // the number of contenstans
        for (int c=0; c<nc; c++) {
          // other for loop here...
        }
        

        【讨论】:

          【解决方案5】:

          我认为您的教练的意思是循环遍历所有参赛者,然后再次循环输入每个参赛者的信息(姓名和分数)。

          所以试试这样的:

          ifstream infile;
          infile.open("dive.txt");
          for(int i = 0; i <= 6; i++) {
              infile >> contestantNames[i][0] >> contestantNames[i][1];
              for (int j = 0; i <= 7; j++) {
                   infile >> judgeScores[i][j];
              }
          }
          infile.close();
          

          注意:括号不是必需的,因为每个语句由一行组成。这是规则的例外,但是 - 如果for 循环中有超过 1 个语句,则需要括号。 (你可能知道。)

          【讨论】:

            【解决方案6】:

            连三个循环都让你的老师大吃一惊:

            ifstream infile;
            infile.open("dive.txt");
            for(int i = 0; i <= 6; i++)
            {
                for (int j = 0; j < 2; ++j)
                {
                    infile >> contestantNames[i][j];
                }
            
                for (int j = 0; j < 8; ++j)
                {
                   infile >> judgeScores[i][j];
                }
            }
            infile.close();
            

            【讨论】:

            • 任何以“让你的老师大吃一惊”开头的建议都应该立即发出警告。
            • @Cody Gray:为什么不呢?我的意思是好的。如您所见,此版本是这里唯一的版本,可以更轻松地更改contestants 的数量。如果我是老师,我很想看到这个版本。
            【解决方案7】:

            如果超过 6 行,请使用 while 循环检查文件结尾 (EOF)。不良数据的测试用例也不会受到伤害。

            【讨论】:

              猜你喜欢
              • 2014-11-17
              • 1970-01-01
              • 2020-09-05
              • 1970-01-01
              • 1970-01-01
              • 2015-04-15
              • 1970-01-01
              • 1970-01-01
              • 2018-08-15
              相关资源
              最近更新 更多