1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332
|
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#define NB_PROP 10
/******************* DECLARATION DES STRUCTURES *******************/
typedef struct res {
int sem_deb;
int sem_fin;
char nom[20];
struct res *next;
}reserv;
typedef struct app {
char type[2];
int ind_prop;
reserv *myRes;
struct app *next;
}appart;
void fpurge(FILE *fp){
int c;
if(fp != NULL){
while((c=fgetc(fp)) != '\n' && c != EOF){
}
}
}
void fclean(char *buffer, FILE *fp){
if(buffer != NULL && fp != NULL){
char *pc = strchr(buffer, '\n');
if(pc != NULL){
*pc = 0;
}else{
fpurge(fp);
}
}
}
void clear(){
#ifdef __WIN32__
system("CLS");
#else
system("clear");
#endif
}
void displayMenu(){
clear();
printf("..:: LOCATIONS SAISONNIERES ::..\n\n");
printf("\t0: Sortir ->\n");
printf("\t1: Ajouter un appartement\n");
printf("\t2: Ajouter une réservation sur un appartement\n");
printf("\t3: Supprimer un appartement\n");
printf("\t4: Supprimer une réservation sur un appartement\n");
printf("\t5: Afficher les appartements libres\n");
printf("\t6: Afficher les appartements réservés\n");
printf("\t7: Afficher tous les appartements\n");
printf("\nEntrez votre choix\n>");
}
int addAppart(appart **myApparts){
appart *apps = *myApparts;
appart *tmp = NULL;
appart *myAppart;
int rv = 0;
char *type;
size_t type_size = 3;
int ind_prop;
type = malloc(type_size * sizeof *type);
if(!type){
return -1;
}
/* Saisie des données */
printf("Saisissez le type d'appartement: ");
fflush(stdout);
fgets(type, type_size, stdin);
fclean(type, stdin);
do{
printf("Saisissez l'identifiant du propriétaire: ");
fflush(stdout);
rv = scanf("%d", &ind_prop);
fpurge(stdin);
}while(rv != 1);
/* Remplissage de l'élément */
myAppart = malloc(sizeof *myAppart);
if(!myAppart){
return -1;
}
strcpy(myAppart->type, type);
myAppart->ind_prop = ind_prop;
myAppart->myRes = NULL;
/* Parcours de la liste chaînée */
while(apps && strcmp(apps->type, type)!=0){
tmp = apps;
apps = apps->next;
}
/* Création du noeud pour insertion */
myAppart->next = apps;
if(tmp){
tmp->next = myAppart;
}else{
*myApparts = myAppart;
}
return 0;
}
int remAppart(appart **myApparts){
int id = -1;
int count = 0;
int rv = 0;
appart *apps = NULL;
appart *apps2 = NULL;
appart *tmp = NULL;
if(!*myApparts || !myApparts){
return -1;
}
apps = *myApparts;
do{
printf("Saisissez l'identifiant de l'appartement: ");
fflush(stdout);
rv = scanf("%d", &id);
fpurge(stdin);
}while(rv != 1);
if(id == -1){
return -2;
}
if(id == 0){
tmp = apps;
apps = apps->next;
*myApparts = apps;
free(tmp), tmp=NULL;
return 0;
}
apps2 = *myApparts;
apps = apps->next;
count++;
while(apps){
if(count == id){
apps2->next = apps->next;
free(apps);
return 0;
}
apps = apps->next;
apps2 = apps2->next;
count++;
}
return -3;
}
int addReserv(appart **myApparts){
int id = -1;
int count = 0;
int vscan = 0;
int semDeb, semFin;
appart *apps = *myApparts;
/*appart *tmp;*/
reserv *myReserv;
/* reserv *myRess = NULL;*/
char *Name;
Name = malloc(20*sizeof(char));
if(!*myApparts || !myApparts){
return -1;
}
do{
printf("Saisissez l'identifiant de l'appartement: ");
fflush(stdout);
vscan = scanf("%d", &id);
fpurge(stdin);
}while(vscan != 1);
do{
printf("Saisissez la semaine de début de la location: ");
fflush(stdout);
vscan = scanf("%d", &semDeb);
fpurge(stdin);
}while(vscan != 1);
do{
printf("Saisissez la semaine de fin de la location: ");
fflush(stdout);
vscan = scanf("%d", &semFin);
fpurge(stdin);
}while(vscan != 1);
if(semDeb > semFin){
return -4;
}
printf("Nom du locataire: ");
fflush(stdout);
fgets(Name, 20, stdin);
fclean(Name, stdin);
if(id == -1){
return -2;
}
myReserv = (reserv*)malloc(sizeof(reserv));
if(!myReserv){
return -5;
}
myReserv->sem_deb = semDeb;
myReserv->sem_fin = semFin;
strcpy(myReserv->nom,Name);
while(apps){
if(count == id){
myReserv->next = apps->myRes;
apps->myRes = myReserv;
free(apps);
return 0;
}
apps = apps->next;
count++;
}
return 0;
}
int remReserv(appart **myApparts){
return 0;
}
int affApparts(appart **myApparts, char **props, int mode){
appart *apps = *myApparts;
int count = 0, choice, i;
switch(mode){
case 0: /* Tous les apparts */
while(apps){
clear();
printf("--> Appartement N°%d\n", count);
printf("\tType:\t%s\n", apps->type);
printf("\tPropriétaire:\n\t%s", props[apps->ind_prop]);
printf("\n");
printf("--> Réservation(s):\n");
for(i=0;apps->myRes;i++, apps->myRes=apps->myRes->next){
printf("\t%d:\tDeb: %d\tFin: %d\tNom: %s\n",i, apps->myRes->sem_deb, apps->myRes->sem_fin, apps->myRes->nom);
}
printf("1: Appartement suivant\n0: Sortir\n>");
scanf("%d", &choice);
fflush(stdin);
if(choice == 1){
apps = apps->next;
count++;
}else{
return 0;
}
}
free(apps);
break;
case 1: /* Apparts libres */
break;
case 2: /* Apparts réservés */
break;
default:
return -1;
}
return 0;
}
void init_props(char **props){
props[0] = "Mr Londuba\n\t3, allée des enbrumes\n\t78666 Lost Highlands";
props[1] = "Mme Parvati\n\t9, rue de la baguette\n\t02896 Hill Valley";
props[2] = "Mlle Lovegood\n\t3 bis, avenue Philenas\n\t94200 Wastedland";
props[3] = "Mr Potter\n\t7, Privet Drive\n\tXXXX Unknown Town";
props[4] = "Mlle Ganger\n\t8, Westside road\n\t125 Two Pines Mall";
props[5] = "Mr Malefoy\n\t45, Darkest street\n\t452 Crypt";
}
/******************* FONCTION MAIN *******************/
int main(){
char **props;
appart *myApparts=NULL;
int choice;
int rv = 0;
props = malloc(NB_PROP * sizeof(char*));
if(!props){
exit(EXIT_FAILURE);
}
init_props(props);
while(1){
do{
displayMenu();
rv = scanf("%d", &choice);
fpurge(stdin);
}while(rv!=1 || choice<0 || choice>7);
switch(choice){
case 0:
clear();
return EXIT_SUCCESS;
break;
case 1:
if(addAppart(&myApparts) < 0){
perror("Erreur lors de l'ajout du nouvel appartement");
sleep(2);
}
break;
case 2:
addReserv(&myApparts);
break;
case 3:
remAppart(&myApparts);
break;
case 4:
break;
case 5:
affApparts(&myApparts, props, 1);
break;
case 6:
affApparts(&myApparts, props, 2);
break;
case 7:
affApparts(&myApparts, props, 0);
break;
}
}
return EXIT_SUCCESS;
} |
Partager