我有一个包含大量行的文本文件,病态复制粘贴其中一些,以显示我正在使用的内容。
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 %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);
}
我将值变为不同的变量,但是如何从每一轮中添加结果并查看哪一个目标最多?任何建议都会得到满足! 谢谢:))
答案 0 :(得分: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);
}
}
但这现在循环两次,绝对不是解决问题的最佳方案。
答案 1 :(得分:0)
你应该制作一些数组:
int goal1Array[]
int goal2Array[]
int listLength = 0;
然后,当你阅读目标时,你可以将它们添加到数组中(确保记录你要添加的数量):
goal1Array[9] = goal1;
listLength++;
注意:您需要进行一些动态内存管理。查找c数组等。
最后,您可以循环浏览此列表以进行比较:
for (i = 0; i < listLength; i++) {
/*compare stuff*/
}
这只是一些一般性建议,您需要付出一些努力才能在没有内存错误的情况下进行编译。
祝你好运。
答案 2 :(得分:0)
为每个团队创建一个int数组。他们将是每个团队的总和。
然后,strcmp team1和team2的名称以及您团队的名称。然后,将目标添加到其关联的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);