pepito17 | 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 :
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <math.h>
- struct produit {
- char origine;
- long NumProduit;
- char NomProduit[30];
- char UniteMesure[10];
- float Prix;
- char TypeProduit[20];
- int categorie;
- };
- short EncodeProduit (struct produit [], int, long [], int, int, int);
- short RechercheNumProd (struct produit[], int, long []);
- void InsertionNumProd (struct produit[], long[], int);
- void AfficheProduit (struct produit[], int);
- void Indexation (struct produit[], char [][56], int [], int, int);
- //void AfficheIndex (char [][56], int [], int);
- int main ()
- {
- int nproduit=0, ProduitB=0, ProduitE=0, index[100];
- int enc,i;
- char data[100][56];
- long numprod[100];
- struct produit ListeProduits[100];
- i=0;
- do
- {
- printf("Produit n%d : ", i+1);
- printf("\n\n" );
- enc=EncodeProduit(ListeProduits, i, numprod, nproduit, ProduitB, ProduitE);
- if (ListeProduits[i].origine=='B')
- {
- ProduitB++;
- }
- else
- {
- if (ListeProduits[i].origine=='E')
- {
- ProduitE++;
- }
- }
- if (ListeProduits[i].origine!=0x0A)
- {
- nproduit++;
- }
- system("cls" );
- i++;
- }
- while (enc==1);
- AfficheProduit (ListeProduits, nproduit);
- printf("\n\n" );
- for (i=0;i<nproduit;i++)
- {
- if (ListeProduits[i].origine=='B')
- {
- data[i][0]='B';
- sprintf(&data[i][1], "%s", ListeProduits[i].NomProduit);
- }
- else
- {
- data[i][0]='E';
- sprintf(&data[i][1], "%s", ListeProduits[i].NomProduit);
- }
- }
- for (i=0;i<nproduit;i++)
- {
- {
- printf("%s ", data[i]);
- }
- }
- Indexation (ListeProduits, data, index, nproduit, ProduitE);
- //AfficheIndex (data, index, nproduit);
- /*for (i=0;i<nproduit;i++)
- {
- printf("%d ", (index[i]+1));
- }*/
- system("pause" );
- }
- short EncodeProduit (struct produit ListeProduits[], int i, long numprod [], int nproduit, int ProduitB, int ProduitE)
- {
- int r, tp, cp, verif;
- char v[3][30]={"Fini", "Semi-Fini", "Matiere Premiere"};
- do
- {
- printf("Quel est l'origine ? " );
- fflush (stdin);
- scanf("%c", &ListeProduits[i].origine);
- }
- while (ListeProduits[i].origine!=0x0A && ListeProduits[i].origine!='B' && ListeProduits[i].origine!='E');
- if (ListeProduits[i].origine!=0x0A)
- {
- printf("Entrez le numero du produit : " );
- scanf("%ld", &ListeProduits[i].NumProduit);
- do
- {
- verif=RechercheNumProd (ListeProduits, i, numprod);
- }
- while (verif==0);
- InsertionNumProd (ListeProduits, numprod, i);
- printf("Entrez le nom du produit : " );
- fflush (stdin);
- gets (ListeProduits[i].NomProduit);
- printf("Entrez l'unite du produit : " );
- fflush (stdin);
- gets (ListeProduits[i].UniteMesure);
- printf("Entrez le prix du produit : " );
- scanf("%f", &ListeProduits[i].Prix);
- printf("Entrez le type du produit : " );
- printf("\n\n\t1.\tFini\n\t2.\tSemi-fini\n\t3.\tMatiere Premiere\n" );
- scanf ("%d", &tp);
- strcpy (ListeProduits[i].TypeProduit,v[tp-1]);
- printf("\n\nEntrez la categorie du produit : " );
- printf("\n\n\t0.\tNon Dangereux\n\t1.\tDangereux\n" );
- scanf ("%d", &cp);
- ListeProduits[i].categorie=cp;
- r=1;
- }
- else
- {
- r=0;
- }
- return r;
- }
- short RechercheNumProd (struct produit ListeProduits[], int i, long numprod[])
- {
- int t;
- t=i;
- while (t>=0)
- {
- if (ListeProduits[i].NumProduit==numprod[t])
- {
- printf("Numero de produit deja entrer..." );
- printf("Veuillez entrez a nouveau le numero du produit : " );
- scanf("%ld", &ListeProduits[i].NumProduit);
- return 0;
- }
- t--;
- }
- return 1;
- }
- void InsertionNumProd (struct produit ListeProduits[], long numprod [], int i)
- {
- int t;
- t=i-1;
- while ((t>=0) && (ListeProduits[i].NumProduit<numprod[t]))
- {
- numprod[t+1]=numprod[t];
- t--;
- }
- numprod[t+1]=ListeProduits[i].NumProduit;
- }
- void AfficheProduit (struct produit ListeProduits[], int nproduit)
- {
- int i, n;
- printf("Quel produit voulez vous afficher ? \n\n" );
- for (i=0;i<nproduit;i++)
- {
- printf("\tProduit n%d", i+1);
- printf("\n" );
- }
- scanf("\n\n\t\t\t\t%d", &n);
- system("cls" );
- printf("B : Belgique ou E : Etranger ? %c", ListeProduits[n-1].origine);
- printf("\n\nNumero du produit : %ld", ListeProduits[n-1].NumProduit);
- printf("\n\nNom du produit : " );
- puts (ListeProduits[n-1].NomProduit);
- printf("\nL'unite de mesure : " );
- puts (ListeProduits[n-1].UniteMesure);
- printf("\nLe prix : %.1f", ListeProduits[n-1].Prix);
- printf("\n\nLe type de produit : " );
- puts (ListeProduits[n-1].TypeProduit);
- printf("\nLa categorie : %d", ListeProduits[n-1].categorie);
- }
- void Indexation (struct produit ListeProduits [], char data [][56], int index[], int nproduit, int ProduitE)
- {
- int v, i, c, t, u;
- t=ProduitE;
- c=-1;
- for (i=0;i<nproduit;i++)
- {
- if (ListeProduits[i].origine=='E')
- {
- c=u;
- while ((c>=0) && strcmp(data[i],data[index[c]])<0) // <==
- {
- index[c+1]=index[c];
- u--;
- }
- index[c+1]=i;
- c++;
- }
- else
- {
- t=v;
- while ((t>ProduitE) && strcmp(data[i],data[index[v]])<0)
- {
- index[v+1]=index[v];
- t--;
- }
- index[v+1]=i;
- v++;
- }
- }
- }
- /*void AfficheIndex (char data [][56], int index [], int nproduit)
- {
- int i;
- printf("\n\n" );
- fflush (stdin);
- for (i=0;i<nproduit;i++)
- {
- //printf("\t\t(-)\t" );
- puts(data[index[i]]);
- printf("\n\n" );
- }
- }*/
| elmo-edit : ajout des balises [cpp][/cpp] pour la présentation du code Message édité par Elmoricq le 05-02-2008 à 15:34:27
|