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

  FORUM HardWare.fr
  Programmation
  C

  Probleme de code

 


 Mot :   Pseudo :  
 
Bas de page
Auteur Sujet :

Probleme de code

n°1682031
pepito17
Posté le 05-02-2008 à 15:25:56  profilanswer
 

Bonjour, je dois rendre un dossier pour lundi et dedans je dois y faire une indexation. En fait je dois entrer des produits et puis faire un tableau a deux dimensions "data" qui reprend l'origine du produit et le nom produit. Par exemple : Bsable (b=belge, sable) donc un tableau contenant que cela. puis avec ca, je dois faire une indexation, c'est a dire regarder dans ce tableau et le trier par ordre croissant mais en decroissant, par la je veux dire que si on avait entrer, Bsable, Efer, Bblé, Bacier, Egrain il devra etre trier comme ca : Efer, Egrain, Bacier, Bblé, Bsable. j'espere que vous comprenez... donc voila mtn je v vous donner mon code en entier... aidez moi svp j'en ai vrmt besoin !!
merci beaucoup a tout ceux qui m'aideront !!

 

code :

Code :
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <math.h>
  5. struct produit {
  6. char origine;
  7. long NumProduit;
  8. char NomProduit[30];
  9. char UniteMesure[10];
  10. float Prix;
  11. char TypeProduit[20];
  12. int categorie;
  13. };
  14. short EncodeProduit (struct produit [], int, long [], int, int, int);
  15. short RechercheNumProd (struct produit[], int, long []);
  16. void InsertionNumProd (struct produit[], long[], int);
  17. void AfficheProduit (struct produit[], int);
  18. void Indexation (struct produit[], char [][56], int [], int, int);
  19. //void AfficheIndex (char [][56], int [], int);
  20. int main ()
  21. {
  22. int nproduit=0, ProduitB=0, ProduitE=0, index[100];
  23. int enc,i;
  24. char data[100][56];
  25. long numprod[100];
  26. struct produit ListeProduits[100];
  27. i=0;
  28. do
  29. {
  30.  printf("Produit n%d : ", i+1);
  31.  printf("\n\n" );
  32.  enc=EncodeProduit(ListeProduits, i, numprod, nproduit, ProduitB, ProduitE);
  33.  if (ListeProduits[i].origine=='B')
  34.  {
  35.   ProduitB++;
  36.  }
  37.  else
  38.  {
  39.   if (ListeProduits[i].origine=='E')
  40.   {
  41.    ProduitE++;
  42.   }
  43.  }
  44.  if (ListeProduits[i].origine!=0x0A)
  45.  {
  46.   nproduit++;
  47.  }
  48.  system("cls" );
  49.  i++;
  50. }
  51. while (enc==1);
  52. AfficheProduit (ListeProduits, nproduit);
  53. printf("\n\n" );
  54. for (i=0;i<nproduit;i++)
  55. {
  56.  if (ListeProduits[i].origine=='B')
  57.  {
  58.   data[i][0]='B';
  59.   sprintf(&data[i][1], "%s", ListeProduits[i].NomProduit);
  60.  }
  61.  else
  62.  {
  63.   data[i][0]='E';
  64.   sprintf(&data[i][1], "%s", ListeProduits[i].NomProduit);
  65.  }
  66. }
  67. for (i=0;i<nproduit;i++)
  68. {
  69.  {
  70.   printf("%s  ", data[i]);
  71.  }
  72. }
  73. Indexation (ListeProduits, data, index, nproduit, ProduitE);
  74. //AfficheIndex (data, index, nproduit);
  75. /*for (i=0;i<nproduit;i++)
  76. {
  77.  printf("%d ", (index[i]+1));
  78. }*/
  79. system("pause" );
  80. }
  81. short EncodeProduit (struct produit ListeProduits[], int i, long numprod [], int nproduit, int ProduitB, int ProduitE)
  82. {
  83. int r, tp, cp, verif;
  84. char v[3][30]={"Fini", "Semi-Fini", "Matiere Premiere"};
  85. do
  86. {
  87.  printf("Quel est l'origine ? " );
  88.  fflush (stdin);
  89.  scanf("%c", &ListeProduits[i].origine);
  90. }
  91. while (ListeProduits[i].origine!=0x0A && ListeProduits[i].origine!='B' && ListeProduits[i].origine!='E');
  92. if (ListeProduits[i].origine!=0x0A)
  93. {
  94.  printf("Entrez le numero du produit : " );
  95.  scanf("%ld", &ListeProduits[i].NumProduit);
  96.  do
  97.  {
  98.   verif=RechercheNumProd (ListeProduits, i, numprod);
  99.  }
  100.  while (verif==0);
  101.  InsertionNumProd (ListeProduits, numprod, i);
  102.  printf("Entrez le nom du produit : " );
  103.  fflush (stdin);
  104.  gets (ListeProduits[i].NomProduit);
  105.  printf("Entrez l'unite du produit : " );
  106.  fflush (stdin);
  107.  gets (ListeProduits[i].UniteMesure);
  108.  printf("Entrez le prix du produit : " );
  109.  scanf("%f", &ListeProduits[i].Prix);
  110.  printf("Entrez le type du produit : " );
  111.  printf("\n\n\t1.\tFini\n\t2.\tSemi-fini\n\t3.\tMatiere Premiere\n" );
  112.  scanf ("%d", &tp);
  113.  strcpy (ListeProduits[i].TypeProduit,v[tp-1]);
  114.  printf("\n\nEntrez la categorie du produit : " );
  115.  printf("\n\n\t0.\tNon Dangereux\n\t1.\tDangereux\n" );
  116.  scanf ("%d", &cp);
  117.  ListeProduits[i].categorie=cp;
  118.  r=1;
  119. }
  120. else
  121. {
  122.  r=0;
  123. }
  124. return r;
  125. }
  126. short RechercheNumProd (struct produit ListeProduits[], int i, long numprod[])
  127. {
  128. int t;
  129. t=i;
  130. while (t>=0)
  131. {
  132.  if (ListeProduits[i].NumProduit==numprod[t])
  133.   {
  134.    printf("Numero de produit deja entrer..." );
  135.    printf("Veuillez entrez a nouveau le numero du produit : " );
  136.    scanf("%ld", &ListeProduits[i].NumProduit);
  137.    return 0;
  138.   }
  139.  t--;
  140. }
  141. return 1;
  142. }
  143. void InsertionNumProd (struct produit ListeProduits[], long numprod [], int i)
  144. {
  145. int t;
  146. t=i-1;
  147. while ((t>=0) && (ListeProduits[i].NumProduit<numprod[t]))
  148. {
  149.  numprod[t+1]=numprod[t];
  150.  t--;
  151. }
  152. numprod[t+1]=ListeProduits[i].NumProduit;
  153. }
  154. void AfficheProduit (struct produit ListeProduits[], int nproduit)
  155. {
  156. int i, n;
  157. printf("Quel produit voulez vous afficher ? \n\n" );
  158. for (i=0;i<nproduit;i++)
  159. {
  160.  printf("\tProduit n%d", i+1);
  161.  printf("\n" );
  162. }
  163. scanf("\n\n\t\t\t\t%d", &n);
  164. system("cls" );
  165. printf("B : Belgique ou E : Etranger ?  %c", ListeProduits[n-1].origine);
  166. printf("\n\nNumero du produit : %ld", ListeProduits[n-1].NumProduit);
  167. printf("\n\nNom du produit : " );
  168. puts (ListeProduits[n-1].NomProduit);
  169. printf("\nL'unite de mesure : " );
  170. puts (ListeProduits[n-1].UniteMesure);
  171. printf("\nLe prix : %.1f", ListeProduits[n-1].Prix);
  172. printf("\n\nLe type de produit : " );
  173. puts (ListeProduits[n-1].TypeProduit);
  174. printf("\nLa categorie : %d", ListeProduits[n-1].categorie);
  175. }
  176. void Indexation (struct produit ListeProduits [], char data [][56], int index[], int nproduit, int ProduitE)
  177. {
  178. int v, i, c, t, u;
  179. t=ProduitE;
  180. c=-1;
  181. for (i=0;i<nproduit;i++)
  182. {
  183.  if (ListeProduits[i].origine=='E')
  184.  {
  185.   c=u;
  186.   while ((c>=0) && strcmp(data[i],data[index[c]])<0) // <==
  187.   {
  188.    index[c+1]=index[c];
  189.    u--;
  190.   }
  191.   index[c+1]=i;
  192.   c++;
  193.  }
  194.  else
  195.  {
  196.   t=v;
  197.   while ((t>ProduitE) && strcmp(data[i],data[index[v]])<0)
  198.   {
  199.    index[v+1]=index[v];
  200.    t--;
  201.   }
  202.   index[v+1]=i;
  203.   v++;
  204.  }
  205. }
  206. }
  207. /*void AfficheIndex (char data [][56], int index [], int nproduit)
  208. {
  209. int i;
  210. printf("\n\n" );
  211. fflush (stdin);
  212. for (i=0;i<nproduit;i++)
  213. {
  214.  //printf("\t\t(-)\t" );
  215.  puts(data[index[i]]);
  216.  printf("\n\n" );
  217. }
  218. }*/
 

