Bonjour,

j'aurais besoin d'aide pour savoir ce qui ne va pas dans mon code car j'ai un seg fault que je n'arrive pas à résoudre.

MERCI

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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
 
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <float.h>
#include <string.h>
 
 
 
enum Type 
  {
    Operateur, 
    Constante, 
    Variable
  };
 
 
union Info  
{
  char *nom;
  double cte;
  char op;
};
 
 
typedef struct noeud{
  enum Type type;
  union Info info;
  struct noeud *gauche;
  struct noeud *droit;
}Noeud,*Arbre;
 
 
void espace (char **ligne)
{
  while(**ligne == ' ') (*ligne)++;
}
 
 
int EstDouble(char *ligne, double *val)
{
  double v;
  char *endptr = NULL;
  v = strtod(ligne,&endptr);
 
  /* la chaine ne contient pas que des chiffres */
  if(*endptr != '\0')
    {
      fprintf(stderr,"le chiffre n'est pas correctement ecrit\n");
      return 1;
    }
 
  /* verification si debordements */
  if(v<=(-DBL_MAX) || v>=DBL_MAX)
    {
      fprintf(stderr,"debordement\n");
      return 1;
    }
 
  /* on recupere la valeur */
  *val = v;
 
  return 0;
}
 
 
 
 
 
/* Analyse d'une expression : creation de l'arbre */
Arbre CreerArbre(char **ligne)
{
  double val;
  char operateur;
  char *chiffre;
  char *nom;
 
  Arbre a = malloc(sizeof(Noeud));
  if(a == NULL)
    {
      fprintf(stderr,"erreur allocation de l'arbre\n"); 
      return NULL;
    }
 
  while(**ligne != '\0')
    {
      /* c'est un chiffre */
      if(isdigit(**ligne))
	{
	  chiffre = *ligne;
	  int i = 0;
	  while(isdigit(**ligne) || **ligne =='.')
	    {
	      chiffre[i] = **ligne;
	      (*ligne)++;
	      i++;
	    }
	  chiffre[i] = '\0';
 
	  if(**ligne != ' ' || **ligne != '\0')
	    return NULL;
 
	  if(EstDouble(chiffre,&val))
	    {	    
	      fprintf(stderr,"le chiffre : '%s' n'est pas correctement ecrit\n",chiffre);
	      return NULL;
	    }
 
	  a->type = Constante;
	  a->info.cte = val;
 
	  chiffre = NULL;
	  espace(ligne);
	}
 
      else
	{
	  /* c'est une lettre -> variable*/
	  if(isalpha(**ligne))
	    {
	      nom = *ligne;
	      int j = 0;
	      while(isalpha(**ligne))
		{
		  nom[j] = **ligne;
		  (*ligne)++;
		  j++;
		}
	      nom[j] = '\0';
 
	      if(**ligne != ' ' || **ligne != '\0')
		return NULL;
 
	      a->type = Variable;
	      a->info.nom = malloc(strlen(nom)+1);
	      if(a->info.nom == NULL)
		{
		  fprintf(stderr,"erreur allocation");
		  return NULL;
		}	
	      strcpy(a->info.nom,nom);       
 
	      nom = NULL;
	      espace(ligne);
	    }
 
	  else
	    {
	      /* le caractere est un operateur */
	      operateur = *((*ligne)++);
	      if(operateur == '+' || operateur  == '-' || 
		 operateur == '*' || operateur == '/')
		{
		  a->type = Operateur;
		  a->info.op = operateur;
		  a->gauche = CreerArbre(ligne);
		  a->droit = CreerArbre(ligne);
		}
 
	      else 
		{
		  if(operateur == '@' || operateur == '~')
		    {
		      a->type = Operateur;
		      a->info.op = operateur;
		      a->gauche = NULL;
		      a->droit = CreerArbre(ligne);
		    }
 
		  else 
		    printf("Erreur,'%c' n'est pas un operateur prevu\n",operateur);
		} 
	    } 
	}
    }
  return a;
}
 
 
 
 
void Infixe(Arbre a)
{
 
  switch(a->type)
    {
    case Constante:
      printf("%f",a->info.cte);
      break;
 
    case Variable:
      printf("%s",a->info.nom);
      break;
 
    case Operateur:
      switch(a->info.op)
	{
	  case'+':
	    case'-':/* affichage infixe sans () */
	    Infixe(a->gauche);
	  if(a->info.op == '+') printf("+");
	  else printf("-");
	  Infixe(a->droit);
	  break;
	  case'*':
	    case'/':/* affichage infixe avec () */
	    if(a->gauche->type != Operateur)
	      Infixe(a->gauche);
	    else
	      {
		printf("(");
		Infixe(a->gauche);
		printf(")");
	      }
	  if(a->info.op == '*') printf("*");
	  else printf("/");
 
	  if(a->droit->type != Operateur)
	    Infixe(a->droit);
	  else
	    {
	      printf("(");
	      Infixe(a->droit);
	      printf(")");
	    }
	  break;
	  case'@':/* affichage prefixe */
	    printf("@");
	  Infixe(a->gauche);
	  break;
	  case'~':
	    printf("~");
	  Infixe(a->gauche);
	  break;
	}
    }
} 
 
 
 
 
 
 
int main(void)
{
  char *ligne = "+ 1 2";
  Arbre a = CreerArbre(&ligne);
  Infixe(a);
  return 0;
}