【问题标题】:c combining numbers from each line in a text filec 组合文本文件中每一行的数字
【发布时间】:2012-11-13 10:42:54
【问题描述】:

我有一个包含很多行的文本文件,我将其中的一些复制粘贴到这里,以向您展示我正在处理的内容。

1 16.07.2011 吉隆坡。 17.00 转播 - FCN 2 - 0 6.965
1 17.07.2011 吉隆坡。 14.00 FCM - SIF 1 - 2 5.370

2 23.07.2011 吉隆坡。 17.00 SIF - BIF 0 - 1 4.173
2 23.07.2011 吉隆坡。 19.00 FCK - 产科 2 - 2 14.774
3 30.07.2011 千升。 17.00 AGF - 产科 2 - 2 11.312
3 30.07.2011 千升。 19.00 FCK - FCN 2 - 0 11.076

while (fscanf(ifp, "%d %d.%d.%d kl. %lf %4s - %4s %d - %d %lf\n", &runde, &dag, &month, &year, &clock, team1, team2, &goal1, &goal2, &attendance) == 10)
    int prev_goal = goal1 + goal2;
    int current;
    if(prev_goal > current) {
    printf("Runde %d var den mest målrige med %d mål\n", runde, prev_goal);
   }

我将值放入不同的变量中,但是如何将每一轮的结果相加,看看哪一个的目标最多?任何建议都会被采纳! 谢谢你:)

【问题讨论】:

  • 尝试将goal1goal2添加到新变量中,但似乎无法真正发挥作用
  • 你能显示那个不工作的代码吗?
  • 丹麦, ikke sant?请参阅下面@nonsensical 的答案。
  • Jo, ja men den virker sku ikke helt :(

标签: c text-files scanf


【解决方案1】:

我会假设您只关心哪一轮的目标最多,并且您不需要像 @Ben 建议的那样将文本文件存储在内存中。

如果是这种情况,您可以这样做:

int i, maxGoals = 0, roundWithMostGoals = 0;

for (i = 0; fscanf(ifp, "%d %d.%d.%d kl. %lf %4s - %4s %d - %d %lf\n", &runde, &dag, &month, &year, &clock, team1, team2, &goal1, &goal2, &attendance) == 10; ++i)
{
    if (maxGoals < goal1 + goal2)
    {
        roundWithMostGoals = runde;
        maxGoals = goal1 + goal2;
    }
}

// Edit:
printf("The largest number of goals was %d, scored in round %d", maxGoals, roundWithMostGoals);

这段代码确实有问题。如果有两轮进球数最多,则只打印第一个。

为避免这种情况,我们需要循环两次,这并不理想,我建议使用其他建议的方法之一,将所有这些数据加载到内存中。

但是,尽管我认为它不是最佳解决方案,但这里有一个与上述类似的修改解决方案:

int i, maxGoals = 0, roundWithMostGoals = 0;

// Find the maximum number of goals that was scored in any one round.
for (i = 0; fscanf(ifp, "%d %d.%d.%d kl. %lf %4s - %4s %d - %d %lf\n", &runde, &dag, &month, &year, &clock, team1, team2, &goal1, &goal2, &attendance) == 10; ++i)
{
    if (maxGoals < goal1 + goal2)
    {
        maxGoals = goal1 + goal2;
    }
}

printf("The largest number of goals scored was %d.\n", maxGoals);
printf("The largest number of goals was scored in\n");

// TODO: Reposition the file stream back to the beginning or close it and then reopen it again.
// XXX Code Here XXX

// Loop through again getting all the rounds with the maximum number of goals.

for (i = 0; fscanf(ifp, "%d %d.%d.%d kl. %lf %4s - %4s %d - %d %lf\n", &runde, &dag, &month, &year, &clock, team1, team2, &goal1, &goal2, &attendance) == 10; ++i)
{
    if (maxGoals == goal1 + goal2)
    {
        printf("\tRound %d\n", runde);
    }
}

但这现在循环了两次,绝对不是解决您问题的最佳方法。

【讨论】:

  • 谢谢,但是当我打印出来时,我得到了几轮而不是确切的目标数量。
  • 您从哪里打印?我已经编辑了我的帖子以打印最多的进球数以及它发生在哪一轮。
  • 不知何故,我只得到了该回合中进球最多的第一场比赛的进球.. 嗯
  • 据我所知,您正在迭代几轮。您能否更详细地解释一下您的数据实际代表什么?这将是相同的构造,但会迭代不同的部分以进行匹配。
【解决方案2】:

你应该做一些数组:

int goal1Array[]
int goal2Array[]
int listLength = 0;

然后,当您阅读目标时,您可以将它们添加到数组中(确保记录您要添加的数量):

goal1Array[9] = goal1;
listLength++;

注意:您需要进行一些动态内存管理。查找 c 数组等。

最后你可以遍历这个列表来比较它们:

for (i = 0; i < listLength; i++) {
    /*compare stuff*/
}

这只是一些一般性建议,您需要付出一些努力才能使其编译而不会出现内存错误。

祝你好运。

【讨论】:

  • 谢谢,明天试试! :)
  • @Winkz 你需要什么帮助。我怀疑这是一个家庭作业,所以我不会给你答案。这是一个非常简单的概念,继续浏览文件并将答案存储在数组中。这是关于动态数组的教程fydo.net/gamedev/dynamic-arrays 问一个具体的帮助问题,我会将其添加到我的答案中。
【解决方案3】:

为每个团队制作一个带有一个 int 的 int 数组。它们将是每个团队的总和。

然后,strcmp 团队 1 和团队 2 的名称以及您的团队名称。然后,将目标添加到相关的goalSum。

int goalSum[3];
goalSum[0] = 0;goalSum[1] = 0;goalSum[2] = 0


while (fscanf(ifp, "%d %d.%d.%d kl. %lf %4s - %4s %d - %d %lf\n", &runde, &dag, &month, &year, &clock, team1, team2, &goal1, &goal2, &attendance) == 10)
{

    //comparisions for the first team
    if(strcmp(team1,"nameofteam0")==0)
    {
            ++goalSum[0];
    }
    if(strcmp(team1,"nameofteam1")==0)
    {
            ++goalSum[1];
    }
    if(strcmp(team1,"nameofteam2")==0)
    {
            ++goalSum[2];
    }
    //comparisions for the second team
    if(strcmp(team2,"nameofteam0")==0)
    {
            ++goalSum[0];
    }
    if(strcmp(team2,"nameofteam1")==0)
    {
            ++goalSum[1];
    }
    if(strcmp(team2,"nameofteam2")==0)
    {
            ++goalSum[2];
    }
}

然后,比较每个团队的目标。

    const int numberOfTeams = 3;
    int winningTeam=0;

//You'll have to do the support for draws yourself.
    for(int i = 0; i<numberOfTeams; ++i)
    {
        if(sumOfGoal[i]>sumOfGoal[i+1])
        {
                winningTeam = i+1;
        }
        else
        {
                winningTeam = i+2;
        }
    }
    printf("Team%i wins!\n", winningTeam);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-08-11
    • 1970-01-01
    • 1970-01-01
    • 2013-07-26
    • 2017-01-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多