Voici un exemple de "main.c" #include<stdio.h> #include<conio.h> int main() { ........ ....... if(cas == 1) //appel de votre solution //le résultat est doit être sauvegardé dans un fichier ou autre structure pour les utiliser après dans le main if (cas == 1 ) //comment appeler et adapter votre solution? if (cas == 2 ) //comment appeler et adapter votre solution? if (cas == 3 ) //comment appeler et adapter votre solution? if (cas == 4 ) //comment appeler et adapter votre solution? if (cas == 5 ) //comment appeler et adapter votre solution? ........... ............ return 0; }
<Galicia_Document> <Lattice numberObj="5" numberAtt="4" numberCpt="11"> <Name>lattice(essai)</Name> <Object>3</Object> <Object>2</Object> <Object>1</Object> <Object>5</Object> <Object>4</Object> d b c a <Concept> 1 <Extent> <Object_Ref>3</Object_Ref> <Object_Ref>2</Object_Ref> <Object_Ref>1</Object_Ref> <Object_Ref>5</Object_Ref> <Object_Ref>4</Object_Ref> </Extent> </Concept> <Concept> 2 <Extent> <Object_Ref>1</Object_Ref> <Object_Ref>5</Object_Ref> </Extent> a <Concept_Ref>1</Concept_Ref> </Concept> <Concept> 5 <Extent> <Object_Ref>2</Object_Ref> <Object_Ref>5</Object_Ref> <Object_Ref>4</Object_Ref> </Extent> b <Concept_Ref>1</Concept_Ref> </Concept> <Concept> 4 <Extent> <Object_Ref>3</Object_Ref> <Object_Ref>1</Object_Ref> <Object_Ref>4</Object_Ref> </Extent> c <Concept_Ref>1</Concept_Ref> </Concept> <Concept> 3 <Extent> <Object_Ref>3</Object_Ref> <Object_Ref>2</Object_Ref> <Object_Ref>5</Object_Ref> </Extent> d <Concept_Ref>1</Concept_Ref> </Concept> <Concept> 8 <Extent> <Object_Ref>1</Object_Ref> </Extent> c a <Concept_Ref>2</Concept_Ref> <Concept_Ref>4</Concept_Ref> </Concept> <Concept> 9 <Extent> <Object_Ref>4</Object_Ref> </Extent> b c <Concept_Ref>5</Concept_Ref> <Concept_Ref>4</Concept_Ref> </Concept> <Concept> 6 <Extent> <Object_Ref>2</Object_Ref> <Object_Ref>5</Object_Ref> </Extent> d b <Concept_Ref>5</Concept_Ref> <Concept_Ref>3</Concept_Ref> </Concept> <Concept> 7 <Extent> <Object_Ref>3</Object_Ref> </Extent> d c <Concept_Ref>4</Concept_Ref> <Concept_Ref>3</Concept_Ref> </Concept> <Concept> 10 <Extent> <Object_Ref>5</Object_Ref> </Extent> d b a <Concept_Ref>2</Concept_Ref> <Concept_Ref>6</Concept_Ref> </Concept> <Concept> 11 <Extent> </Extent> d b c a <Concept_Ref>10</Concept_Ref> <Concept_Ref>8</Concept_Ref> <Concept_Ref>9</Concept_Ref> <Concept_Ref>7</Concept_Ref> </Concept> </Lattice> </Galicia_Document>
Vous n’avez pas trouvé la réponse que vous recherchez ?
Posez votre question#include <stdlib.h> #include <string.h> #include <libxml/parser.h> #pragma comment(lib,"libxml2.lib") typedef struct { char* reference; void* next; } REF; typedef struct { char* ID; REF* references; REF* upperCovers; } CONCEPT ; CONCEPT* _concepts = 0; int _countCpt = 0; // Permet de compter les occurrences de <Concept> int _currentCpt = -1; // Permet de compter les occurrences de <Concept> int _inAttribute = 0; int _inConceptRef = 0; int _inID = 0; void startElement(void *user_data, const xmlChar *name, const xmlChar **attrs){ int i; // Si on est sur un Lattice... if (!xmlStrcmp(name, (xmlChar*)"Lattice") && attrs) { // On parcoure les attributs for (i = 0; attrs[i]; i+=2) { // Jusqu'à trouver l'atribut numberCpt if (!xmlStrcmp(attrs[i], (xmlChar*)"numberCpt")) { // Que l'on mémorise (strtol permet d'analyser la valeur texte, pour en faire une valeur numérique) _countCpt = strtol((const char*)attrs[i+1],NULL,0); // on créé un tableau qui va contenir les concepts. _concepts = (CONCEPT*)calloc(_countCpt, sizeof(CONCEPT)); break; // inutile de considérer les attributs suivants } } } else if (!xmlStrcmp(name, (xmlChar*)"Concept") && _countCpt) _currentCpt++; else if (!xmlStrcmp(name, (xmlChar*)"Concept_Ref") && _countCpt) _inConceptRef = 1; else if (!xmlStrcmp(name, (xmlChar*)"Attribute_Ref")) _inAttribute = 1; // On souhaite mémoriser les Attribute_Ref else if (!xmlStrcmp(name, (xmlChar*)"ID")) _inID = 1; // On souhaite mémoriser les Attribute_Ref } void characters(void* user_data, const xmlChar *ch, int len) { if (_inID && len) { _concepts[_currentCpt].ID = (char*)malloc(len+1); strncpy(_concepts[_currentCpt].ID, ch, len); *(_concepts[_currentCpt].ID+len) = 0; _inID = 0; } else if ((_inAttribute || _inConceptRef) && len) { // lien vers les references du concept courant REF** source; REF* ref; REF* newRef; if (_inAttribute) source = &(_concepts[_currentCpt].references); else source = &(_concepts[_currentCpt].upperCovers); ref = *source; // on créé une nouvelle reference newRef = (REF*)calloc(1, sizeof(REF)); // permet de relier la nouvelle reference a la structure (liste chainee) if (ref) { while(ref->next) ref = (REF*)ref->next; ref->next = newRef; } else *source = newRef; // on recopie le contenu de la reference newRef->reference = (char*)malloc(len+1); strncpy(newRef->reference, ch, len); *(newRef->reference+len) = 0; _inAttribute = 0; _inConceptRef = 0; } } // Permet de désallouer proporement la structure void cleanup() { int i; REF* ref; for (i=0; i<_countCpt; i++) { free(_concepts[i].ID); ref = _concepts[i].references; while (ref) { free(ref->reference); ref = ref->next; } ref = _concepts[i].upperCovers; while (ref) { free(ref->reference); ref = ref->next; } } free(_concepts); } int main() { REF* ref; REF* index; int i; // structure permettant de renseigner les handlers perso à invoquer pour nos evenements de lecture xmlSAXHandler sh = { 0 }; sh.startElement = startElement; sh.characters = characters; // On appelle l'enalyse du fichier XML if(xmlSAXUserParseFile(&sh, NULL, "Input.xml")) { printf("Un erreur est survenue. Le fichier XML est peut-être mal consitué"); return -1; } printf("\nCas 1, 2 et 3 :\n"); ref = _concepts[_countCpt-1].references; while (ref) { printf("%s ", ref->reference); ref = ref->next; } printf("\n"); printf("\nCas 4 :\n"); index = _concepts[_countCpt-1].upperCovers; while (index) { printf("%s ", index->reference); for (i=0; i<_countCpt; i++) { if (strcmp(index->reference, _concepts[i].ID)==0) { ref = _concepts[i].references; if (ref) { while (ref) { if (ref->reference) printf("%s ", ref->reference); ref = ref->next; } } } } printf("\n"); index = index->next; } printf("\nCas 5 :\n"); for (i=0; i<_countCpt; i++) { ref = _concepts[i].references; if (ref) { while (ref) { if (ref->reference) printf("%s ", ref->reference); ref = ref->next; } printf("\n"); } } system("pause"); cleanup(); return 0; }
Cas 1, 2 et 3 :
d b c a
Cas 4 :
10
8
9
7
Cas 5 :
a
b
c
d
c a
b c
d b
d c
d b a
d b c a
Appuyez sur une touche pour continuer...
Cas 1, 2 et 3 :
d b c a
Cas 4 :
5 b d a
1 c a
4 b c
3 d c
Cas 5 :
a
b
c
d
c a
b c
d b
d c
d b a
d b c a
Appuyez sur une touche pour continuer...
<Galicia_Document> <Lattice numberObj="5" numberAtt="4" numberCpt="11"> <Name>lattice(input)</Name> <Object>3</Object> <Object>2</Object> <Object>1</Object> <Object>5</Object> <Object>4</Object> d b c a <Concept> 1 <Extent> <Object_Ref>3</Object_Ref> <Object_Ref>2</Object_Ref> <Object_Ref>1</Object_Ref> <Object_Ref>5</Object_Ref> <Object_Ref>4</Object_Ref> </Extent> </Concept> <Concept> 2 <Extent> <Object_Ref>1</Object_Ref> <Object_Ref>5</Object_Ref> </Extent> a <Concept_Ref>1</Concept_Ref> </Concept> <Concept> 5 <Extent> <Object_Ref>2</Object_Ref> <Object_Ref>5</Object_Ref> <Object_Ref>4</Object_Ref> </Extent> b <Concept_Ref>1</Concept_Ref> </Concept> <Concept> 4 <Extent> <Object_Ref>3</Object_Ref> <Object_Ref>1</Object_Ref> <Object_Ref>4</Object_Ref> </Extent> c <Concept_Ref>1</Concept_Ref> </Concept> <Concept> 3 <Extent> <Object_Ref>3</Object_Ref> <Object_Ref>2</Object_Ref> <Object_Ref>5</Object_Ref> </Extent> d <Concept_Ref>1</Concept_Ref> </Concept> <Concept> 8 <Extent> <Object_Ref>1</Object_Ref> </Extent> c a <Concept_Ref>2</Concept_Ref> <Concept_Ref>4</Concept_Ref> </Concept> <Concept> 9 <Extent> <Object_Ref>4</Object_Ref> </Extent> b c <Concept_Ref>5</Concept_Ref> <Concept_Ref>4</Concept_Ref> </Concept> <Concept> 6 <Extent> <Object_Ref>2</Object_Ref> <Object_Ref>5</Object_Ref> </Extent> d b <Concept_Ref>5</Concept_Ref> <Concept_Ref>3</Concept_Ref> </Concept> <Concept> 7 <Extent> <Object_Ref>3</Object_Ref> </Extent> d c <Concept_Ref>4</Concept_Ref> <Concept_Ref>3</Concept_Ref> </Concept> <Concept> 10 <Extent> <Object_Ref>5</Object_Ref> </Extent> d b a <Concept_Ref>2</Concept_Ref> <Concept_Ref>6</Concept_Ref> </Concept> <Concept> 11 <Extent> </Extent> d b c a <Concept_Ref>10</Concept_Ref> <Concept_Ref>8</Concept_Ref> <Concept_Ref>9</Concept_Ref> <Concept_Ref>7</Concept_Ref> </Concept> </Lattice> </Galicia_Document>
#include <stdlib.h> #include <string.h> #include <libxml/parser.h> #pragma comment(lib,"libxml2.lib") void DisplayConceptInfos(int, int); // Structure de base pour nos listes chainées. // Il s'agit en fait de stocker un nombre non défini de chaines de caractères typedef struct { char* reference; // la donnée proprement dite void* next; // pointeur vers la reference suivante (ou 0 s'il n'y a plus de donnée) } REF; // Structure décrivant les <Concept> typedef struct { char* ID; // Valeur de l'Id REF* intent; // Liste des de l' REF* upperCovers; // Liste des <Concept_Ref> de l' REF* extent; // Liste des <Object_Ref> de l'<extent> } CONCEPT ; CONCEPT* _concepts = NULL; // Tableau de <Concept> int _countCpt = 0; // Permet de compter les occurrences de <Concept> int _currentCpt = -1; int _inIntent = 0; // Si a 1, la prochaine donnée lue sera un et sera ajoutée dans la liste des int _inUpperCovers = 0; // idem pour l' int _inExtent = 0; // idem pour l'<extent> int _inID = 0; // Si a 1, la prochaine donnée lue sera l'ID du <Concept> void startElement(void *user_data, const xmlChar *name, const xmlChar **attrs){ int i; // Si on est sur un Lattice... if (!xmlStrcmp(name, (xmlChar*)"Lattice") && attrs) { // On parcoure les attributs for (i = 0; attrs[i]; i+=2) { // Jusqu'à trouver l'atribut numberCpt if (!xmlStrcmp(attrs[i], (xmlChar*)"numberCpt")) { // Que l'on mémorise (strtol permet d'analyser la valeur texte, pour en faire une valeur numérique) _countCpt = strtol((const char*)attrs[i+1],NULL,0); // on alloue un tableau qui va contenir les concepts. _concepts = (CONCEPT*)calloc(_countCpt, sizeof(CONCEPT)); break; // inutile de considérer les attributs suivants } } } else if (!xmlStrcmp(name, (xmlChar*)"Concept") && _countCpt) _currentCpt++; else if (!xmlStrcmp(name, (xmlChar*)"Concept_Ref") && _countCpt) _inUpperCovers = 1; else if (!xmlStrcmp(name, (xmlChar*)"Attribute_Ref")) _inIntent = 1; else if (!xmlStrcmp(name, (xmlChar*)"Object_Ref")) _inExtent = 1; else if (!xmlStrcmp(name, (xmlChar*)"ID")) _inID = 1; } void characters(void* user_data, const xmlChar *ch, int len) { if (_inID && len) { // On va enregistrer l'ID, mais en supprimant les espaces qui semblent s'être introduits 5 while(len>1 && *ch ==32) { len--; ch+=sizeof(xmlChar); } // On supprime également les espaces qui suivent la valeur. while(len>1 && *(ch+len-1)==32) len--; _concepts[_currentCpt].ID = (char*)malloc(len+1); strncpy(_concepts[_currentCpt].ID, ch, len); *(_concepts[_currentCpt].ID+len) = 0; _inID = 0; } else if ((_inIntent || _inUpperCovers || _inExtent) && len) { // lien vers les references du concept courant REF** source; REF* ref; REF* newRef; if (_inIntent) source = &(_concepts[_currentCpt].intent); else if (_inUpperCovers) source = &(_concepts[_currentCpt].upperCovers); else if (_inExtent) source = &(_concepts[_currentCpt].extent); ref = *source; // on créé une nouvelle reference newRef = (REF*)calloc(1, sizeof(REF)); // permet de relier la nouvelle reference a la structure (liste chainee) if (ref) { // La liste contient au moins une cellule. // On va parcourir la liste a la recherche de la dernière cellule (celle qui à le pointeur next à NULL) while(ref->next) ref = (REF*)ref->next; ref->next = newRef; } else *source = newRef; // Première cellule de la liste chainée // on recopie la donnée newRef->reference = (char*)malloc(len+1); strncpy(newRef->reference, ch, len); // 0 terminal de la chaine de caractères *(newRef->reference+len) = 0; // Remise à 0 des flags de position _inIntent _inUpperCovers _inExtent = 0; } } // Permet de désallouer proprement la structure void cleanup() { int i; REF* ref; for (i=0; i<_countCpt; i++) { // On libère donc une a une les zones mémoire allouées free(_concepts[i].ID); // On néttoie ensuite les listes chainées, une à une. ref = _concepts[i].intent; while (ref) { free(ref->reference); ref = ref->next; } ref = _concepts[i].upperCovers; while (ref) { free(ref->reference); ref = ref->next; } ref = _concepts[i].extent; while (ref) { free(ref->reference); ref = ref->next; } } // Avant de désallouer le tableau de Concepts free(_concepts); } int main() { REF* index; int i; // structure permettant de renseigner les handlers perso à invoquer pour nos evenements de lecture xmlSAXHandler sh = { 0 }; sh.startElement = startElement; sh.characters = characters; // On appelle l'enalyse du fichier XML if(xmlSAXUserParseFile(&sh, NULL, "Input.xml")) { printf("Un erreur est survenue. Le fichier XML est peut-être mal consitué"); return -1; } printf("\nCas 1, 2 et 3 :\n"); DisplayConceptInfos(_countCpt-1, 1); printf("\n"); printf("\nCas 4 :\n"); index = _concepts[_countCpt-1].upperCovers; while (index) { for (i=0; i<_countCpt; i++) { if (strcmp(index->reference, _concepts[i].ID)==0) DisplayConceptInfos(i, 1); } index = index->next; } printf("\nCas 5 :\n"); for (i=0; i<_countCpt; i++) DisplayConceptInfos(i, 0); system("pause"); cleanup(); return 0; } // Procédure servant d'exemple // Permet d'afficher les détails concernant un Concept // index: indice du concept a afficher // showExtent: indique si l'on doit ou non afficher les <Extent> void DisplayConceptInfos(int index, int showExtent) { REF* ref; // On valide la valeur de l'index if (index>0 && index<_countCpt) { if (showExtent) { ref = _concepts[index].extent; if (ref) { while (ref) { if (ref->reference) printf("%s ", ref->reference); ref = ref->next; } printf(": "); } } ref = _concepts[index].intent; while (ref) { if (ref->reference) printf("%s ", ref->reference); ref = ref->next; } printf("\n"); } }
ah... quand je pense qu'a la base tu demandais un exemple de parsing via SAX...
system("pause");
DisplayConceptInfos(i, 0);}
void DisplayConceptInfos(int index, int showExtent) { REF* ref; ......... ......... }
void DisplayConceptInfos(int, int);
xmlSAXHandler sh = { 0 }; sh.startElement = startElement; sh.characters = characters;
FILE *fp; fp = fopen("resultat.txt","w"); fprintf(fp,............); fclose(fp)
int main() { double debut,end; .............. .............. debut = clock(); //traitements .......... .......... fin = clock(); fprintf(stderr, "\ntemps : %f\n", (double)(fin-debut) / (double) CLOCKS_PER_SEC); return 0; }
system("pause"); on peut ne pas mettre cette instruction ? car j'ai le message sur la console sans faire "pause"
a toi de voir...
l'ouverture du fichier dans le Main, je dirai, passage du fp à DisplayConceptInfos qui lui utilisera fprintf