나는 어떤 라운드가 가장 많은 목표를 가지고 있었는지, 그리고 @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);
}
}
하지만 지금 두 번을 반복하고 확실히 당신의 문제에 대한 최선의 해결책이 아닙니다.
새로운 변수에 goal1 goal2를 추가하려고했지만 실제로 작동하지 않는 것처럼 보입니다. – Winkz
작동하지 않는 코드를 표시 할 수 있습니까? –
확실히 지금 추가하십시오 – Winkz