我有一个有很多行的文本文件,我复制粘贴其中一些在这里,向您展示我正在使用什么。
1 16.07.2011 kl. 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 kl. 17.00 SIF - BIF 0 - 1 4.173
2 23.07.2011 kl. 19.00 FCK - OB 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 %lfn", &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åln", runde, prev_goal);
}
我将值放入不同的变量中,但是如何将每轮的结果相加,看看哪一轮的进球最多?任何建议将不胜感激!谢谢:)
我将假设您只关心哪个回合的进球最多,并且您不需要像@Ben建议的那样将文本文件存储在内存中。
如果是这种情况,您可以执行以下操作:
int i, maxGoals = 0, roundWithMostGoals = 0;
for (i = 0; fscanf(ifp, "%d %d.%d.%d kl. %lf %4s - %4s %d - %d %lfn", &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 %lfn", &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 inn");
// 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 %lfn", &runde, &dag, &month, &year, &clock, team1, team2, &goal1, &goal2, &attendance) == 10; ++i)
{
if (maxGoals == goal1 + goal2)
{
printf("tRound %dn", runde);
}
}
但是现在循环了两次,绝对不是解决您问题的最佳方法。
你应该做一些数组:
int goal1Array[]
int goal2Array[]
int listLength = 0;
然后,当您阅读目标时,您可以将它们添加到数组中(确保记录要添加的数量):
goal1Array[9] = goal1;
listLength++;
注:您将需要执行一些动态内存管理。查找 c 数组等。
最后,您可以循环浏览此列表以比较它们:
for (i = 0; i < listLength; i++) {
/*compare stuff*/
}
这只是一些一般建议,您需要付出一些努力才能在没有内存错误的情况下进行编译。
祝你好运。
为每个团队制作一个带有 int 的整数数组。它们将是每个团队的总和。
然后,将团队 1 和团队 2 的名称与团队的名称进行 strcmp 。然后,将目标添加到其关联的目标总和中。
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 %lfn", &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);