Forum |  HardWare.fr | News | Articles | PC | S'identifier | S'inscrire | Shop Recherche
1503 connectés 

  FORUM HardWare.fr
  Programmation
  C

  Probleme de segmentation

 


 Mot :   Pseudo :  
 
Bas de page
Auteur Sujet :

Probleme de segmentation

n°1280747
Gattuso
Posté le 10-01-2006 à 17:44:50  profilanswer
 

Bonjour,
 
j'aurais besoin d'aide car mon programme ne fonctionne pas .
 
Est ce que vous pourriez m'aider.
 
Merci  
 
 

Code :
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <ctype.h>
  4. #include <float.h>
  5. #include <string.h>
  6. enum Type
  7.   {
  8.     Operateur,
  9.     Constante,
  10.     Variable
  11.   };
  12. union Info 
  13. {
  14.   char *nom;
  15.   double cte;
  16.   char op;
  17. };
  18. typedef struct noeud{
  19.   enum Type type;
  20.   union Info info;
  21.   struct noeud *gauche;
  22.   struct noeud *droit;
  23. }Noeud,*Arbre;
  24. void espace (char **ligne)
  25. {
  26.   while(**ligne == ' ') (*ligne)++;
  27. }
  28. int EstDouble(char *ligne, double *val)
  29. {
  30.   double v;
  31.   char *endptr = NULL;
  32.   v = strtod(ligne,&endptr);
  33.  
  34.   /* la chaine ne contient pas que des chiffres */
  35.   if(*endptr != '\0')
  36.     {
  37.       fprintf(stderr,"le chiffre n'est pas correctement ecrit\n" );
  38.       return 1;
  39.     }
  40.  
  41.   /* verification si debordements */
  42.   if(v<=(-DBL_MAX) || v>=DBL_MAX)
  43.     {
  44.       fprintf(stderr,"debordement\n" );
  45.       return 1;
  46.     }
  47.  
  48.   /* on recupere la valeur */
  49.   *val = v;
  50.  
  51.   return 0;
  52. }
  53. /* Analyse d'une expression : creation de l'arbre */
  54. Arbre CreerArbre(char **ligne)
  55. {
  56.   double val;
  57.   char operateur;
  58.   char *chiffre;
  59.   char *nom;
  60.  
  61.   Arbre a = malloc(sizeof(Noeud));
  62.   if(a == NULL)
  63.     {
  64.       fprintf(stderr,"erreur allocation de l'arbre\n" );
  65.       return NULL;
  66.     }
  67.  
  68.   if(**ligne == '\0')
  69.     return NULL;
  70.   while(**ligne != '\0')
  71.     {
  72.       /* c'est un chiffre */
  73.       if(isdigit(**ligne))
  74. {
  75.   chiffre = *ligne;
  76.   int i = 0;
  77.   while(isdigit(**ligne) || **ligne =='.')
  78.     {
  79.       chiffre[i] = **ligne;
  80.       (*ligne)++;
  81.       i++;
  82.     }
  83.   chiffre[i] = '\0';
  84.  
  85.   if(**ligne != ' ' || **ligne != '\0')
  86.     return NULL;
  87.  
  88.   if(EstDouble(chiffre,&val))
  89.     {    
  90.       fprintf(stderr,"le chiffre : '%s' n'est pas correctement ecrit\n",chiffre);
  91.       return NULL;
  92.     }
  93.  
  94.   a->type = Constante;
  95.   a->info.cte = val;
  96.  
  97.   chiffre = NULL;
  98.   espace(ligne);
  99. }
  100.      
  101.       else
  102. {
  103.   /* c'est une lettre -> variable*/
  104.   if(isalpha(**ligne))
  105.     {
  106.       nom = *ligne;
  107.       int j = 0;
  108.       while(isalpha(**ligne))
  109.  {
  110.    nom[j] = **ligne;
  111.    (*ligne)++;
  112.    j++;
  113.  }
  114.       nom[j] = '\0';
  115.      
  116.       if(**ligne != ' ' || **ligne != '\0')
  117.  return NULL;
  118.      
  119.       a->type = Variable;
  120.       a->info.nom = malloc(strlen(nom)+1);
  121.       if(a->info.nom == NULL)
  122.  {
  123.    fprintf(stderr,"erreur allocation" );
  124.    return NULL;
  125.  }
  126.       strcpy(a->info.nom,nom);     
  127.      
  128.       nom = NULL;
  129.       espace(ligne);
  130.     }
  131.  
  132.   else
  133.     {
  134.       /* le caractere est un operateur */
  135.       operateur = *((*ligne)++);
  136.       if(operateur == '+' || operateur  == '-' ||
  137.   operateur == '*' || operateur == '/')
  138.  {
  139.    a->type = Operateur;
  140.    a->info.op = operateur;
  141.    a->gauche = CreerArbre(ligne);
  142.    a->droit = CreerArbre(ligne);
  143.  }
  144.      
  145.       else
  146.  {
  147.    if(operateur == '@' || operateur == '~')
  148.      {
  149.        a->type = Operateur;
  150.        a->info.op = operateur;
  151.        a->gauche = NULL;
  152.        a->droit = CreerArbre(ligne);
  153.      }
  154.   
  155.    else
  156.      printf("Erreur,'%c' n'est pas un operateur prevu\n",operateur);
  157.  }
  158.     }
  159. }
  160.     }
  161.   return a;
  162. }
  163. void Infixe(Arbre a)
  164. {
  165.  
  166.   switch(a->type)
  167.     {
  168.     case Constante:
  169.       printf("%f",a->info.cte);
  170.       break;
  171.      
  172.     case Variable:
  173.       printf("%s",a->info.nom);
  174.       break;
  175.      
  176.     case Operateur:
  177.       switch(a->info.op)
  178. {
  179.   case'+':
  180.     case'-':/* affichage infixe sans () */
  181.     Infixe(a->gauche);
  182.   if(a->info.op == '+') printf("+" );
  183.   else printf("-" );
  184.   Infixe(a->droit);
  185.   break;
  186.   case'*':
  187.     case'/':/* affichage infixe avec () */
  188.     if(a->gauche->type != Operateur)
  189.       Infixe(a->gauche);
  190.     else
  191.       {
  192.  printf("(" );
  193.  Infixe(a->gauche);
  194.  printf(" )" );
  195.       }
  196.   if(a->info.op == '*') printf("*" );
  197.   else printf("/" );
  198.  
  199.   if(a->droit->type != Operateur)
  200.     Infixe(a->droit);
  201.   else
  202.     {
  203.       printf("(" );
  204.       Infixe(a->droit);
  205.       printf(" )" );
  206.     }
  207.   break;
  208.   case'@':/* affichage prefixe */
  209.     printf("@" );
  210.   Infixe(a->gauche);
  211.   break;
  212.   case'~':
  213.     printf("~" );
  214.   Infixe(a->gauche);
  215.   break;
  216. }
  217.     }
  218. }
  219. int main(void)
  220. {
  221.   char *ligne = "+ * premier deuxieme 25";
  222.   Arbre a = CreerArbre(&ligne);
  223.   Infixe(a);
  224.   return 0;
  225. }


