Bonjour à tous, voila j'ai un problème, je dois faire un tp sur les liste chainée générique mais je n'y arrive pas. Il faut que je transforme le fichier element.c
et element.h
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7 #include <cstdio> #include "element.H" void afficheElement(const Element & e) { std::printf("%d ",e); }en mettant definissant Element de la manière suivante : typedef void * Element et en prévoyant des fonction d'initialisation, d'affectation et de testament
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8 #ifndef _ELEMENT #define _ELEMENT typedef int Element; void afficheElement(const Element & e); #endif
voila ce que j'ai fait: element.het element.c
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 #ifndef _ELEMENT #define _ELEMENT typedef void * Element; void afficheElement(Element& e, void (*affiche)(Element &)); void compareElt(Element e,Element b,void(*compare)(Element ,Element)); int * initialisationInt(Element & a,Element b); void affichageInt(Element & e); void affectationInt ( Element & a,Element b); void testamentInt (Element & e); void compareInt(Element a,Element b); double * initialisationDouble (Element & a, double b); void affichageDouble (Element & e); void affectationDouble ( Element & a,Element b); void testamentDouble (Element & e); void compareDouble(Element a,Element b); #endif
Je voulais savoir si c'était bon et si il fallait initialiser comme ma fonction initialiseDouble ou comme ma fonction initialiseInt?
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 #include <iostream> #include <stdio.h> #include "element.h" using namespace std; void afficheElement(Element & e,void(*affiche)(Element &)) { affiche(e); } int * initialisationInt(Element & a, Element b) { a= new (int*); a= b; return a; } double* initialisationDouble (Element & a,double b) { a= new (double*); *(reinterpret_cast<double*>(a))=b; } void affectationInt ( Element & a,Element b) { if (a!=b) { *(reinterpret_cast<int*>(a)) = *(reinterpret_cast<int*>(b)) ; } } void affectationDouble ( Element & a,Element b) { if (a!=b) { *(reinterpret_cast<double*>(a)) = *(reinterpret_cast<double*>(b)) ; } } void affichageInt(Element & e) { printf("%d", *(reinterpret_cast<int*>(e))); } void affichageDouble (Element & e) { printf("%f",*(reinterpret_cast<double*>(e))); } void compareInt(Element a,Element b) { if (*(reinterpret_cast<int*>(b)) < *(reinterpret_cast<int*>(a))) {cout<<"le plus petit est"<< *(reinterpret_cast<int*>(b));} else cout<<"le plus petit est"<< *(reinterpret_cast<int*>(a)); } void compareDouble(Element a,Element b) { if (*(reinterpret_cast<double*>(a)) > *(reinterpret_cast<double*>(b))) cout<<"le plus petit est"<< *(reinterpret_cast<double*>(b)); else cout<<"le plus petit est"<< *(reinterpret_cast<double*>(a)); } void compareElt(Element e,Element b,void(*compare)(Element ,Element)) { compare(e,b); } void testamentInt (Element & e) { delete reinterpret_cast<int*>(e); } void testamentDouble (Element & e) { delete reinterpret_cast<double*>(e); }
Voila merci
Partager