tibus27
11Messages postés
3 septembre 2007Date d'inscription
4 sept. 2007 à 21:21
Je n'est pas le droit d'utiliser la fonction (devoir scolaire) voici mon programme (attention bug switch / case voir mon aurtre poste) :
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define N 9
char buf[21];//variable temporaire de l'identifiant
char *table[10]; //tableau d'identifiant de l'indentifiant.
void affichemenu();
void recherche(int);
void affichage(int);
void doublon(int);
void tri(int);
int main(int argc, char *argv[])
{
int choixMenu="";
int cont=0;//controle de saisi
int tailleid=0; //taille de l'identifiant.
int i=0;//compteur de saisi.
int j=0;//compteur
int find=1;//paramètre de la recherch
printf("***************************************\nBienvenue dans le programme Identifiant\n***************************************\n\n");
do
{
affichemenu();
scanf("%d",&choixMenu);
printf("\n\n");
switch (choixMenu)
{
case 1:
printf("Veuillez entrer votre identifiant :\n\n");
if (i <= N)
{
scanf("%s", buf);//saisie de l'identifiant
for (j=0; j<=i-1 ; j++)
{
find = strcmp(table[j],buf);//test si il s'agi de la meme chaine
if (find == 0)
{
printf("\nL'identifiant existe deja\n\n",j+1);
break;//sorti de la boucle for
}
}
if (find != 0)
{
printf("\n\n");
tailleid = strlen(buf);//taille de l'identifiant
table[i] = (char*) malloc(tailleid);//attribut le nombre d'octets au pointeur dans table de i
strcpy (table[i], buf);
buf[0]=0 ;
i++;
{
tri(i);
}
}
if (tailleid > 21)//vérification de la taille
{
printf("votre identifiant est trop grand, maximum 20 caracteres\n\n");
i--;
}
}
else
{
printf("Vous avez saisi le nombre maximum d'identifiant\n\n");
}
choixMenu="";
break;
case 2:
printf("Voici la liste des identifiants :\n\n");
affichage(i);
choixMenu="";
break;
case 3:
printf("Veuillez entrer l'identifiant recherche :\n\n");
scanf("%s",buf);//chaine recherché
recherche(i);
choixMenu="";
break;
case 4:
printf("A bientot\n");
return (0);
default:
printf("vous n'avez pas entre un nombre correct ! \n\n");
break;
}
}while (choixMenu != 5);
}
void affichemenu()
{
printf("1) Entrer un identifiant\n2) Afficher la liste des identifiants\n");
printf("3) Chercher la position d'un identifiant\n4) Sortir de l'application\n\n");
printf("Veuillez saisir le numero de la fontion :");
}
void recherche(int i)
{
int j; //indice
int find=1;//paramètre de recherche
for (j=0; j<=i-1 ; j++)
{
find = strcmp(table[j],buf);//test si il s'agi de la meme chaine
if (find == 0)
{
printf("\nl'identifiant a ete trouve en %ld position\n\n",j+1);
}
}
if (find != 0)
{
printf("\nPas de reference pour cet identifiant\n\n");
}
}
void affichage(int i)
{
int j;//indice
for (j=0 ; j<=i-1 ; j++)//boucle parcourant les lignes de table
{
printf("%s\n\n", table[j]);
}
}
void tri(nb)
{
tri_bis(0,nb-1);
}
void tri_bis(int debut, int fin)
{
int pivot;
if (debut < fin)
{
pivot = placement_pivot(debut, fin);
tri_bis (debut, pivot-1);
tri_bis (pivot+1, fin);
}
}
int placement_pivot(int debut, int fin)
{
int compteur = debut;
int pivot = table[debut];
int i;//indice
for (i=debut+1; i<=fin; i++)
{
if (strcmp (table[i], table[pivot]) < 0)//test de la valeur de ligne
{
pivot=i;
strcpy (buf, table[compteur] );//echange des indices
strcpy (table[compteur], table[pivot]);
strcpy (table[pivot], buf);
}
strcpy (buf, table[debut] );//echange des indices
strcpy (table[compteur], table[debut]);
strcpy (table[compteur], buf);
return(compteur);
}
}