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 97 98 99 100 101 102 103 104
| #include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct treeNode
{
char * data;
char operation;
struct treeNode *left;
struct treeNode *right;
} treeNode;
treeNode *creerNoeud(char op, char * val, treeNode * gauche, treeNode * droite){
treeNode *n = malloc(sizeof(treeNode *));
n->left = gauche;
n->right= droite;
n->data = val;
n->operation=op;
return n;
}
char *lirechaine(){
int tableauSize = 5;
int i=0;
char *tableau= malloc(tableauSize);
printf("donner une expression: ");
char c;
while((c=getchar()) != '\n')
{
tableau[i++]=c;
if(i>=tableauSize){
tableau = realloc(tableau, tableauSize*=2);
if(tableau == 0){
exit(0);
}
}
}
tableau[i]='\0';
return tableau;
}
treeNode *creerASA(char *tab){
char *data = malloc(10);
treeNode *left = malloc(sizeof(treeNode*));
treeNode *right = malloc(sizeof(treeNode*));
treeNode **arbrePile = malloc(10*sizeof(treeNode));
char c;
int j=0; int i = 0; int k = 0;
boucle:
while((c=tab[i++])!='\n'){
if(c>='0' && c<='9'){
data[j++] = c;
}
else if(c=='/' || c=='+'){
if(k>=2){
right = arbrePile[--k];
left = arbrePile[--k];
arbrePile[k++] = creerNoeud(c, NULL, left, right);
}else{
exit(0);
}
}
else if(c=' ' && j!=0 ){
data[j]='\0';
j=0;
arbrePile[k++] = creerNoeud(0, data, NULL, NULL);
data = malloc(10);
}
else if(c= ' ' && tab[i+1]!=EOF){
goto fin;
}
else if((c=tab[i++])=='\n')
exit(0);
else{
exit(0);
}
}
if(k!=1) exit(0);
fin:
return arbrePile[0];
}
void Infixe(treeNode *Node){
if(Node!=NULL){
Infixe(Node->left);
printf("%d", Node->data);
Infixe(Node->right);
}
return;
}
int main()
{
char *tab;
char *tableau =tableau+ '\n';
tableau = lirechaine();
//tableau = "66 33 / 9 10 +'\n'";
treeNode *racine = creerASA(tableau);
Infixe(creerASA(tableau));
// printf("nkfgk");
} |
Partager