IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

C Discussion :

Programme qui m'affiche n'importe quoi


Sujet :

C

  1. #1
    Inactif
    Profil pro
    Inscrit en
    Décembre 2005
    Messages
    72
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Décembre 2005
    Messages : 72
    Points : 33
    Points
    33
    Par défaut Programme qui m'affiche n'importe quoi
    Bonjour,

    dans le programme suivant ,j'ai un problème lorsque je teste la fonction "EstFonction".

    Dans ma ligne de commande ,je tape:
    qui devrait normalement me renvoyer :
    or j'ai comme resultat:un truc totalement faux.

    Est ce que vous seriez d'ou vient l'erreur.

    Merci d'avance.

    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
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <limits.h>
    #include <float.h>
    #include <ctype.h>
     
     
     
     
    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;
    }
     
     
     
     
    int EstVariable(char *ligne)
    {
      while(isalpha(*ligne))
        ligne++;
     
      if(*ligne == '\0')
        return 0;
      return 1;
    }
     
     
     
    int EstFonction(char *ligne, char *nom, int *nb)
    {
      int val;
      char *blanc = NULL;
      char *sep = NULL;
     
     
      /*il ne doit pas y avoir d'espae entre le nom,'@' et le nombre de parametres */
      blanc = strchr(ligne,' ');
      if(blanc != NULL)
        return 1;
     
      /*recherche de '@' pour avoir le nom de la fonction et le nombre de parametres */
      sep = strchr(ligne,'@');
      if(sep == NULL)
        return 1;
     
      *sep = '\0';
      /*sep pointe sur le nombre de parametre*/
      sep++;
     
      /*on verifie qu'on a un chiffre*/
      if(!isdigit(*sep))
        return 1;
     
      val = atoi(sep);
      if(val<=INT_MIN || val >=INT_MAX)
        return 1;
     
      *nb = val;
     
      nom = malloc(strlen(ligne)+1);
      if(nom == NULL)
        return 1;
      strcpy(nom,ligne);
     
      return 0;
    }
     
     
    int main(int argc, char *argv[])
    {
      char *nom;
      double val;
      int nb;
      int i;
      for(i=1;i<argc;i++)
        {
          /*if(!EstDouble(argv[i],&val))
    	printf("%lf ",val);*/
          /*if(!EstVariable(argv[i]))
    	printf("%s ",argv[i]);*/
          if(!EstFonction(argv[i],nom,&nb))
    	printf("%s=%d ",nom,nb);
          else 
    	printf("erreur ");
        }
      printf("\n");
      return 0;
    }

  2. #2
    Expert éminent sénior

    Avatar de fearyourself
    Homme Profil pro
    Ingénieur Informaticien Senior
    Inscrit en
    Décembre 2005
    Messages
    5 121
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 44
    Localisation : Etats-Unis

    Informations professionnelles :
    Activité : Ingénieur Informaticien Senior
    Secteur : Industrie

    Informations forums :
    Inscription : Décembre 2005
    Messages : 5 121
    Points : 11 877
    Points
    11 877
    Par défaut Re: Programme qui m'affiche n'importe quoi
    Encore un problème d'allocation... Dans ta fonction EstFonction, tout est correct mais lorsque tu fais:

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
     
      nom = malloc(strlen(ligne)+1);
      if(nom == NULL)
        return 1;
      strcpy(nom,ligne);
    Cela ne veut pas dire que ta variable de ton main sera modifié:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
     
      char *nom;
     ....
     
          if(!EstFonction(argv[i],nom,&nb))

    Au final, tu ne fais rien de mieux que ceci:

    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
     
    #include <stdio.h>
     
    void fct(int a)
    { 
      a = 5;
    }
     
    int main()
    { 
     int a = 0;
     fct(a);
     printf("%d\n",a);
     return 0;
    }

    Juste parce que ta variable est un pointeur ne veut pas dire que la valeur du pointeur sera modifié dans ton appel. Cela veut simplement dire que tu peux modifier la valeur de la/des donnée(s) pointée(s).

    Jc

  3. #3
    Inactif
    Profil pro
    Inscrit en
    Décembre 2005
    Messages
    72
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Décembre 2005
    Messages : 72
    Points : 33
    Points
    33
    Par défaut
    Merci,le programme fonctionne après avoir mis un char **.


    J'ai un problème concernant ce code .
    Dans mon main pour le teste,j'ai mis ceci:
    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
    int main(int argc, char *argv[])
    {
      int i;
      Environnement Env;
      Init(&Env);
      for(i=1; i<argc; i++)
        {
          if(AddVariable(&Env,argv[i],NULL))
    	fprintf(stderr,"erreur\n");
          else 
    	printf("argv[i]");
        }
     
    return 0;
    }
    Le programme m'affiche un message d'erreur concernant ma table alors qu'elle n'a qu'un seule élément .
    D'ou vient mon erreur?
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    la table est remplit
    erreur

    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
     
    #include <string.h>
    #include <stdio.h>
    #include <stdlib.h>
     
     
    void Init(Environnement *Env)
    {
      unsigned int i;
      for (i = 0; i < MAX; i++)
        {
          Env->tab[i].type = INDETERMINE;
          Env->tab[i].nom = NULL;
          Env->tab[i].nb_param = 0;
          Env->tab[i].fct = NULL;
          Env->tab[i].a = NULL;
          Env->tab[i].etat = LIBRE;
        }
    }
     
     
     
    int hash(const char *nom)
    {
      int som = 0;
      int i;
      for (i = 0; i < MAX; i++)
        som += nom[i];
      return som % MAX;
    }
     
     
    /* Recherche la position d'un nom dans la table */
    unsigned int RechercheNom(Environnement *Env, const char *nom)
    {
      unsigned int pos ;
     
      for (pos = 0U; pos < MAX; ++pos)
        if (Env->tab[pos].nom != NULL && (!(strcmp(nom, Env->tab[pos].nom))))
          return pos;
      return MAX;
    }
     
     
     
    int AddVariable(Environnement *Env, const char *nom, Arbre a)
    {
      unsigned pos = RechercheNom(Env, nom);
     
      if (pos != MAX) /* deja present */
        {
          fprintf(stderr,"erreur,cette variable est deja presente\n");
          return 1;
        }
     
      unsigned int indice = hash(nom);
      unsigned int marque = indice;
     
      if (Env->tab[indice].etat == OCCUPE)
        {
          indice ++;
          while (Env->tab[indice].etat == OCCUPE && indice != marque)
    	{
    	  if (indice < MAX)
                indice ++;
    	  else
                indice -= MAX;
    	}
        }
     
      if (indice == marque)
        {
          fprintf(stderr, "la table est remplit\n");
          return 1 ;
        }
     
      Env->tab[indice].type = 0;
     
      Env->tab[indice].nom = (char*)malloc(strlen(nom) + 1);
      if (Env->tab[indice].nom == NULL)
        {
          fprintf(stderr, "erreur allocation\n");
          return 1 ;
        }
      strcpy(Env->tab[indice].nom, nom);
     
      Env->tab[indice].a = a;
      Env->tab[indice].etat = OCCUPE;
     
      return 0;
    }

  4. #4
    Expert éminent sénior

    Avatar de fearyourself
    Homme Profil pro
    Ingénieur Informaticien Senior
    Inscrit en
    Décembre 2005
    Messages
    5 121
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 44
    Localisation : Etats-Unis

    Informations professionnelles :
    Activité : Ingénieur Informaticien Senior
    Secteur : Industrie

    Informations forums :
    Inscription : Décembre 2005
    Messages : 5 121
    Points : 11 877
    Points
    11 877
    Par défaut
    Sans pouvoir voir ce que tu passes en argument ni voir les structures de données... Voici une erreur:

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
     
    int hash(const char *nom)
    {
      int som = 0;
      int i;
      for (i = 0; i < MAX; i++)
        som += nom[i];
      return som % MAX;
    }
    Dépasse la longueur du tableau (ou du moins sûrement...), il faut tester la fin de la chaîne de caractères.

  5. #5
    Inactif
    Profil pro
    Inscrit en
    Décembre 2005
    Messages
    72
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Décembre 2005
    Messages : 72
    Points : 33
    Points
    33
    Par défaut
    Voici la structure

    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
    #include <string.h>
    #include <stdio.h>
    #include <stdlib.h>
     
     
    enum Type {Operateur, Constante, Variable, Fonction};
     
     
    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;
     
     
     
     
    #define MAX 100
    #define LIBRE 0
    #define OCCUPE 1
    #define INDETERMINE 2 /* type du symbole*/
     
     
    typedef struct
    {
      char *param;
      Arbre arbresparam;
    }TableFonction;
     
     
    typedef struct
    {
      char type;/* 0:variable, 1:fonction*/
      char *nom;
      int nb_param;
      TableFonction *fct;
      Arbre a;
      char etat;
    } Symbole;
     
     
    typedef struct 
    {
      Symbole tab[MAX];
    } Environnement;

    Les fonctions
    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
    void Init(Environnement *Env)
    {
      unsigned int i;
      for (i = 0; i < MAX; i++)
        {
          Env->tab[i].type = INDETERMINE;
          Env->tab[i].nom = NULL;
          Env->tab[i].nb_param = 0;
          Env->tab[i].fct = NULL;
          Env->tab[i].a = NULL;
          Env->tab[i].etat = LIBRE;
        }
    }
     
     
    int hash(const char *nom)
    {
      int som = 0;
      int i;
      size_t lg = strlen(nom);
      for (i = 0; i < lg; i++)
        som += nom[i];
      return som % MAX;
    }
     
     
    /* Recherche la position d'un nom dans la table */
    unsigned int RechercheNom(Environnement *Env, const char *nom)
    {
      unsigned int pos ;
     
      for (pos = 0U; pos < MAX; ++pos)
        if (Env->tab[pos].nom != NULL && (!(strcmp(nom, Env->tab[pos].nom))))
          return pos;
      return MAX;
    }
     
     
     
    int AddVariable(Environnement *Env, const char *nom, Arbre a)
    {
      unsigned pos = RechercheNom(Env, nom);
     
      if (pos != MAX) /* deja present */
        {
          fprintf(stderr,"erreur,cette variable est deja presente\n");
          return 1;
        }
     
      unsigned int indice = hash(nom);
      unsigned int marque = indice;
     
      if (Env->tab[indice].etat == OCCUPE)
        {
          indice ++;
          while (Env->tab[indice].etat == OCCUPE && indice != marque)
    	{
    	  if (indice < MAX)
                indice ++;
    	  else
                indice -= MAX;
    	}
        }
     
      if (indice == marque)
        {
          fprintf(stderr, "la table est remplit\n");
          return 1 ;
        }
     
      Env->tab[indice].type = 0;
     
      Env->tab[indice].nom = (char*)malloc(strlen(nom) + 1);
      if (Env->tab[indice].nom == NULL)
        {
          fprintf(stderr, "erreur allocation\n");
          return 1 ;
        }
      strcpy(Env->tab[indice].nom, nom);
     
      Env->tab[indice].a = a;
      Env->tab[indice].etat = OCCUPE;
     
      return 0;
    }
     
     
     
     
    /* Ajout d'une fonction dans la table des symboles */
    int AddFonction(Environnement *Env, const char *nom, int nb_param, char **tabparam, Arbre a)
    {
      unsigned pos = RechercheNom(Env, nom);
     
      if (pos != MAX) /* deja present */
        {
          fprintf(stderr,"erreur,cette fonction est deja presente\n");
          return 1;
        }
     
      unsigned int indice = hash(nom);
      unsigned int marque = indice;
     
      if (Env->tab[indice].etat == OCCUPE)
        {
          indice ++;
          while (Env->tab[indice].etat == OCCUPE && indice != marque)
    	{
    	  if (indice < MAX)
                indice ++;
    	  else
                indice -= MAX;
    	}
        }
     
      if (indice == marque)
        {
          fprintf(stderr, "la table est remplit\n");
          return 1 ;
        }
     
      Env->tab[indice].type = 1;
     
      Env->tab[indice].nom = (char*)malloc(strlen(nom) + 1);
      if (Env->tab[indice].nom == NULL)
        {
          fprintf(stderr, "erreur allocation\n");
          return 1 ;
        }
      strcpy(Env->tab[indice].nom, nom);
     
      Env->tab[indice].nb_param = nb_param;
     
      int i ;
      Env->tab[indice].fct = (TableFonction*)malloc(sizeof(TableFonction) * nb_param);
      if (Env->tab[indice].fct == NULL)
        return 1;
     
      for (i = 0; i < nb_param; i++)
        {
          Env->tab[indice].fct[i].param = malloc(strlen(tabparam[i]) + 1);
          if (Env->tab[indice].fct[i].param == NULL)
    	{
    	  fprintf(stderr, "erreur allocation\n");
    	  return 1;
    	}
          strcpy(Env->tab[indice].fct[i].param, tabparam[i]);
          Env->tab[indice].fct[i].arbresparam = NULL;
        }
     
      Env->tab[indice].a = a;
      Env->tab[indice].etat = OCCUPE;
      return 0;
    }
     
     
     
     
    /* Recherche si une variable appartient  la table et donne sa valeur si c'est le cas */
     
    int RechercheVariable(Environnement *Env,const char *nom, Arbre *arbre)
    {
      unsigned pos = RechercheNom(Env,nom);
     
      /* la variable n'a pas ete trouvee */
      if (pos == MAX) return 1;
     
      /* trouvee */
      if(Env->tab[pos].type != 0)
        {
          fprintf(stderr,"ce symbole n'est pas une variable\n");  
          return 1;
        }
      *arbre = Env->tab[pos].a;
     
      return 0;
    }
     
     
     
     
     
     
    /* Recherche d'une fonction dans la table des symboles */
    int RechercheFonction(Environnement *Env, const char *nom, int *nb_param, char **tabparam, Arbre *a)
    {
      unsigned pos = RechercheNom(Env, nom);
     
      if (pos == MAX) /* pas trouve */
        return 1 ;
      *nb_param = Env->tab[pos].nb_param;
     
      tabparam = malloc(sizeof(*tabparam) * (*nb_param));
      if (tabparam == NULL)
        return 1;
     
      int i;
      for (i = 0; i < (*nb_param); i++)
        {
          tabparam[i] = malloc(sizeof(**tabparam) * (strlen(Env->tab[pos].fct[i].param) + 1));
          if (tabparam[i] == NULL)
    	return 1;
          strcpy(tabparam[i],Env->tab[pos].fct[i].param);
        }
      *a = Env->tab[pos].a;
      return 0;
    }
     
     
    int main(int argc, char *argv[])
    {
      Environnement Env;
      Init(&Env);
     
      if(AddVariable(&Env,argv[1],NULL))
        fprintf(stderr,"erreur\n");
      else 
        printf("%s",argv[1]);
     
      return 0;
    }

  6. #6
    Expert éminent sénior

    Avatar de fearyourself
    Homme Profil pro
    Ingénieur Informaticien Senior
    Inscrit en
    Décembre 2005
    Messages
    5 121
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 44
    Localisation : Etats-Unis

    Informations professionnelles :
    Activité : Ingénieur Informaticien Senior
    Secteur : Industrie

    Informations forums :
    Inscription : Décembre 2005
    Messages : 5 121
    Points : 11 877
    Points
    11 877
    Par défaut
    Tu as un problème de parenthésage dans la fonction AddVariable et AddFonction:

    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
    if (Env->tab[indice].etat == OCCUPE)
        {
          indice ++;
          while (Env->tab[indice].etat == OCCUPE && indice != marque)
       {
         if (indice < MAX)
                indice ++;
         else
                indice -= MAX;
       }
        }
     
      if (indice == marque)
        {
          fprintf(stderr, "la table est remplit\n");
          return 1 ;
        }
    Devrait être:
    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
     
    if (Env->tab[indice].etat == OCCUPE)
        {
          indice ++;
          while (Env->tab[indice].etat == OCCUPE && indice != marque)
       {
         if (indice < MAX)
                indice ++;
         else
                indice -= MAX;
       }
     
     
      if (indice == marque)
        {
          fprintf(stderr, "la table est remplit\n");
          return 1 ;
        }
    }
    Jc

  7. #7
    Expert éminent sénior
    Avatar de Emmanuel Delahaye
    Profil pro
    Retraité
    Inscrit en
    Décembre 2003
    Messages
    14 512
    Détails du profil
    Informations personnelles :
    Âge : 67
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Retraité

    Informations forums :
    Inscription : Décembre 2003
    Messages : 14 512
    Points : 20 985
    Points
    20 985
    Par défaut Re: Programme qui m'affiche n'importe quoi
    Citation Envoyé par Gryzzly
    dans le programme suivant ,j'ai un problème lorsque je teste la fonction "EstFonction".
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
      char *nom;
     
          if(!EstFonction(argv[i],nom,&nb))
    'nom' est un pointeur non initialisé. Je ne sais pas ce que tu cherches à faire, mais passer une valeur indéterminée à une fonction invoque un comportement indéfini.

    Il y a une erreur de conception. Si tu veux que la fonction modifie le pointeur, il faut soit retourner une adresse (à-la-malloc() ou fopen()) ou passer son adresse. Ne pas oublier de libérer l'espace alloué.
    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
     
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <limits.h>
    #include <ctype.h>
     
    static int EstFonction(char *ligne, char **p_nom, int *nb)
    {
       int val;
       char *sep = NULL;
     
       /*il ne doit pas y avoir d'espace entre le nom,'@' et le nombre de parametres */
       char *blanc = strchr(ligne, ' ');
       if (blanc != NULL)
          return 1;
     
       /*recherche de '@' pour avoir le nom de la fonction et le nombre de parametres */
       sep = strchr(ligne, '@');
       if (sep == NULL)
          return 1;
     
       *sep = '\0';
       /*sep pointe sur le nombre de parametre*/
       sep++;
     
       /*on verifie qu'on a un chiffre*/
       if (!isdigit(*sep))
          return 1;
     
       val = atoi(sep);
       if (val <= INT_MIN || val >= INT_MAX)
          return 1;
     
       if (nb != NULL)
       {
          *nb = val;
       }
     
       {
          char *nom = malloc(strlen(ligne) + 1);
          if (nom == NULL)
             return 1;
     
          strcpy(nom, ligne);
     
          if (p_nom != NULL)
          {
             *p_nom = nom;
          }
       }
     
       return 0;
    }
     
     
    int main(int argc, char *argv[])
    {
       char *nom = NULL;
       int nb;
       int i;
       for (i = 1;i < argc;i++)
       {
          if (!EstFonction(argv[i], &nom, &nb))
          {
             printf("%s=%d\n", nom, nb);
             free (nom), nom = NULL;
          }
          else
             printf("erreur\n");
       }
       printf("\n");
       return 0;
    }
    Mais passer 2 adresses de résulats, c'est une de trop. Une structure serait plus adaptée :
    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
     
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <limits.h>
    #include <ctype.h>
     
    struct fonct
    {
       char *nom;
       int nb;
    };
     
    static int EstFonction(char *ligne, struct fonct *p_fun)
    {
       int err = 1;
     
       if (p_fun != NULL)
       {
          /*il ne doit pas y avoir d'espace entre le nom,'@' et le nombre de parametres */
          char *p = strchr(ligne, ' ');
     
          if (p == NULL)
          {
             /*recherche de '@' pour avoir le nom de la fonction et le nombre de parametres */
             p = strchr(ligne, '@');
     
             if (p != NULL)
             {
                *p = '\0';
                /*p pointe sur le nombre de parametre */
                p++;
     
                /*on verifie qu'on a un chiffre*/
                if (isdigit(*p))
                {
                   int val = atoi(p);
     
                   if (val >= INT_MIN && val <= INT_MAX)
                   {
                      p_fun->nb = val;
     
                      {
                         char *nom = malloc(strlen(ligne) + 1);
     
                         if (nom != NULL)
                         {
                            strcpy(nom, ligne);
     
                            p_fun->nom = nom;
                            err = 0;
                         }
                      }
                   }
                }
             }
          }
       }
       return err;
    }
     
    int main(int argc, char *argv[])
    {
       struct fonct f;
       int i;
     
       for (i = 1; i < argc; i++)
       {
          if (!EstFonction(argv[i], &f))
          {
             printf("%s=%d\n", f.nom, f.nb);
     
             free (f.nom), f.nom = NULL;
          }
          else
             printf("erreur\n");
       }
       return 0;
    }

  8. #8
    Inactif
    Profil pro
    Inscrit en
    Décembre 2005
    Messages
    72
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Décembre 2005
    Messages : 72
    Points : 33
    Points
    33
    Par défaut
    Citation Envoyé par Gryzzly
    Voici la structure

    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
    #include <string.h>
    #include <stdio.h>
    #include <stdlib.h>
     
     
    enum Type {Operateur, Constante, Variable, Fonction};
     
     
    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;
     
     
     
     
    #define MAX 100
    #define LIBRE 0
    #define OCCUPE 1
    #define INDETERMINE 2 /* type du symbole*/
     
     
    typedef struct
    {
      char *param;
      Arbre arbresparam;
    }TableFonction;
     
     
    typedef struct
    {
      char type;/* 0:variable, 1:fonction*/
      char *nom;
      int nb_param;
      TableFonction *fct;
      Arbre a;
      char etat;
    } Symbole;
     
     
    typedef struct 
    {
      Symbole tab[MAX];
    } Environnement;

    Les fonctions
    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
    void Init(Environnement *Env)
    {
      unsigned int i;
      for (i = 0; i < MAX; i++)
        {
          Env->tab[i].type = INDETERMINE;
          Env->tab[i].nom = NULL;
          Env->tab[i].nb_param = 0;
          Env->tab[i].fct = NULL;
          Env->tab[i].a = NULL;
          Env->tab[i].etat = LIBRE;
        }
    }
     
     
    int hash(const char *nom)
    {
      int som = 0;
      int i;
      size_t lg = strlen(nom);
      for (i = 0; i < lg; i++)
        som += nom[i];
      return som % MAX;
    }
     
     
    /* Recherche la position d'un nom dans la table */
    unsigned int RechercheNom(Environnement *Env, const char *nom)
    {
      unsigned int pos ;
     
      for (pos = 0U; pos < MAX; ++pos)
        if (Env->tab[pos].nom != NULL && (!(strcmp(nom, Env->tab[pos].nom))))
          return pos;
      return MAX;
    }
     
     
     
    int AddVariable(Environnement *Env, const char *nom, Arbre a)
    {
      unsigned pos = RechercheNom(Env, nom);
     
      if (pos != MAX) /* deja present */
        {
          fprintf(stderr,"erreur,cette variable est deja presente\n");
          return 1;
        }
     
      unsigned int indice = hash(nom);
      unsigned int marque = indice;
     
      if (Env->tab[indice].etat == OCCUPE)
        {
          indice ++;
          while (Env->tab[indice].etat == OCCUPE && indice != marque)
    	{
    	  if (indice < MAX)
                indice ++;
    	  else
                indice -= MAX;
    	}
        }
     
      if (indice == marque)
        {
          fprintf(stderr, "la table est remplit\n");
          return 1 ;
        }
     
      Env->tab[indice].type = 0;
     
      Env->tab[indice].nom = (char*)malloc(strlen(nom) + 1);
      if (Env->tab[indice].nom == NULL)
        {
          fprintf(stderr, "erreur allocation\n");
          return 1 ;
        }
      strcpy(Env->tab[indice].nom, nom);
     
      Env->tab[indice].a = a;
      Env->tab[indice].etat = OCCUPE;
     
      return 0;
    }
     
     
     
     
    /* Ajout d'une fonction dans la table des symboles */
    int AddFonction(Environnement *Env, const char *nom, int nb_param, char **tabparam, Arbre a)
    {
      unsigned pos = RechercheNom(Env, nom);
     
      if (pos != MAX) /* deja present */
        {
          fprintf(stderr,"erreur,cette fonction est deja presente\n");
          return 1;
        }
     
      unsigned int indice = hash(nom);
      unsigned int marque = indice;
     
      if (Env->tab[indice].etat == OCCUPE)
        {
          indice ++;
          while (Env->tab[indice].etat == OCCUPE && indice != marque)
    	{
    	  if (indice < MAX)
                indice ++;
    	  else
                indice -= MAX;
    	}
        }
     
      if (indice == marque)
        {
          fprintf(stderr, "la table est remplit\n");
          return 1 ;
        }
     
      Env->tab[indice].type = 1;
     
      Env->tab[indice].nom = (char*)malloc(strlen(nom) + 1);
      if (Env->tab[indice].nom == NULL)
        {
          fprintf(stderr, "erreur allocation\n");
          return 1 ;
        }
      strcpy(Env->tab[indice].nom, nom);
     
      Env->tab[indice].nb_param = nb_param;
     
      int i ;
      Env->tab[indice].fct = (TableFonction*)malloc(sizeof(TableFonction) * nb_param);
      if (Env->tab[indice].fct == NULL)
        return 1;
     
      for (i = 0; i < nb_param; i++)
        {
          Env->tab[indice].fct[i].param = malloc(strlen(tabparam[i]) + 1);
          if (Env->tab[indice].fct[i].param == NULL)
    	{
    	  fprintf(stderr, "erreur allocation\n");
    	  return 1;
    	}
          strcpy(Env->tab[indice].fct[i].param, tabparam[i]);
          Env->tab[indice].fct[i].arbresparam = NULL;
        }
     
      Env->tab[indice].a = a;
      Env->tab[indice].etat = OCCUPE;
      return 0;
    }
     
     
     
     
    /* Recherche si une variable appartient  la table et donne sa valeur si c'est le cas */
     
    int RechercheVariable(Environnement *Env,const char *nom, Arbre *arbre)
    {
      unsigned pos = RechercheNom(Env,nom);
     
      /* la variable n'a pas ete trouvee */
      if (pos == MAX) return 1;
     
      /* trouvee */
      if(Env->tab[pos].type != 0)
        {
          fprintf(stderr,"ce symbole n'est pas une variable\n");  
          return 1;
        }
      *arbre = Env->tab[pos].a;
     
      return 0;
    }
     
     
     
     
     
     
    /* Recherche d'une fonction dans la table des symboles */
    int RechercheFonction(Environnement *Env, const char *nom, int *nb_param, char **tabparam, Arbre *a)
    {
      unsigned pos = RechercheNom(Env, nom);
     
      if (pos == MAX) /* pas trouve */
        return 1 ;
      *nb_param = Env->tab[pos].nb_param;
     
      tabparam = malloc(sizeof(*tabparam) * (*nb_param));
      if (tabparam == NULL)
        return 1;
     
      int i;
      for (i = 0; i < (*nb_param); i++)
        {
          tabparam[i] = malloc(sizeof(**tabparam) * (strlen(Env->tab[pos].fct[i].param) + 1));
          if (tabparam[i] == NULL)
    	return 1;
          strcpy(tabparam[i],Env->tab[pos].fct[i].param);
        }
      *a = Env->tab[pos].a;
      return 0;
    }
     
     
    int main(int argc, char *argv[])
    {
      Environnement Env;
      Init(&Env);
     
      if(AddVariable(&Env,argv[1],NULL))
        fprintf(stderr,"erreur\n");
      else 
        printf("%s",argv[1]);
     
      return 0;
    }
    Et dans cet code,est ce que les allocations ont été effectues correctement?
    Les fonctions Add.. servent à ajouter les parametres à la table
    les fonctions REcherche... servent à recuperer les parametres

  9. #9
    Expert éminent sénior
    Avatar de Emmanuel Delahaye
    Profil pro
    Retraité
    Inscrit en
    Décembre 2003
    Messages
    14 512
    Détails du profil
    Informations personnelles :
    Âge : 67
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Retraité

    Informations forums :
    Inscription : Décembre 2003
    Messages : 14 512
    Points : 20 985
    Points
    20 985
    Par défaut
    Citation Envoyé par Gryzzly
    int RechercheFonction(Environnement *Env, const char *nom, int *nb_param, char **tabparam, Arbre *a)
    {
    <...>
    tabparam = malloc(sizeof(*tabparam) * (*nb_param));
    [/code]
    Et dans cet code,est ce que les allocations ont été effectues correctement?
    Non. Une fois de plus, tu modifies un paramètre localement, ce qui n'a évidemment aucune incidence sur la valeur originale. On t'a déjà expliqué ça et tu continues à répéter les même erreurs, c'est desespérant...

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Printf qui affiche n'importe quoi
    Par med1001 dans le forum C
    Réponses: 2
    Dernier message: 13/04/2012, 04h11
  2. Mon programme affiche n'importe quoi
    Par chicabonux dans le forum Débuter
    Réponses: 43
    Dernier message: 18/08/2009, 11h34
  3. Programme qui n'affiche rien
    Par Premium dans le forum OpenGL
    Réponses: 2
    Dernier message: 03/12/2006, 21h43
  4. Réponses: 18
    Dernier message: 13/12/2005, 13h27
  5. [LG]Programme qui n'affiche rien
    Par ousunas dans le forum Langage
    Réponses: 4
    Dernier message: 17/02/2004, 19h38

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo