Old stuff/ecole_etude_fac_de_pau/licence_3/genie-logiciel/td1/parking.c
(Deskargatu)
#include "parking.h"
#include <stdio.h>
#include <stdlib.h>
parking* initialiser_parking()
{
parking* park;
park = (parking*)malloc(sizeof(parking));
int i;
for(i=0;i<1200;i++)
{
strcpy(park->vehicules[i].immatriculation,"");
park->places[i].numero = i;
}
printf("** Parking initialise **\n");
return park;
}
parking* se_garer(parking* park, place* pl, vehicule* vec)
{
if (!occupee(park,pl))
{
strcpy(park->vehicules[pl->numero].immatriculation,vec->immatriculation);
park->nb_places_occupees = park->nb_places_occupees + 1;
printf("--> Vehicule %s gare en %d (%d en tout)\n",park->vehicules[pl->numero].immatriculation, pl->numero,park->nb_places_occupees);
printf("fin garer\n");
}
else
{
printf("/!\\ Impossible de se garer: Place occupee\n");
}
return park;
}
int autoriser(parking* park)
{
return park->nb_places_occupees < 1200;
}
int occupee(parking* park,place* pl)
{
return strcmp(park->vehicules[pl->numero].immatriculation,"");
}
parking* quitter(parking* park, vehicule* vec)
{
int i;
i=0;
while(i<1200 && strcmp(vec->immatriculation, park->vehicules[i].immatriculation))
{
i++;
}
if (i<1200)
{
printf("Suppresion du vehicule en place %d\n",i);
strcpy(park->vehicules[i].immatriculation,"");
}
}
place* quelle_place(parking* park, vehicule* vec)
{
int i=0;
while(i<1200 && strcmp(vec->immatriculation, park->vehicules[i].immatriculation))
{
i++;
}
return i<1200 ? &park->places[i] : NULL;
}
vehicule* quelle_vehicule(parking* park, place* pl)
{
return occupee(park,pl) ? &park->vehicules[pl->numero] : NULL;
}
place* places_occupees(parking* park, int* nb)
{
place* ret= NULL;
*nb=0;
int i=0;
for(i=0;i<1200;i++)
{
if (! strcmp(park->vehicules[i].immatriculation,""))
{
*nb = *nb + 1;
ret = (ret==NULL ? malloc(sizeof(place)) : realloc(ret,(*nb) * sizeof(place)));
ret[*nb].numero = park->places[i].numero;
}
}
}