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

  FORUM HardWare.fr
  Programmation
  C

  aide petit prog c

 


 Mot :   Pseudo :  
 
Bas de page
Auteur Sujet :

aide petit prog c

n°1393497
mayapour
Posté le 23-06-2006 à 11:02:38  profilanswer
 

Bonjour,
 
Débutant en C, je souhaiterai savoir comment changer ce programme initiale (liste chainée) qui demande à l'utilisateur de choisir les options selon l'affichage :
1 : ajout dans la liste
2 : suppression dans liste
3 : trie de la liste
4 : affiche la liste
0 : quit programme
 
Je voudrai que celui-ci lance le programme suivi de l'option puis les paramêtres (selon option choisi).
 
Par explemple, ./monprog -a elementAajouter (qui correspond à l'option 1)
  ./monprog -b elementAsupp (qui correspond à l'option 2)
  ./monprog -c (option 3)
  ./monprog -d (option 4)
  ./monprog -q (option 0)
 
Avez vous des conseils, des exemples, des idées ?
 
Merci
 
Voici le code :
 

Code :
  1. #include<stdio.h>
  2. #include<malloc.h>
  3. #define Faux 0
  4. #define Vrai 1
  5. typedef struct Element{
  6.                int val;
  7.                struct Element *next;
  8.         }Element;
  9. int estVide(Element* L){
  10.          if(L == NULL){
  11.                  return Vrai;
  12.               }
  13.          else{
  14.                  return Faux;
  15.               }           
  16.      }         
  17.  
  18. void ajouter(Element **L, int v){
  19.        Element *tmp = *L;
  20.        Element *nouveau = (Element*)malloc(sizeof(Element));
  21.        if(nouveau == NULL){
  22.                   exit(1);
  23.              }
  24.        nouveau->val = v;
  25.        nouveau->next = NULL;
  26.        if(estVide(*L)){
  27.             *L = nouveau;         
  28.        }
  29.        else{
  30.             tmp = *L;
  31.             while(tmp->next != NULL){
  32.                  tmp = tmp->next; 
  33.             }
  34.             tmp->next = nouveau;
  35.        }
  36.      }
  37. void Trier(Element **L){
  38.        Element *tmp1, *tmp2;
  39.        Element *Min;
  40.        int t;
  41.        tmp1 = *L;
  42.        while(tmp1 != NULL){
  43.              Min = tmp1;
  44.              tmp2 = tmp1;
  45.              while(tmp2 != NULL){
  46.                  if(Min->val > tmp2->val) Min = tmp2;
  47.                  tmp2 = tmp2->next;     
  48.              }
  49.              t = tmp1->val;
  50.              tmp1->val = Min->val;
  51.              Min->val = t;
  52.            
  53.              tmp1=tmp1->next;   
  54.        }
  55.   }   
  56. void Supprimer(Element **L, int v){
  57.       Element *tmp;
  58.       int d;
  59.       if(!estVide(*L)){
  60.           tmp = *L;
  61.           d = 0;
  62.           if(tmp->val == v){
  63.              *L = (*L)->next;       
  64.           }
  65.           else{
  66.              while( d == 0 && tmp->next != NULL){
  67.                    if(tmp->next->val == v) d = 1;
  68.                    else  tmp = tmp->next;               
  69.              }
  70.              if(d == 1)  tmp->next = tmp->next->next;
  71.              else printf("!cette valeur n'est pas trouvable!\n" );     
  72.           }                 
  73.       }
  74.       else{
  75.            printf("!La liste est vide. Impossible de faire la suppression!\n" );
  76.       }
  77. int Longueur(Element *L){
  78.      int l=0;
  79.      Element *tmp = L;
  80.      while(tmp != NULL){
  81.          l++;
  82.          tmp = tmp->next;   
  83.      }
  84.      return l;
  85. }
  86. void afficher(Element *L){
  87.          Element *tmp;
  88.          if(estVide(L)){
  89.              printf("!la liste est vide!\n" );         
  90.          }
  91.          else{
  92.            tmp = L;
  93.            while(tmp != NULL){
  94.                 printf("%d ",tmp->val);
  95.                 tmp = tmp->next;   
  96.            }
  97.            printf("   - longueur : %d\n",Longueur(L));   
  98.          }
  99.       }
  100.    
  101.          
  102. int main(){
  103.   Element *L = NULL;
  104.   int choix;
  105.   char *fichier;
  106.   int v;
  107.   do{
  108.       printf("1-Ajouter Element.\n" );
  109.       printf("2-Supprimer Element.\n" );
  110.       printf("3-Trier la Liste.\n" );
  111.       printf("4-Afficher.\n" );
  112.       printf("0-Quitter.\n" );
  113.      
  114.       printf("   -->choix(0-4):" );scanf("%d",&choix);
  115.      
  116.       switch(choix){
  117.          case   1:printf(" - Entrer une valeur : " ); scanf("%d",&v);
  118.                   ajouter(&L, v);
  119.                 break;
  120.          case   2:printf(" - Entrer une valeur : " ); scanf("%d",&v);
  121.                   Supprimer(&L,v);
  122.                 break;
  123.          case   3:Trier(&L);
  124.                 break;
  125.          case   4:afficher(L);
  126.                 break;
  127.       }
  128.   }while(choix != 0);
  129. return 0;
  130. }

Message cité 1 fois
Message édité par mayapour le 23-06-2006 à 13:11:54
mood
Publicité
Posté le 23-06-2006 à 11:02:38  profilanswer
 

n°1393498
_darkalt3_
Proctopathe
Posté le 23-06-2006 à 11:04:04  profilanswer
 

lire le k&r, rechercher sur google, utiliser les argc et argv.

n°1393554
mayapour
Posté le 23-06-2006 à 11:58:29  profilanswer
 

ok, j'ai fait un début d'ajout des options en paramètres mais ça ne fontionne pas (j'ai une erreur de déclaration)
 

Code :
  1. #include<stdio.h>
  2. #include<malloc.h>
  3. #define Faux 0
  4. #define Vrai 1
  5. typedef struct Element{
  6.                int val;
  7.                struct Element *next;
  8.         }Element;
  9. int estVide(Element* L){
  10.          if(L == NULL){
  11.                  return Vrai;
  12.               }
  13.          else{
  14.                  return Faux;
  15.               }           
  16.      }
  17.    
  18. int woptions(int ac,char ***av)
  19. {
  20. int option2return;
  21. option2return=getopt(ac, *av,"abcd" );
  22. if(getopt(ac,*av,"abcd" ) != -1)
  23.        {
  24.               printf("listeChaine: usage -[abcd]\n" );
  25.        exit(EXIT_FAILURE);
  26.         }
  27. else
  28.  return option2return;
  29. }
  30.    
  31. void ajouter(Element **L, int v){
  32.        Element *tmp = *L;
  33.        Element *nouveau = (Element*)malloc(sizeof(Element));
  34.        if(nouveau == NULL){
  35.                   exit(1);
  36.              }
  37.        nouveau->val = v;
  38.        nouveau->next = NULL;
  39.        if(estVide(*L)){
  40.             *L = nouveau;         
  41.        }
  42.        else{
  43.             tmp = *L;
  44.             while(tmp->next != NULL){
  45.                  tmp = tmp->next; 
  46.             }
  47.             tmp->next = nouveau;
  48.        }
  49.      }
  50. void Trier(Element **L){
  51.        Element *tmp1, *tmp2;
  52.        Element *Min;
  53.        int t;
  54.        tmp1 = *L;
  55.        while(tmp1 != NULL){
  56.              Min = tmp1;
  57.              tmp2 = tmp1;
  58.              while(tmp2 != NULL){
  59.                  if(Min->val > tmp2->val) Min = tmp2;
  60.                  tmp2 = tmp2->next;     
  61.              }
  62.              t = tmp1->val;
  63.              tmp1->val = Min->val;
  64.              Min->val = t;
  65.            
  66.              tmp1=tmp1->next;   
  67.        }
  68.   }   
  69. void Supprimer(Element **L, int v){
  70.       Element *tmp;
  71.       int d;
  72.       if(!estVide(*L)){
  73.           tmp = *L;
  74.           d = 0;
  75.           if(tmp->val == v){
  76.              *L = (*L)->next;       
  77.           }
  78.           else{
  79.              while( d == 0 && tmp->next != NULL){
  80.                    if(tmp->next->val == v) d = 1;
  81.                    else  tmp = tmp->next;               
  82.              }
  83.              if(d == 1)  tmp->next = tmp->next->next;
  84.              else printf("!cette valeur n'est pas trouvable!\n" );     
  85.           }                 
  86.       }
  87.       else{
  88.            printf("!La liste est vide. Impossible de faire la suppression!\n" );
  89.       }
  90. int Longueur(Element *L){
  91.      int l=0;
  92.      Element *tmp = L;
  93.      while(tmp != NULL){
  94.          l++;
  95.          tmp = tmp->next;   
  96.      }
  97.      return l;
  98. }
  99. int nbrconf(char ac)
  100. {
  101. if(ac!=4)
  102. {
  103.  printf("listeChaine: usage -[abcd]\n" );
  104.  exit(EXIT_FAILURE);
  105. }
  106. else
  107.   return 1;
  108. }
  109. void afficher(Element *L){
  110.          Element *tmp;
  111.          if(estVide(L)){
  112.              printf("!la liste est vide!\n" );         
  113.          }
  114.          else{
  115.            tmp = L;
  116.            while(tmp != NULL){
  117.                 printf("%d ",tmp->val);
  118.                 tmp = tmp->next;   
  119.            }
  120.            printf("   - longueur : %d\n",Longueur(L));   
  121.          }
  122.       }
  123. void priseoption(char choixoption, char *v)
  124. {
  125.   do{
  126.   switch (choixoption)
  127.          {
  128.              case 'a':scanf("%d",&v);
  129.                     ajouter(&L, v);
  130.                  break;
  131.              case 'b':scanf("%d",&v);
  132.                     Supprimer(&L,v);
  133.           break;
  134.          case 'c':
  135.                 Trier(&L);
  136.           break;
  137.           case 'd':
  138.                     afficher(L);
  139.           break;
  140.          }
  141.      }while(choixoption != 0);
  142.    return 0;
  143. }
  144.          
  145. int main(char ac, char **av)
  146. {
  147.   Element *L = NULL;
  148.   char choixoption;
  149.   char *v;
  150. }


Message édité par mayapour le 23-06-2006 à 13:12:55
n°1393555
_darkalt3_
Proctopathe
Posté le 23-06-2006 à 11:59:35  profilanswer
 

- met des balises [ code ], ce sera plus lisible.
- copie/colle le msg d'erreur stp.

n°1393560
mayapour
Posté le 23-06-2006 à 12:05:00  profilanswer
 

Erreurs (avec compilateur gcc):
 
In function `priseoption':
138: error: `L' undeclared (first use in this function)
138: error: (Each undeclared identifier is reported only once
138: error: for each function it appears in.)
138: warning: passing arg 2 of `ajouter' makes integer from pointer without a cast
141: warning: passing arg 2 of `Supprimer' makes integer from pointer without a cast
151: warning: `return' with a value, in function returning void
 
Merci

n°1393562
_darkalt3_
Proctopathe
Posté le 23-06-2006 à 12:09:13  profilanswer
 

mayapour a écrit :

138: error: `L' undeclared (first use in this function)


 
T'as des problèmes de vue ?
Mets des balises [ code ] stp

n°1393586
breizhbugs
Posté le 23-06-2006 à 13:11:53  profilanswer
 

mayapour a écrit :

Bonjour,
...
0 : quit programme
Par exemple, ...
  ./monprog -q (option 0)


C'est quoi l'interet?  :pt1cable:  
 

n°1393589
mayapour
Posté le 23-06-2006 à 13:16:17  profilanswer
 

J'ai ajouté les [CODE] (un peu tard dsl)
l'intéret du -q c'est de passer au programme des fichiers tests a stocker dans des logs et de pouvoir y spécifier -q tout comme l'utilisateur qui taperait 0 pour quitter le programme.

n°1393597
mayapour
Posté le 23-06-2006 à 13:29:31  profilanswer
 

ok, à froce de mofifs :
Je ne sais pas quel pointeur utiliser pour récuper la bonne info pour la prise des options :
 

Code :
  1. void priseoption(char choixoption, char *L, char *v)
  2. {
  3.   do{
  4.    switch (choixoption)
  5.          {
  6.              case 'a':scanf("%d",&v);
  7.                     ajouter(&L, v);
  8.                  break;
  9.              case 'b':scanf("%d",&v);
  10.                     Supprimer(&L,v);
  11.            break;
  12.           case 'c':
  13.                  Trier(&L);
  14.            break;
  15.            case 'd':
  16.                     afficher(L);
  17.            break;
  18.          }
  19.      }while(choixoption != 0);
  20.    return 0;
  21. }
  22.          
  23. int main(int ac, char **av)
  24. {
  25.   Element *L = NULL;
  26.   char choixoption;
  27.   char *v;
  28. }


 
du coup j'ai des types de pointeurs imcompatibles ?? (le problème doit venir de L)
non ?

n°1393608
_darkalt3_
Proctopathe
Posté le 23-06-2006 à 13:52:02  profilanswer
 

Je crois que tu n'as malheureusement pas compris grand chose.
 
Dans ton main, où est l'appel à ta fonction priseoption ?
Nulle part. Résultat ton programme ne fait rien du tout, et c'est normal.
 
D'autre part, tu n'as pas de cas default dans ton case, c'est toujours bien d'en avoir un.?
 
Encore une fois, renseigne les messages d'erreurs ou soit plus clair dans la description de ton probleme.

mood
Publicité
Posté le 23-06-2006 à 13:52:02  profilanswer
 

n°1393630
mayapour
Posté le 23-06-2006 à 14:11:27  profilanswer
 

Exact ... MERCI
J'ai modifier mon main comme ceci : c'est mieux ?
 

Code :
  1. int main(int ac, char **av)
  2. {
  3.   Element *L = NULL;
  4.   char choixoption;
  5.   char *v;
  6.   nbrconf(ac);
  7.   choixoption=woptions(ac,&av);
  8.   priseoption(choixoption,av[2]);
  9. }


 
Cependant j'ai 161: error: too few arguments to function `priseoption' dans le main
Il attend quel argument ?

n°1393636
_darkalt3_
Proctopathe
Posté le 23-06-2006 à 14:16:09  profilanswer
 

t'as déjà programmé avant ? [:pingouino]
ou tu fais ton tp d'année à l'arrache avant de le rendre ?
 
T'as écrit une fonction à 3 paramètres et t'en renseigne que 2, y'aurait pas comme un oubli ?  [:pingouino]

n°1393763
mayapour
Posté le 23-06-2006 à 16:12:46  profilanswer
 

En effet, je suis novice en C ... (c'est pourquoi je recherche de l'aide de personnes qui savent programmer)
 
j'ai ajouté un paramètre à "priseoption".

Code :
  1. int main(int ac, char **av)
  2. {
  3.   Element *L = NULL;
  4.   char choixoption;
  5.   char *v;
  6.   nbrconf(ac);
  7.   choixoption=woptions(ac,&av);
  8.   priseoption(choixoption,av[2],v);
  9. }


 
Mais actuellement, tous mes appelles de fonction dans priseoption génèrent l'erreur : makes integer from pointer without a cast pour ajouter
et from incompatible pointer type pour Supprimer, Tier, afficher.
 
Je ne trouve pas bcp d'info sur le net sur ce type d'erreur.
Merci de m'éclairer...

Message cité 1 fois
Message édité par mayapour le 23-06-2006 à 16:13:18
n°1393766
_darkalt3_
Proctopathe
Posté le 23-06-2006 à 16:15:16  profilanswer
 

Reprend de 0, en ayant quelques connaissances, sinon tu vas jamais t'en sortir.
 
Trouve un livre , lis les bibliolinks, jette un oeil là ( http://mapage.noos.fr/emdel/ ), commence par des tutoriaux, parce que là tu n'as pas les bases.

n°1393774
mayapour
Posté le 23-06-2006 à 16:22:08  profilanswer
 

ok, merci pour le lien

n°1393799
Tamahome
⭐⭐⭐⭐⭐
Posté le 23-06-2006 à 16:39:58  profilanswer
 

Code :
  1. int main(int ac, char **av)
  2.   Element *L = NULL; 
  3.   char choixoption; 
  4.   char *v; 
  5.   nbrconf(ac); 
  6.   choixoption=woptions(ac,&av); 
  7.   priseoption(choixoption,av[2],v);
  8. }


 
un ptit

Code :
  1. return 0;

à la fin de ton main ne serait pas superflu


Message édité par Tamahome le 23-06-2006 à 16:40:46
n°1393801
Tamahome
⭐⭐⭐⭐⭐
Posté le 23-06-2006 à 16:41:37  profilanswer
 

Code :
  1. void priseoption(char choixoption, char *v)
  2. {
  3. ...
  4. return 0;
  5. }


 
un void qui renvoit un int, c'est conceptuel [:roane]

n°1393825
mayapour
Posté le 23-06-2006 à 16:56:59  profilanswer
 

Oui, mauvais placement du return (MERCI)
 
c'est mieux comme cela :
 

Code :
  1. void priseoption(char choixoption, char *L, char *v)
  2. {
  3.   do{
  4.   switch (choixoption)
  5.          {
  6.              case 'a':scanf("%d",&v);
  7.                     ajouter(&L, v);
  8.                  break;
  9.              case 'b':scanf("%d",&v);
  10.                     Supprimer(&L,v);
  11.           break;
  12.          case 'c':
  13.                 Trier(&L);
  14.           break;
  15.           case 'd':
  16.                     afficher(L);
  17.           break;
  18.          }
  19.      }while(choixoption != 0);
  20. }
  21.          
  22. int main(int ac, char **av)
  23. {
  24.   Element *L = NULL;
  25.   char choixoption;
  26.   char *v;
  27.   nbrconf(ac);
  28.   choixoption=woptions(ac,&av);
  29.   priseoption(choixoption,av[2],v);
  30.   return 0;
  31. }


 
Il me reste néanmoins toujours les erreurs :
In function `priseoption':
138: warning: passing arg 1 of `ajouter' from incompatible pointer type
138: warning: passing arg 2 of `ajouter' makes integer from pointer without a cast
141: warning: passing arg 1 of `Supprimer' from incompatible pointer type
141: warning: passing arg 2 of `Supprimer' makes integer from pointer without a cast
144: warning: passing arg 1 of `Trier' from incompatible pointer type
147: warning: passing arg 1 of `afficher' from incompatible pointer type


Message édité par mayapour le 23-06-2006 à 16:59:21
n°1394010
Tamahome
⭐⭐⭐⭐⭐
Posté le 23-06-2006 à 23:19:20  profilanswer
 

utiliser scanf mondieu mondieu, as tu lu le lien qu'on t'a filé plus haut ? fgets il pue ?
 

Code :
  1. Element *nouveau = (Element*)malloc(sizeof(Element));


 
on ne cast pas un malloc.


Message édité par Tamahome le 26-06-2006 à 11:43:08

---------------
Hobby eien /人◕ ‿‿ ◕人\
n°1394057
_darkalt3_
Proctopathe
Posté le 24-06-2006 à 08:55:07  profilanswer
 

On ne peut pas résoudre ses problèmes erreur par erreur, il y a vraiment un souci de fond là [:spamafote], il doit vraiment lire ses cours cette fois ;)


---------------
Töp of the plöp
n°1394273
Emmanuel D​elahaye
C is a sharp tool
Posté le 24-06-2006 à 20:00:31  profilanswer
 

mayapour a écrit :

En effet, je suis novice en C ...


http://mapage.noos.fr/emdel/images/c_warn.png


Message édité par Emmanuel Delahaye le 24-06-2006 à 20:01:00

---------------
Des infos sur la programmation et le langage C: http://www.bien-programmer.fr Pas de Wi-Fi à la maison : http://www.cpl-france.org/
n°1394858
Tamahome
⭐⭐⭐⭐⭐
Posté le 26-06-2006 à 11:42:43  profilanswer
 

voila, et encore "maitriser" est un bien grand mot. J'aurais plutot dit "bricoler".

n°1394885
skelter
Posté le 26-06-2006 à 12:11:58  profilanswer
 

quand meme pas, le langage seul est assez simple, mais bien programmer en c requiert beaucoup de methodologie

mood
Publicité
Posté le   profilanswer
 


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

  aide petit prog c

 

Sujets relatifs
Besoin d'aide affichage variable en phpPetit soucis...
aide pour petit script phpbesoin d'aide css customisation index apache
eleve mp besoin d'aide pour convertir camel >>> pascalfaire bouger par JS la hauteur d'un DIV petit a petit...
Aide pour une requête!Besoin d'aide pour un petit prog!
Aide pour petit progbesoin d'aide pour ce petit prog.
Plus de sujets relatifs à : aide petit prog c


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