Fixed alloc_mem() deporte kind error.
[final_edu.git] / final.c
blob1d3f41d7957f803c8838fe8274daa1cc918a18b4
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
5 /*
6 * Structure declaration
7 */
8 typedef struct _jugador jugador;
9 struct
10 _jugador
12 char nombre[128];
13 int anotaciones;
16 typedef struct _futbol futbol;
17 struct
18 _futbol
21 * Here goes the distinct fields
25 typedef struct _basket basket;
26 struct
27 _basket
30 * Here goes the distinct fields
32 int triples;
35 typedef struct _deporte deporte;
36 struct
37 _deporte
39 char *nombre;
40 int victorias;
41 int derrotas;
42 int anotaciones_favor;
43 int anotaciones_contra;
44 jugador estrella;
46 /*
47 * 0 = futbol
48 * 1 = basket
50 int num_deporte;
51 void *tipo_deporte;
54 int
55 get_equipo(deporte *equipo)
57 char tmp[1024];
59 /*
60 * Prints user message
62 switch ( equipo->num_deporte)
65 case 0:
66 printf("[Nombre equipo],[victorias],[derrotas],[goles a favor]"
67 ",[goles en contra] : ");
68 break;
70 case 1:
71 printf("[Nombre equipo],[victorias],[derrotas],[anotaciones a favor]"
72 ",[anotaciones en contra] : ");
73 break;
78 * Gets data
80 scanf("%s", tmp);
81 equipo->nombre = strtok(tmp, ",");
82 sscanf(strtok(NULL, ","), "%d", &equipo->victorias);
83 sscanf(strtok(NULL, ","), "%d", &equipo->derrotas);
84 sscanf(strtok(NULL, ","), "%d", &equipo->anotaciones_favor);
85 sscanf(strtok(NULL, ","), "%d", &equipo->anotaciones_contra);
89 * Gets specific data
91 switch ( equipo->num_deporte)
94 case 0:
95 break;
97 case 1:
98 break;
102 printf("[Nombre jugador estrella] : ");
103 scanf("%s"
104 , equipo->estrella.nombre
107 return 1;
111 get(deporte **equipos)
113 while (*equipos)
114 get_equipo(*equipos),equipos++;
116 return 1;
119 deporte
120 get_win(deporte **equipos)
122 deporte winner = **equipos;
124 while ( *equipos )
126 if( (*equipos)->victorias > winner.victorias)
127 winner=**equipos;
129 equipos++;
132 return winner;
136 * Allocate memory
138 deporte **
139 alloc_mem(int LIM, int num_deporte)
141 deporte **tmp = malloc(sizeof(void *)*(LIM+1));
143 int i;
144 for(i=0;i<LIM;i++)
145 tmp[i] = malloc(sizeof(deporte))
146 , tmp[i]->num_deporte = num_deporte
147 , tmp[i]->tipo_deporte = malloc(sizeof(futbol));
148 tmp[LIM] = NULL;
150 return tmp;
154 main()
156 int LIM=0;
158 printf("Numero de equipos futbol : ");
159 scanf("%d", &LIM);
160 deporte **equipos_futbol = alloc_mem(LIM, 0);
163 printf("Numero de equipos basket : ");
164 scanf("%d", &LIM);
165 deporte **equipos_basket = alloc_mem(LIM, 1);
167 get(equipos_futbol);
169 get(equipos_basket);
171 deporte winner = get_win(equipos_futbol);
173 printf("El ganador de futbol es : %s con %d victorias.\n", winner.nombre
174 , winner.victorias);
176 return 1;