1 #include <stdio.
h>
2 #include <malloc.h>
3 #include <string.h>
4 #include <stdlib.h>
5 //------------------------------------- Les
structures
--------------------------------------------------------
--------------------------------------------------------
-----
6 struct data_article
7 {
8 int code;
9 char des[15];
10 int code_fa;
11 float prix;
12 int code_fo;
13 int qte;
14 struct data_article *next;
15 };typedef struct data_article article;
16
//------------------------------------------------------
--------------------------------------------------------
---------------
17
18 struct data_famille
19 {
20 char des[15];
21 int code_fa;
22 char type[2];
23 struct data_famille *next;
24 };
25 typedef struct data_famille famille;
26
//------------------------------------------------------
--------------------------------------------------------
----------------
27
28 struct data_fournisseur
29 {
30 char raison[15];
31 int code_fo;
32 struct data_fournisseur *next;
33 };
34 typedef struct data_fournisseur fournisseur;
35
//------------------------------------------------------
--------------------------------------------------------
-----------------
36
37 struct data_procede
38 {
39 int qte;
40 int code_composant;
41 struct data_procede *fils;
42 struct data_procede *frere;
43 };
44 typedef struct data_procede procede;
45
//------------------------------------------------------
--------------------------------------------------------
-----------------------
46
47 struct Mat
48 {
49 int code_mat;
50 int besoin;
51 struct Mat *next;
52 };
53 typedef struct Mat mat_pre;
54
//------------------------------------------------------
--------------------------------------------------------
----------------------------
55
56 struct pro
57 {
58 int code;
59 int qte;
60 struct pro *next;
61 };
62 typedef struct pro produit;
63 //----------------------------------QUESTION
1-------------------------------------------------------
----------------------------------------
64
65 float re_prix(article*first,int code){
66 if(first){
67 if(first->code==code){
68 return(first->prix);
69 }
70 else
71 return(re_prix(first->next,code));
72 }
73 }
74
75 void afficher(procede*racine,article*liste){
76 float prix;
77 if(racine)
78 {
79 prix=re_prix(liste,racine->code_composant);
80 printf(" \n %d ",racine->qte);
81 afficher(racine->frere,liste);
82 }
83 }
84
85
86 void chercher_com(procede* racine,article* liste,
int code){
87 if(racine){
88 if((racine->code_composant)==code){
89 afficher(racine->fils,liste);
90 }
91 else{
92 chercher_com(racine->frere,liste,code);
93 chercher_com(racine->fils,liste,code);
94 }
95 }
96
97 }
98
//------------------------------------------------------
--------------------------------------------------------
-------------------------------------------
99
100 //-----------------------------------------QUESTION
2-------------------------------------------------------
----------------------------------------------------
101 int est_PF(int code,famille*first,char*des)
102 {
103 if(first)
104 {
105 if((first->code_fa==code)&& (!strcmp(first
->type,"PF")))
106 {
107 strcpy(des,first->des);
108 return(1);
109 }
110 else{
111 return(est_PF(code,first->next,des));
}
112 }
113 else return(0);
114 }
115
116
117 void affiche_produit(article*first,famille*liste,
procede*racine){
118 char des[16];
119 if(first){
120 if(est_PF(first->code_fa,liste,des)){
121 printf(" \n %s : %f",des,first->prix);
122 affiche_produit(first->next,liste,racine);
123 }
124 else{
125 affiche_produit(first->next,liste,racine);
126 }
127 }
128
129 }
130
//------------------------------------------------------
--------------------------------------------------------
--------------------------------
131
132 //-----------------------------------QUESTION
3-------------------------------------------------------
-----------------------------------------------
133
134 procede *seek_noeud(procede*racine,int code){
135 procede*p;
136 if(racine){
137 if(racine->code_composant==code){
138 return(racine);
139 }
140 else{
141 if(p=seek_noeud(racine->fils,code))
142 return(p);
143 return(seek_noeud(racine->frere,code));
144 }
145 }
146 else return(NULL);
147 }
148
149 int seek_codefam(int code,article*first){
150 if(first)
151 {
152 if(first->code==code){
153 return(first->code_fa);
154 }
155 else
156 return(seek_codefam(code,first->next));
157 }
158
159 }
160
161 int est_MP(int code,famille*first)
162 {
163 if(first)
164 {
165 if((first->code_fa==code)&& (!strcmp(first
->type,"MP")))
166 {
167 return(1);
168 }
169 else{
170 return(est_MP(code,first->next));
}
171 }
172 else return(0);
173 }
174
175 int r_forni(famille*liste,procede*racine,article*
liste_arc){
176 int v,codefam;
177 if(racine){
178 codefam=seek_codefam(racine->code_composant,
liste_arc);
179 if(est_MP(codefam,liste)){
180 return(1);
181 }
182 else
183 {
184 if(v=r_forni(liste,racine->fils,liste_arc))
185 return(v);
186 return(r_forni(liste,racine->frere,liste_arc
));
187 }
188 }
189 else
190 return(0);
191 }
192
193 int forni(int code,famille*liste,procede*racine,
article*liste_arc){
194 procede *p;
195 int v;
196 if(p=seek_noeud(racine,code))
197 {
198 return(r_forni(liste,p->fils,liste_arc));
199
200 }
201 else return(0);
202 }
203
204 int nombre_arc(int code,article*first,famille*liste
,procede*racine,article*liste_arc){
205 if(first)
206 {
207 if((first->code_fo)==code)
208 {
209 if( (est_MP(first->code_fa,liste)) || (
forni(first->code,liste,racine,liste_arc)))
210 return(1+nombre_arc(code,first->next,
liste,racine,liste_arc));
211 else
212 return(nombre_arc(code,first->next,
liste,racine,liste_arc));
213 }
214 else
215 return(nombre_arc(code,first->next,liste,
racine,liste_arc));
216 }
217 else return(0);
218 }
219
220
221 void lister_four(fournisseur*first,article*
liste_arc,famille*liste,procede*racine){
222 int nb;
223 if(first){
224 nb=nombre_arc(first->code_fo,liste_arc,liste,
racine,liste_arc);
225 printf(" \n %d : %d \n ",first->code_fo,nb);
226 lister_four(first->next,liste_arc,liste,racine
);
227 }
228 }
229
230 //---------------------------------------QUESTION
4-------------------------------------------------------
--------------------------------------------------------
---------------
231 mat_pre*seek_mat(mat_pre*first,int code){
232 if(first){
233 if(first->code_mat==code)
234 return(first);
235 else
236 return(seek_mat(first->next,code));
237 }
238 }
239
240 void r_insere(mat_pre**mat,int code,int qte){
241 mat_pre*p,*nov;
242 if(p=seek_mat(*mat,code)){
243 p->besoin=p->besoin+qte;
244 }
245 else{
246 nov=(mat_pre*)malloc(sizeof(mat_pre));
247 nov->next=*mat;
248 nov->besoin=qte;
249 *mat=nov;
250 }
251
252 }
253
254 void insere_mat(mat_pre**mat,procede*first,article*
liste_arc,famille*fam,int qte){
255 int code;
256 while(first){
257 code=seek_codefam(first->code_composant
,liste_arc);
258 if( est_MP(code,fam)){
259 r_insere(mat,first->code_composant,qte
*(first->qte));
260 }
261 first=first->frere;
262 }
263 }
264
265 int qte_stock(int code,article*first){
266 if(first){
267 if(first->code==code){
268 return(first->qte);
269 }
270 else
271 return(qte_stock(code,first->next));
272 }
273 }
274
275 produit *seek_fils(procede*racine,int code){
276 produit*p;
277 if(racine){
278 if(racine->code_composant==code){
279 return(racine->fils);
280 }
281 else{
282 if(p=seek_fils(racine->fils,code))
283 return(p);
284 return(seek_fils(racine->frere,code));
285 }
286 }
287 else
288 return(NULL);
289 }
290
291 void faisaible(produit*first,famille*fam,article*
liste_arc,procede*p){
292 procede*fil;
293 mat_pre *mat;
294 int q;
295 mat=NULL;
296 while(first){
297 fil=seek_fils(p,first->code);
298 insere_mat(&mat,fil,liste_arc,fam,first->qte);
299 first=first->next;
300 }
301 while(mat){
302 q=qte_stock(mat->code_mat,liste_arc);
303 if((mat->besoin)>q)
304 printf(" Mat premier %d : Besoin %d : Manquant
%d \n",mat->code_mat,mat->besoin,(mat->besoin)-q);
305 mat=mat->next;
306 }
307
308 }
309
310 //-----------------------LOAD DES
FICHIERS------------------------------------------------
--------------------------------------------------------
-------------------------------------------------
311
312 article*load_liste1(FILE*fp){
313 article *nov,*first=NULL;
314 char s[100];
315 while(fgets(s,100,fp)){
316 nov=(article*)malloc(sizeof(article));
317 sscanf(s,"%5d%15s%5d%10f%5d%5d",&(nov->code),
nov->des,&(nov->code_fa),&(nov->prix),&(nov->code_fo),&(
nov->qte));
318 nov->next=first;
319 first=nov;
320 }
321 return(first);
322 }
323
324 famille*load_liste2(FILE*fp){
325 famille *nov,*first=NULL;
326 char s[100];
327 while(fgets(s,600,fp)){
328 nov=(famille*)malloc(sizeof(famille));
329 sscanf(s,"%5d%2s %s",&(nov->code_fa),nov->type,
nov->des);
330 nov->next=first;
331 first=nov;
332 }
333 return(first);
334 }
335
336 fournisseur*load_liste3(FILE*fp){
337 fournisseur *nov,*first=NULL;
338 char s[100];
339 while(fgets(s,600,fp)){
340 nov=(fournisseur*)malloc(sizeof(fournisseur));
341 sscanf(s,"%5d%15s",&(nov->code_fo),nov->raison
);
342 nov->next=first;
343 first=nov;
344 }
345 return(first);
346 }
347
348 void insere_noeud(procede**racine,int code,int
code_com,int qte){
349
350 procede *p,*nov;
351 nov=(procede*)malloc(sizeof(procede));
352 nov->fils=NULL;
353 nov->code_composant=code_com;
354 nov->qte=qte;
355 if((p=seek_noeud(*racine,code)) ){
356 nov->frere=p->fils;
357 p->fils=nov;
358
359 }
360 else{
361 nov->frere=*racine;
362 *racine=nov;
363 }
364 }
365
366 void insere_noeud_pere(procede**racine,int code){
367 procede *nov;
368 nov=(procede*)malloc(sizeof(procede));
369 nov->fils=NULL;
370 nov->code_composant=code;
371 nov->qte=1;
372 if(!(seek_noeud(*racine,code)) )
373 {
374 nov->frere=*racine;
375 *racine=nov;
376 }
377
378 }
379
380 procede* load_arb(FILE*fp){
381 procede *racine=NULL;
382 char s[100];
383 int code,code_com,qte;
384 while(fgets(s,600,fp))
385 {
386 sscanf(s,"%5d%5d%3d",&(code),&code_com,&qte);
387 insere_noeud_pere(&racine,code);
388 insere_noeud(&racine,code,code_com,qte);
389 }
390 return(racine);
391 }
392 void view_arb(procede*racine){
393 if(racine){
394 printf("\n %d",racine->code_composant);
395 view_arb(racine->frere);
396 view_arb(racine->fils);
397 }
398 }
399
400 int lire_data(int*code,int*q){
401 int choix;
402 printf("insere 1 ou non 0 : ");
403 scanf("%d",&choix);
404 if(choix==1){
405 printf("\n insere code de produit:");
406 scanf("%d",code);
407 printf("\n insere qte de cette produit:");
408 scanf("%d",q);
409 return(1);
410 }
411 else
412 return(0);
413 }
414
415 produit *load_produit(produit**p){
416 int code,q;
417 produit*nov;
418 while(lire_data(&code,&q)){
419 nov=(produit*)malloc(sizeof(produit));
420 nov->next=*p;
421 nov->code=code;
422 nov->qte=q;
423 *p=nov;
424 }
425 return(*p);
426 }
427
//------------------------------------MAIN--------------
--------------------------------------------------------
--------------------------------------------
428 void main(){
429 FILE *f1,*f2,*f3,*f4;
430 article *liste_arc;
431 fournisseur *liste_four;
432 famille *liste_fami;
433 procede *racine;
434 produit*p;
435 int c;
436 p=NULL;
437 f1=fopen("article.txt","r");
438 f2=fopen("famille.txt","r");
439 f3=fopen("fournisseur.txt","r");
440 f4=fopen("procede.txt","r");
441 liste_arc=load_liste1(f1);
442 liste_fami=load_liste2(f2);
443 liste_four=load_liste3(f3);
444 racine=load_arb(f4);
445 view_arb(racine);
446 p=load_produit(&p);
447 faisaible(p,liste_fami,liste_arc,racine);
448 //printf("\n donner code de produit ");
449 //scanf("%d",&c);
450 //chercher_com(racine,liste_arc,c);
451 //affiche_produit(liste_arc,liste_fami,racine);
452
//lister_four(liste_four,liste_arc,liste_fami,racine);
453 fclose(f1);
454 fclose(f2);
455 fclose(f3);
456 fclose(f4);
457 }