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
| #include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct Tree {
char name[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->name, Item->name)<0)
{
addItem(root->left, root, 0, Item);
}
else
if (strcmp(root->name, Item->name)>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->name, 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)
{
fparcoursrbre(file, root->left);
fprintf(file, "%s %s \r\n", root->name, 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;
} |
Partager