#include <stdio.h> #include <stdlib.h> #include <string.h> struct Tree { char nom[32]; char number[32]; struct tree * left; struct tree * right; }* root; void addItem(struct Tree *root, struct Tree *Parain, int bDir, struct Tree *Item) { if (root) { if(strcmp(root->nom, Item->nom)<0) { addItem(root->left, root, 0, Item); } else if (strcmp(root->nom, Item->nom)>0) { addItem(root->right, root, 1, Item); } else if (strcmp(root->number, Item->number)<0) { addItem(root->left, root, 0, Item); } else { addItem(root->right, root, 1, Item); } } else { if (bDir) Parain->right=Item; else Parain->left=Item; } } int fCreerArbre (FILE* file) { char sBuf[128]; struct Tree * Item; while (fgets(sBuf, 128, file)) { Item=(struct Tree*)malloc(sizeof(struct Tree)); sscanf(sBuf, "%s[31] %s[31]", Item->nom, Item->number); Item->left=NULL; Item->right=NULL; if (root) addItem(root, root, 0, Item); else root=Item; } } int fparcoursArbre(FILE *file, struct Tree *root) { if(root) { fparcoursArbre(file, root->left); fprintf(file, "%s %s \r\n", root->nom, root->number); fparcoursArbre(file, root->right); } } int main (int nargs, char ** args) { char * sInputFile; char * sOutputFile; FILE * fInputFile; FILE * fOutputFile; sInputFile=(char *)malloc(strlen(args[1])*sizeof(char)+1); strcpy(sInputFile, args[1]); sInputFile [strlen(args[1])]; sOutputFile=(char *)malloc(strlen(args[1])*sizeof(char)+1); strcpy(sOutputFile, args[1]); sOutputFile [strlen(args[1])]; fInputFile=fopen(sInputFile, "r+"); fOutputFile=fopen(sOutputFile, "w+"); fCreerArbre(fInputFile); fparcoursArbre(fOutputFile, root); fclose(fInputFile); fclose(fOutputFile); return 0; }
Vous n’avez pas trouvé la réponse que vous recherchez ?
Posez votre question