clplaner.c
changeset 75 a2c32869d4d5
child 77 49e0babccb23
equal deleted inserted replaced
74:829976007e62 75:a2c32869d4d5
       
     1 /**
       
     2  * test/demos/clplaner.c
       
     3  * Copyright (C) 2008 Markus Broeker
       
     4  */
       
     5 
       
     6 #include <stdio.h>
       
     7 #include <stdlib.h>
       
     8 #include <string.h>
       
     9 #include <time.h>
       
    10 #include <limits.h>
       
    11 
       
    12 #define MAXLOOP 25
       
    13 #define MAXLINE 80
       
    14 
       
    15 #ifndef MAXTEAM
       
    16 #define MAXTEAM 32
       
    17 #endif
       
    18 
       
    19 #define getrandom(max) (1+(int)((float)(max)*rand()/RAND_MAX))
       
    20 
       
    21 void swap (int *v, int i, int j)
       
    22 {
       
    23     int temp;
       
    24 
       
    25     if (i == j)
       
    26         return;
       
    27 
       
    28     temp = v[i];
       
    29 
       
    30     v[i] = v[j];
       
    31     v[j] = temp;
       
    32 }
       
    33 
       
    34 void cleanup (char **first, char **second)
       
    35 {
       
    36     int i;
       
    37 
       
    38     i = 0;
       
    39     while (first[i] != NULL)
       
    40         free (first[i++]);
       
    41 
       
    42     i = 0;
       
    43     while (second[i] != NULL)
       
    44         free (second[i++]);
       
    45 }
       
    46 
       
    47 unsigned int time_seed ()
       
    48 {
       
    49     time_t now = time (NULL);
       
    50 
       
    51     unsigned char *p = (unsigned char *)&now;
       
    52     unsigned int seed = 0;
       
    53 
       
    54     size_t i;
       
    55 
       
    56     for (i = 0; i < sizeof (now); i++)
       
    57         seed = seed * (UCHAR_MAX + 2U) + p[i];
       
    58 
       
    59     return seed;
       
    60 }
       
    61 
       
    62 int getTeams (FILE * f, char **first, int *first_idx, char **second, int *second_idx)
       
    63 {
       
    64     int i;
       
    65     char buffer[MAXLINE + 1];
       
    66     char *ptr;
       
    67 
       
    68     i = 0;
       
    69     while ((i < MAXTEAM) && fgets (buffer, MAXLINE, f) != NULL) {
       
    70         if ((first[i] = malloc (MAXLINE + 1)) == NULL) {
       
    71             perror ("malloc");
       
    72             break;
       
    73         }
       
    74         if ((ptr = strchr (buffer, '\n')))
       
    75             *ptr = '\0';
       
    76 
       
    77         strncpy (first[i], buffer, MAXLINE);
       
    78         first_idx[i] = i;
       
    79 
       
    80         if (fgets (buffer, MAXLINE, f) == NULL) {
       
    81             printf ("You need 2, 4, 6, ..., or %d teams in your file\n", 2 * MAXTEAM);
       
    82             second[i] = first[i + 1] = NULL;
       
    83             cleanup (first, second);
       
    84             fclose (f);
       
    85             exit (EXIT_FAILURE);
       
    86         }
       
    87 
       
    88         if ((second[i] = malloc (MAXLINE + 1)) == NULL) {
       
    89             perror ("malloc");
       
    90             break;
       
    91         }
       
    92         if ((ptr = strchr (buffer, '\n')))
       
    93             *ptr = '\0';
       
    94 
       
    95         strncpy (second[i], buffer, MAXLINE);
       
    96         second_idx[i] = i;
       
    97         i++;
       
    98     }
       
    99 
       
   100     first[i] = second[i] = NULL;
       
   101 
       
   102     return i;
       
   103 }
       
   104 
       
   105 void shakeTeamIdx (int *first_idx, int *second_idx, int teams)
       
   106 {
       
   107     int i, j, k;
       
   108 
       
   109     /*
       
   110      * swap the index MAXLOOP times...
       
   111      */
       
   112     for (i = 0; i < MAXLOOP; i++) {
       
   113         for (j = 0; j < teams; j++) {
       
   114             k = getrandom (teams - 1);
       
   115             swap (second_idx, j, k);
       
   116 
       
   117             k = getrandom (teams - 1);
       
   118             swap (first_idx, j, k);
       
   119         }
       
   120     }
       
   121 
       
   122     /*
       
   123      * avoid group clashes
       
   124      */
       
   125     for (i = 0; i < teams - 1; i++) {
       
   126         if (first_idx[i] == second_idx[i])
       
   127             swap (first_idx, i, i + 1);
       
   128     }
       
   129 
       
   130     if (first_idx[i] == second_idx[i])
       
   131         swap (first_idx, i, i - 1);
       
   132 }
       
   133 
       
   134 int main (int argc, char **argv)
       
   135 {
       
   136     FILE *f;
       
   137 
       
   138     char *first[MAXTEAM + 1];
       
   139     char *second[MAXTEAM + 1];
       
   140 
       
   141     int first_idx[MAXTEAM];
       
   142     int second_idx[MAXTEAM];
       
   143 
       
   144     int i, teams;
       
   145 
       
   146     if (argc != 2) {
       
   147         printf ("Usage: %s FILE\n\n", argv[0]);
       
   148         printf ("One Team per line, seperated by newline, n=2, 4, ..., or %d\n", 2 * MAXTEAM);
       
   149         printf ("Team n is the Groupwinner, n+1 is the Runners Up\n");
       
   150         printf ("Report bugs to mbroeker@largo.homelinux.org\n");
       
   151         return EXIT_FAILURE;
       
   152     }
       
   153 
       
   154     if ((f = fopen (argv[1], "r")) == NULL) {
       
   155         perror ("fopen");
       
   156         return EXIT_FAILURE;
       
   157     }
       
   158 
       
   159     srand (time_seed ());
       
   160 
       
   161     teams = getTeams (f, first, first_idx, second, second_idx);
       
   162     shakeTeamIdx (first_idx, second_idx, teams);
       
   163 
       
   164     for (i = 0; i < teams; i++)
       
   165         printf ("%30s vs %s\n", second[second_idx[i]], first[first_idx[i]]);
       
   166 
       
   167     cleanup (first, second);
       
   168 
       
   169     if (f != NULL)
       
   170         fclose (f);
       
   171 
       
   172     return EXIT_SUCCESS;
       
   173 }