0

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

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

2 23.07.2011 吉隆坡。17.00 SIF - BIF 0 - 1 4.173
2 23.07.2011 kl。19.00 FCK - 产科 2 - 2
14.774 3 30.07.2011 kl。17.00 AGF - OB 2 - 2 11.312
3 30.07.2011 kl。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);
   }

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

4

3 回答 3

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

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

于 2012-11-25T23:42:01.697 回答
0

你应该做一些数组:

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

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

goal1Array[9] = goal1;
listLength++;

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

最后,您可以遍历此列表以比较它们:

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

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

祝你好运。

于 2012-11-25T23:35:00.823 回答
0

Make an array of ints with an int for each team. They will be your sums for each team.

Then, strcmp the names of team1 and team2 with the names of your teams. Then, add the goals to their associated 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];
    }
}

Then, compare the goals of each team.

    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);
于 2012-11-25T23:46:49.427 回答