soccer.c
contents ::
  soccer.c
  input

/**********************************************
 *                                            *
 *   James Little Programming contest         *
 *              Question H                    *
 *   *Soccer League Problem 30 Points         *
 *                                            *
 *   Sorted only by Points, Also need         *
 *   to be sorted by other variables,         *
 *   such as wins, difference in goal         *
 *   and goals for and alphabetically         *
 *                                            *
 **********************************************/

#include <stdio.h>
#include <stdlib.h>

typedef struct line {
  char name[20];
  int win;
  int draw;
  int forp;
  int agap;
  int points;
} team;

void quicksort(team *a, int lo, int hi){
  if( hi > lo ) {
    int left, right, median;
    team temp;
    left=lo;
    right=hi;
    median=a[(lo+hi)/2].points;
    while(right >= left){
      
      while(a[left].points > median) left++;
      
      while(a[right].points < median) right--;
      if(left > right) break;
      
      temp=a[left];
      a[left]=a[right];
      a[right]=temp; //swap
      
      left++;
      right--;
    }

    
    quicksort(a, lo, right);// divide and conquer
    quicksort(a, left,  hi);
  }
}/*quicksort*/

int duplicate(team *players, char *buffer, int count){
  int i=0;
  for(i = 0; i<count; i++){
    if(strcmp(players[i].name, buffer) == 0){
      // players[i].count++;
      return i;
    }
  }
  return -1;


main(){
  int max_num = 5;
  int count = 0;
  int i = 0;
  team table[max_num];
  char buffer1[20], buffer2[20];
  int score1=0, score2=0;
  int pos = 0;
  int winners_count = 0;
  team winners[max_num];

  while(count<max_num){
    scanf("%s", buffer1);
    if(strcmp(buffer1, "#")==0)
      break;

    scanf("%d %s %d", &score1, buffer2, &score2);

    if((pos = duplicate(table, buffer1, count)) == -1){
      strcpy(table[count].name, buffer1);
      table[count].forp = score1;
      table[count].agap = score2;
      table[count].draw = 0;
      table[count].win = 0;
      table[count].points = 0;

      if(score1 > score2){
         table[count].win = 1;
         table[count].draw = 0;
         table[count].points = 3;
      } else if(score1 == score2){
         table[count].win = 0;
         table[count].draw = 1;
         table[count].points = 1;
      }
      count++;

    } else {
      table[pos].forp += score1;
      table[pos].agap += score2;
      if(score1 > score2){
         
         table[pos].win += 1;
         table[pos].points += 3;
      } else if(score1 == score2){
         table[pos].draw += 1;
         table[pos].points += 1;
      }
    }

    if((pos = duplicate(table, buffer2, count)) == -1){
      strcpy(table[count].name, buffer2);
      table[count].forp = score2;
      table[count].agap = score1;
      table[count].draw = 0;
      table[count].win = 0;
      table[count].points = 0;

      if(score2 > score1){
         table[count].win = 1;
         table[count].draw = 0;
         table[count].points = 3;
      } else if(score1 == score2){
         table[count].win = 0;
         table[count].draw = 1;
         table[count].points = 1;
      }
      count++;

    } else {
      table[pos].forp += score2;
      table[pos].agap += score1;
      if(score2 > score1){
         table[pos].win += 1;
         table[pos].points += 3;
      } else if(score1 == score2){
         table[pos].draw += 1;
         table[pos].points += 1;
      }
    }
  }

  quicksort(table, 0, count-1);

  i=0;
  printf("Team:\tW\tD\tF\tA\tP\n");
  printf("-----\t-\t-\t-\t-\t-\n");
  while(i<count){
    printf("%s\t%d\t%d\t%d\t%d\t%d\n", table[i].name, 
            table[i].win, table[i].draw, 
            table[i].forp, table[i].agap, table[i].points);
    i++;
  }
  

  return EXIT_SUCCESS;
}

James Little