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
|
#include <stdio.h>
int initabtuyau(int nbrtuyau, int tabtuyau[]); // initialise le tableau
int triprix(int nbrtuyau, int tabtuyau[]); //fonction de tri
int afftab(int nbr, int tab[]); //affiche un tableau
int initabindice(int nbr, int tabindice[]); //initialise le nouveau tableau
int main(void){
int nbrtuyau;
printf("Entrer le nombre de tuyaux differents :\n");
scanf("%d", &nbrtuyau);
int tabtuyau[nbrtuyau];
initabtuyau(nbrtuyau, tabtuyau);
system("pause");
triprix(nbrtuyau, tabtuyau);
system("pause");
}
int initabtuyau(int nbrtuyau, int tabtuyau[]){
printf("Initialisation du Tableau :\n");
int x;
for(x=0; x<nbrtuyau; x++){
printf("Origine du Tuyau %d :\n", x);
scanf("%d", &tabtuyau[x]);
}
return 1;
}
int triprix(int nbrtuyau, int tabtuyau[]){
int tabindice[nbrtuyau];
initabindice(nbrtuyau, tabindice);
int tuyencours;
int premier;
int ancienpremier;
int dernier;
int a;
int cherche;
int juste_sup, juste_inf, jj;
premier=0;
for(tuyencours=0; tuyencours<nbrtuyau; tuyencours++){
if(tuyencours==0){ // au premier, il est forcement le moins cher et le dernier !
tabindice[0]=-1;
dernier=0;
premier=0;
}
else{
printf("else pas le premier\n");
if(tabtuyau[tuyencours]<=tabtuyau[premier]){ // le moins cher de tous
ancienpremier = premier;
premier = tuyencours;
tabindice[tuyencours]=ancienpremier;
}
else{
cherche=premier;
juste_inf=premier;
while(((tabtuyau[tuyencours]>tabtuyau[cherche]) || (juste_inf!=-1)) || (cherche!=-1)){
jj=juste_inf;
juste_inf = tabindice[cherche];
cherche=tabindice[cherche];
printf("juste_inf = %d\ttabtuyau[juste_inf]=%d\n", juste_inf, tabtuyau[juste_inf]);
printf("jj=%d\tcherche= %d\n", jj, cherche);
}
if((juste_inf==-1) && (cherche==-1)){ // le plus cher
tabindice[dernier]=tuyencours;
tabindice[tuyencours]=-1;
dernier=tuyencours;
//printf("le plus cher!\n");
}
else{ // entre 2 prix deja classes
printf("%d<%d<%d\n", tabtuyau[juste_inf], tabtuyau[tuyencours], tabtuyau[cherche]);
tabindice[tuyencours]=cherche;
tabindice[juste_inf]=tuyencours;
}
}
}
printf("Le premier prix est : %d\n", premier);
afftab(nbrtuyau, tabindice);
system("pause");
}
}
int afftab(int nbr, int tab[]){
int x;
for(x=0;x<nbr;x++){
printf("%d\t ", tab[x]);
}
printf("\n\n");
}
int initabindice(int nbr, int tabindice[]){
int x;
for(x=0; x<nbr; x++){
tabindice[x]=0;
}
} |
Partager