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
|
#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 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;
char *type;
int ind_prop;
type = (char*)malloc(2*sizeof(char));
/* Saisie des données */
printf("Saisissez le type d'appartement: ");
fflush(stdin);
scanf("%s",type);
printf("Saisissez l'identifiant du propriétaire: ");
fflush(stdin);
scanf("%d", &ind_prop);
/* Remplissage de l'élément */
myAppart = (appart*)malloc(sizeof(appart));
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, count = 0;
appart *apps, *apps2;
apps = *myApparts;
printf("Saisissez l'identifiant de l'appartement: ");
fflush(stdin);
scanf("%d", &id);
if(id == -1){
return -1;
}
if(id == 0){
apps = apps->next;
*myApparts = apps;
free(apps);
return 0;
}
apps2 = *myApparts;
apps = apps->next;
count++;
while(apps){
if(count == id){
apps2->next = apps->next;
free(apps);
*myApparts = apps2;
free(apps2);
return 0;
}
apps = apps->next;
apps2 = apps2->next;
count++;
}
return -2;
}
int affApparts(appart **myApparts, char **props, int mode){
appart *apps = *myApparts;
int count = 0, choice;
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("1: Appartement suivant\n0: Sortir\n>");
scanf("%d", &choice);
if(choice == 1){
apps = apps->next;
count++;
}else{
return 0;
}
}
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] = "prop0";
props[1] = "prop1";
props[2] = "prop2";
props[3] = "prop3";
props[4] = "prop4";
props[5] = "prop5";
}
/******************* FONCTION MAIN *******************/
int main(){
char **props;
appart *myApparts=NULL;
int choice;
props = (char**)malloc(NB_PROP*sizeof(char*));
if(!props){
return EXIT_FAILURE;
}
init_props(props);
while(1){
displayMenu();
scanf("%d", &choice);
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:
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