mood
Publicité
Posté le 10-01-2006 à 17:44:50  profilanswer
 

n°1280757
Elmoricq
Modérateur
Posté le 10-01-2006 à 17:50:29  profilanswer
 

Commence par résoudre ceci :
 

"test.c", line 94: syntax error before or at: int
"test.c", line 97: undefined symbol: i
"test.c", line 101: undefined symbol: i
"test.c", line 125: syntax error before or at: int
"test.c", line 128: undefined symbol: j
"test.c", line 132: undefined symbol: j
"test.c", line 180: cannot recover from previous errors
cc: acomp failed for test.c


:o

Message cité 1 fois
Message édité par Elmoricq le 10-01-2006 à 17:53:58
n°1280768
Gattuso
Posté le 10-01-2006 à 18:02:08  profilanswer
 

Elmoricq a écrit :

Commence par résoudre ceci :
 

"test.c", line 94: syntax error before or at: int
"test.c", line 97: undefined symbol: i
"test.c", line 101: undefined symbol: i
"test.c", line 125: syntax error before or at: int
"test.c", line 128: undefined symbol: j
"test.c", line 132: undefined symbol: j
"test.c", line 180: cannot recover from previous errors
cc: acomp failed for test.c


:o


Bonjour,
 
tu utilises quoi pour obtenir ces messages? car je ne les obtients pas en compilant avec :

gcc -Wall -ansi -g -Wwrite-strings test.c


 

"test.c", line 94: syntax error before or at: int
"test.c", line 97: undefined symbol: i
"test.c", line 101: undefined symbol: i
 
"test.c", line 128: undefined symbol: j
"test.c", line 132: undefined symbol: j
 
cc: acomp failed for test.c


J'ai verife et j'avais deja definit ,ces 2 variables
 

"test.c", line 125: syntax error before or at: int
"test.c", line 180: cannot recover from previous errors


Je ne comprends pas ces 2 lignes ?

n°1280901
Gattuso
Posté le 10-01-2006 à 20:36:37  profilanswer
 

Bonjour,
mon probleme de segmentation vient de cette partie du code.
Est ce que vous pourriez m'indiquer comment modifier ce bout pour ne plus avoir de seg fault.
 
Merci d'avance
 

Code :
  1. chiffre = *ligne;
  2.   int i = 0;
  3.   while(isdigit(**ligne) || **ligne =='.')
  4.     {
  5.       chiffre[i] = **ligne;
  6.       (*ligne)++;
  7.       i++;
  8.     }
  9.   chiffre[i] = '\0';


Message édité par Gattuso le 10-01-2006 à 20:37:56
n°1281035
Elmoricq
Modérateur
Posté le 11-01-2006 à 00:03:38  profilanswer
 

ligne est une chaîne non modifiable.
 
Je crois te l'avoir déjà dit dans un de tes posts. [:klem3i1]


Message édité par Elmoricq le 11-01-2006 à 00:04:05

Aller à :
Ajouter une réponse
  FORUM HardWare.fr
  Programmation
  C

  Probleme de segmentation

 

Sujets relatifs
probleme avec lien sur une imageProblème de boucle de lecture
Probleme sous ieProbleme lecture de flux RSS sur Intranet
Probleme de publipostage(Resolu)Problème de couleur avec un JTabbedPane
Problème de connexion avec l'utilisateur ASPNET???probléme avec create_element qui retourne NULL
problème d'erreur de segmentation[ASM GNU x86] Problème de segmentation fault [Résolu]
Plus de sujets relatifs à : Probleme de segmentation


Copyright © 1997-2022 Hardware.fr SARL (Signaler un contenu illicite / Données personnelles) / Groupe LDLC / Shop HFR