- Code: Select all
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#define lim 40
#define lim_2 39
#define tour 102
#define wor 72
int main()
{
/* In the struct below, result[0] are the wins, result[1] are the ties, result[2] are the losses; goals[0] are the goals scored and goals[1] are the goals against. */
struct _team
{
char name[lim];
int points, result[3], games, goals[2];
}team[lim];
int n_tournaments, i, j, k, l, games, teams, score1, score2, aux;
char *team1, *team2, n_team[lim], word[wor], tournament[tour];
scanf("%d", &n_tournaments);
getc(stdin);
for(i=0; i < n_tournaments; ++i)
{
for(j=0; j < lim; ++j)
{
team[j].games = 0;
team[j].points = 0;
for(k=0; k < 2; k++)
team[j].goals[k] = 0;
for(k=0; k < 3; ++k)
team[j].result[k] = 0;
}
aux = 0;
fgets(tournament, tour, stdin);
scanf("%d", &teams);
getc(stdin);
for(j=0; j < teams; ++j)
{
fgets(n_team, lim, stdin);
team1 = strtok(n_team, "\n");
strcpy(team[j].name, team1);
}
scanf("%d", &games);
getc(stdin);
for(j=0; j < games; ++j)
{
fgets(word, wor, stdin);
team1 = strtok(word, "#");
score1 = atoi(strtok(NULL, "@"));
score2 = atoi(strtok(NULL, "#"));
team2 = strtok(NULL, "\n");
if(strcasecmp(team1, team2) != 0)
{
for(k=0; k < teams; ++k)
{
if(strcasecmp(team[k].name, team1) == 0)
break;
}
for(l=0; l < teams; ++l)
{
if(strcasecmp(team[l].name, team2) == 0)
break;
}
team[l].games++;
team[k].games++;
team[l].goals[1] += score1;
team[l].goals[0] += score2;
team[k].goals[0] += score1;
team[k].goals[1] += score2;
if(score1 < score2)
{
team[l].points += 3; /* Lose 1 and Win 2! */
team[k].result[2]++;
team[l].result[0]++;
}
else if(score1 > score2)
{
team[k].points += 3; /* Win 1 and Lose 2! */
team[l].result[2]++;
team[k].result[0]++;
}
else
{
team[k].points++; /* Tie between 1 */
team[l].points++; /* and 2! */
team[k].result[1]++;
team[l].result[1]++;
}
}
}
/* Calculates for to print OUTPUT */
printf("%s", tournament);
for(k=0; k < (teams - 1); ++k)
{
for(l=k; l < teams; ++l)
{
/* Most points */
if(team[k].points < team[l].points)
{
team[lim_2] = team[k];
team[k] = team[l];
team[l] = team[lim_2];
}
/* Most wins */
else if(team[k].points == team[l].points && team[k].result[0] < team[l].result[0])
{
team[lim_2] = team[k];
team[k] = team[l];
team[l] = team[lim_2];
}
/* Most goals difference */
else if(team[k].points == team[l].points && team[k].result[0] == team[l].result[0] && (team[k].goals[0] - team[k].goals[1]) < (team[l].goals[0] - team[l].goals[1]))
{
team[lim_2] = team[k];
team[k] = team[l];
team[l] = team[lim_2];
}
/* Most goals scored */
else if(team[k].points == team[l].points && team[k].result[0] == team[l].result[0] && (team[k].goals[0] - team[k].goals[1]) == (team[l].goals[0] - team[l].goals[1]) && team[k].goals[0] < team[k].goals[0])
{
team[lim_2] = team[k];
team[k] = team[l];
team[l] = team[lim_2];
}
/* Less games played */
else if(team[k].points == team[l].points && team[k].result[0] == team[l].result[0] && (team[k].goals[0] - team[k].goals[1]) == (team[l].goals[0] - team[l].goals[1]) && team[k].goals[0] == team[l].goals[0] && team[k].games > team[l].games)
{
team[lim_2] = team[k];
team[k] = team[l];
team[l] = team[lim_2];
}
/* Case two teams are tied, the list is appears in lexicographic order. (OBS: Use of the function strcasecmp) */
else if(team[k].points == team[l].points && team[k].result[0] == team[l].result[0] && (team[k].goals[0] - team[k].goals[1]) == (team[l].goals[0] - team[l].goals[1]) && team[k].goals[0] == team[l].goals[0] && team[k].games == team[l].games && strcasecmp(team[k].name, team[l].name) >= 0 )//&& strlen(team[k].name)<strlen(team[l].name))
{
team[lim_2] = team[k];
team[k] = team[l];
team[l] = team[lim_2];
}
}
}
aux++;
/* Printing OUTPUT */
for(j=0; j < teams; ++j)
{
printf("%d) %s %dp, %dg (%d-%d-%d), %dgd (%d-%d)\n", aux, team[j].name, team[j].points, team[j].games, team[j].result[0], team[j].result[1], team[j].result[2], (team[j].goals[0] - team[j].goals[1]), team[j].goals[0], team[j].goals[1]);
aux++;
}
if(i < (n_tournaments - 1))
printf("\n");
}
return 0;
}
Could somebody do me a favor.
Thanks a million.



