-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathclient.c
More file actions
414 lines (384 loc) · 12.7 KB
/
Copy pathclient.c
File metadata and controls
414 lines (384 loc) · 12.7 KB
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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
#include "client.h"
/*-------------------------------------------- Toutes les affichage --------------------------------------------------------*/
void afficherTabClient(Client tabClient[], int nbClient)
{
int i = 0;
printf("Liste des Clients\n");
printf("━━━━━━━━┳━━━━━━━━━━┳━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━┳━━━━━━━━━━━━━\n");
printf(" ID\t┃Civilité ┃Nom ┃Prénom ┃Cdes En Attente┃Cdes En Cours┃Adresse \n");
printf("━━━━━━━━╋━━━━━━━━━━╋━━━━━━━━━━━━━━━╋━━━━━━━━━━━━━━━╋━━━━━━━━━━━━━━━╋━━━━━━━━━━━━━╋━━━━━━━━━━━━━\n");
for (i = 0; i < nbClient; i++)
printf("%d\t┃%-10s┃%-15s┃%-15s┃%-15d┃%-13d┃%s\n", tabClient[i].idClient, tabClient[i].civilite, tabClient[i].nom, tabClient[i].prenom, longueur(tabClient[i].commandesEnAttente),longueur(tabClient[i].commandesEnCours),tabClient[i].adresse);
printf("━━━━━━━━┻━━━━━━━━━━┻━━━━━━━━━━━━━━━┻━━━━━━━━━━━━━━━┻━━━━━━━━━━━━━━━┻━━━━━━━━━━━━━┻━━━━━━━━━━━━━\n");
printf("Nombre de client : %d \n", nbClient);
}
void afficherConsultClient(Client client)
{
system("clear");
printf("\n┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓\n");
printf("┃\t\t\tConsultation Client\t\t\t\t ┃\n");
printf("┣━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┫\n");
printf("┃ID: %d\t\t\t\t\t\t\t\t ┃\n",client.idClient);
printf("┃Nom: %-64s┃\n",client.nom);
printf("┃Prenom: %-64s┃\n",client.prenom);
printf("┃Address: %-64s┃\n",client.adresse);
printf("┣━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┫\n");
printf("┃\t\t\tCommandes En Attentes\t\t\t\t ┃\n");
printf("┣━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━┳━━━━━━━━┳━━━━━━━━━━━━┫\n");
printf("┃idCommande┃Article ┃Prix Unit.┃Quantité┃Prix Total ┃\n");
printf("┣━━━━━━━━━━╋━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╋━━━━━━━━━━╋━━━━━━━━╋━━━━━━━━━━━━┫\n");
afficherCommande(client.commandesEnAttente);
printf("┣━━━━━━━━━━┻━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┻━━━━━━━━━━┻━━━━━━━━┻━━━━━━━━━━━━┫\n");
printf("┃\t\t\tCommandes En Cours\t\t\t\t ┃\n");
printf("┣━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━┳━━━━━━━━┳━━━━━━━━━━━━┫\n");
printf("┃idCommande┃Désignation Article ┃Prix Unit.┃Quantité┃Prix Total ┃\n");
printf("┣━━━━━━━━━━╋━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╋━━━━━━━━━━╋━━━━━━━━╋━━━━━━━━━━━━┫\n");
afficherCommande(client.commandesEnCours);
printf("┗━━━━━━━━━━┻━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┻━━━━━━━━━━┻━━━━━━━━┻━━━━━━━━━━━━┛\n");
}
/*-------------------------------------------- Chargement du tableau de client --------------------------------------------------------*/
Client saisirClient(FILE *flot)
{
Client c;
fscanf(flot, "%d%s%s%s ", &c.idClient,c.civilite,c.nom,c.prenom);
fgets(c.adresse, 50, flot);
c.adresse[strlen(c.adresse) - 1] = '\0';
c.commandesEnAttente = NULL;
c.commandesEnCours = NULL;
return c;
}
Client *chargeTabClient(Client *tabClient, int *nbClient)
{
FILE *flot;
int i = 0, tMax = 1;
flot = fopen(nomFichClient, "r");
if (flot == NULL)
{
printf("Problème d'ouverture du fichier");
fclose(flot);
return NULL;
}
tabClient = (Client *)malloc(sizeof(Client));
if (tabClient == NULL)
{
printf("Problème malloc\n");
fclose(flot);
return NULL;
}
while (!feof(flot))
{
if (i == tMax)
{
tabClient = (Client *)realloc(tabClient, tMax * sizeof(Client) + 1 * sizeof(Client));
tMax = tMax + 1;
}
tabClient[i] = saisirClient(flot);
i = i + 1;
}
*nbClient = i;
// afficherTabClient(tabClient,i);
fclose(flot);
return tabClient;
}
/*-------------------------------------------- Tri dico --------------------------------------------------------*/
int testPosClient(Client R, Client S, int choix)
{
switch (choix)
{
case 1:
return R.idClient - S.idClient;
break;
case 2:
return strcmp(R.nom, S.nom);
break;
case 3:
return strcmp(R.prenom, S.prenom);
break;
default:
break;
}
}
void copierClient(Client tab[], int i, int j, Client R[])
{
int t = 0;
while (i < j)
{
R[t] = tab[i];
i = i + 1;
t = t + 1;
}
}
void fusionClient(Client R[], int n, Client S[], int m, Client t[], int trieID)
{
int i = 0, j = 0, k = 0, testOrdre;
while (i < n && j < m)
{
if (testPosClient(R[i], S[j], trieID) < 0)
{
t[k] = R[i];
i = i + 1;
k = k + 1;
}
else
{
t[k] = S[j];
j = j + 1;
k = k + 1;
}
}
while (i < n)
{
t[k] = R[i];
i = i + 1;
k = k + 1;
}
while (j < m)
{
t[k] = S[j];
j = j + 1;
k = k + 1;
}
}
void triDicoClient(Client tab[], int n, int trieID)
{
Client *R, *S;
if (n == 1)
return;
R = (Client *)malloc((n / 2) * sizeof(Client));
S = (Client *)malloc((n - (n / 2)) * sizeof(Client));
if (R == NULL || S == NULL)
{
printf("Problème malloc");
return;
}
copierClient(tab, 0, n / 2, R);
copierClient(tab, n / 2, n, S);
triDicoClient(R, n / 2, trieID);
triDicoClient(S, n - n / 2, trieID);
fusionClient(R, n / 2, S, n - (n / 2), tab, trieID);
free(R);
free(S);
}
/*------------------------ Recherche dico ------------------------*/
int rechercherDicoClient(char *rechnom, Client tab[], int nbc, int *trouve, int rechID)
{
if(rechID==-1)
{
triDicoClient(tab,nbc,2);
}
else
{
triDicoClient(tab,nbc,1);
}
int inf = 0, sup = nbc - 1, m;
while (inf <= sup)
{
m = (inf + sup) / 2;
if (rechID == -1)
{
if ((strcmp(rechnom, tab[m].nom)) == 0)
{
*trouve = 1;
return m;
}
if ((strcmp(rechnom, tab[m].nom)) < 0)
sup = m - 1;
else
inf = m + 1;
}
else
{
if (rechID == tab[m].idClient)
{
*trouve = 1;
return m;
}
if (rechID < tab[m].idClient)
sup = m - 1;
else
inf = m + 1;
}
}
*trouve = 0;
return inf;
}
/*------------------------SupprimerUnClient------------------------*/
int supprimeClient(Client *tabClient, int nb)
{
char rechdesig_art[50];
int pos, trouve, i;
printf("Saisir le nom du client : \t");
scanf("%s%*c", rechdesig_art);
pos = rechercherDicoClient(rechdesig_art, tabClient, nb, &trouve, -1);
if (trouve == 0)
{
printf("Client non trouvé \n");
return nb;
}
for (i = pos; i < nb; i++)
tabClient[i] = tabClient[i + 1];
tabClient = (Client *)realloc(tabClient, nb * sizeof(Client) - 1 * sizeof(Client));
nb = nb - 1;
printf("Client Supprimmé.\n");
return nb;
}
/*------------------------Ajouter un client------------------------*/
Client saisieClient(int nb)
{
Client c;
printf("Civilité de nouveau client (Mr ou Mme): ");
scanf("%s%*c", c.civilite);
while(strcmp(c.civilite,"Mr")!=0 && strcmp(c.civilite,"Mme")!=0 ){
printf("Civilité de nouveau client (Mr ou Mme): ");
scanf("%s%*c", c.civilite);
}
printf("Nom de nouveau client : ");
scanf("%s%*c", c.nom);
printf("Prénom de nouveau client: ");
scanf("%s%*c", c.prenom);
printf("Saisir l'adresse de nouveau client :");
fgets(c.adresse, 50, stdin);
c.adresse[strlen(c.adresse) - 1] = '\0';
c.commandesEnAttente=ensembleVide();
c.commandesEnCours=ensembleVide();
c.idClient = nb + 1;
return c;
}
Client * ajouterClient(Client tabClient[], int *nbClient)
{
Client a;
int trouve, pos;
triDicoClient(tabClient, *nbClient, 1);
a = saisieClient(tabClient[*nbClient - 1].idClient);
triDicoClient(tabClient, *nbClient, 2);
pos = rechercherDicoClient(a.nom, tabClient, *nbClient, &trouve, -1);
if (trouve == 1)
{
printf("Erreur: Client déjà enregistré \n");
return tabClient;
}
tabClient = (Client *)realloc(tabClient, *nbClient * sizeof(Client) + 1 * sizeof(Client));
tabClient[*nbClient] = a;
*nbClient = *nbClient + 1;
triDicoClient(tabClient, *nbClient, 2);
printf("Ajout du client réussi.\n");
return tabClient;
}
/*------------------------Consulter un client------------------------*/
void consulterClient(Client tabClient[],int nbClient)
{
char rechdesig_art[50];
int pos, trouve, i,choix;
printf("Saisir le nom du client : \t");
scanf("%s%*c", rechdesig_art);
triDicoClient(tabClient,nbClient,2);
pos = rechercherDicoClient(rechdesig_art, tabClient, nbClient, &trouve, -1);
afficherConsultClient(tabClient[pos]);
printf("Appuyer sur ← ou → pour naviguer\n");
printf("Appuyer sur entrer pour sortir\n");
while (choix != 10)
{
choix = getch();
if (choix == 68)
{
pos = pos - 1;
}
else if (choix == 67)
{
pos = pos + 1;
}
if(pos==-1)
{
pos=nbClient-1;
}
if(pos==nbClient)
{
pos=0;
}
afficherConsultClient(tabClient[pos]);
printf("Appuyer sur ← ou → pour naviguer\n");
printf("Appuyer sur entrer pour sortir\n");
}
}
/*-------------------------------------------- Sauvegarder un client -------------------------------------------------------*/
void sauvegardeClient(Client c,FILE * flot){
fprintf(flot,"\n%d %s %s %s %s",c.idClient,c.civilite,c.nom,c.prenom,c.adresse);
}
void sauvegardeTabClient(Client tab[],int tmax){
int i;
FILE *flot;
flot = fopen(nomFichClient, "w");
if (flot == NULL)
{
printf("Problème d'ouverture du fichier");
return;
}
for(i=0;i<tmax;i++){
sauvegardeClient(tab[i],flot);
}
fclose(flot);
}
/*-------------------------------------------- Modification client -------------------------------------------------------*/
Client * modification(Client *tabClient,int taille)
{
Client c;
int id,pos,trouve;
char choix;
printf("Veuillez entrer le nom d'un client :");
fgets(c.nom,10,stdin);
c.nom[strlen(c.nom)-1]='\0';
pos=rechercherDicoClient(c.nom,tabClient,taille,&trouve,-1);
while(trouve==0)
{
printf("Client non enregistré\n");
return tabClient ;
}
printf("\nVoici le client %s %s :\n",c.nom,c.prenom);
afficherConsultClient(tabClient[pos]);
printf("\nConfirmez vous que vous souhaitez modifier ce client?(O/N)");
scanf("%c%*c",&choix);
if(choix=='O'||choix=='o')
{
c=tabClient[pos];
printf("Voulez vous modifier la civilité ? (O/N)\n");
scanf("%c%*c",&choix);
if (choix=='o' || choix=='O')
{
printf("Saisir la nouvelle civilité\t");
scanf("%s%*c",c.civilite);
while(strcmp(c.civilite,"Mr")!=0 && strcmp(c.civilite,"mr")!=0 && strcmp(c.civilite,"Mme")!=0 && strcmp(c.civilite,"mme")!=0)
{
printf("Civilité inconnue, veuillez entrer de nouveau la civilité (Mr ou Mme) :\t");
scanf("%s%*c",c.civilite);
}
}
printf("Voulez vous modifier le nom ? (O/N)\n");
scanf("%c%*c",&choix);
if (choix=='o' || choix=='O')
{
printf("Saisir le nouveau nom\t");
fgets(c.nom,10,stdin);
c.nom[strlen(c.nom)-1]='\0';
}
printf("Voulez vous modifier le prénom ? (O/N)\n");
scanf("%c%*c",&choix);
if (choix=='o' || choix=='O')
{
printf("Saisir le nouveau prénom\t");
fgets(c.prenom,10,stdin);
c.prenom[strlen(c.prenom)-1]='\0';
}
printf("Voulez vous modifier l'adresse ? (O/N)\n");
scanf("%c%*c",&choix);
if (choix=='o' || choix=='O')
{
printf("Saisir l'adresse'\t");
fgets(c.adresse,70,stdin);
c.adresse[strlen(c.adresse)-1]='\0';
}
tabClient[pos]=c;
return tabClient;
}
printf("vous n'avez pas modifié le client %s %s\n",c.nom,c.prenom);
return tabClient;
}