elmo-edit : ajout des balises [cpp][/cpp] pour la présentation du code


Message édité par Elmoricq le 05-02-2008 à 15:34:27
mood
Publicité
Posté le 05-02-2008 à 15:25:56  profilanswer
 

n°1682042
Elmoricq
Modérateur
Posté le 05-02-2008 à 15:36:07  profilanswer
 

Pour ton indexation, pourquoi ne pas utiliser qsort() ?
Ce serait plus simple.
 
Pour le reste du code,  j'ai la flemme de le lire, essaie d'isoler le problème à un morceau pertinent de ton code. [:dawa]

n°1682070
pepito17
Posté le 05-02-2008 à 16:01:08  profilanswer
 

je ne connais pas cette fonction... :s

n°1682075
Elmoricq
Modérateur
Posté le 05-02-2008 à 16:02:27  profilanswer
 

Si tu es sur un unix quelconque : man qsort
 
Sinon, "man qsort" dans google ça marche aussi.

n°1682105
Emmanuel D​elahaye
C is a sharp tool
Posté le 05-02-2008 à 16:57:07  profilanswer
 

pepito17 a écrit :

je ne connais pas cette fonction... :s


Mauvaise réponse. C'est une fonction standard... Il suffit d'ouvrir ton livre de C...
 
Sinon : http://mapage.noos.fr/emdel/notes.htm#rand


---------------
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/

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

  Probleme de code

 

Sujets relatifs
Probleme pour recuprer une variable dans un code embed vers un swfProblème code VBA
[Delphi] Problème dans un code / N'affiche jamais la variableprobléme de compilation avec code::blocks
problème code pour un triProblème COMPILATION Code::Block
Problème Compilation avec Code::Blockcompteur de telechargement mal placé: problème code html ?
Probleme d'installation code block sous UbuntuProbleme FCKeditor (code source image)
Plus de sujets relatifs à : Probleme de code


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