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
| #include<stdio.h>
typedef struct
{
double nb1;
double nb2;
int nb3;
double nb4;
} stock;
void classement(stock commande_trier, int N);
int main(void)
{
int N, i, j;
printf("Combien de commande voulez-vous entrer ?: ");
scanf("%d", &N);
stock commande[N]; /*Déclaration d'un tableau de structure*/
int tableau_entier[N];
/*Boucle d'enregistrement de N commande*/
for (i=0;i<N;i++)
{
printf("Entrez la commande 1: ");
scanf("%lf", &commande[i].nb1);
printf("Entrez la commande 2: ");
scanf("%lf", &commande[i].nb2);
printf("Entrez la commande 3: ");
scanf("%d", &commande[i].nb3);
printf("Entrez la commande 4: ");
scanf("%lf", &commande[i].nb4);
printf("\n");
}
classement (commande, N);
/*Affichage des commandes*/
printf("Affichage des commandes par ordre chronologique:\n\n");
printf("|----------------|---------------|---------------|--------------|\n");
printf("|commande1\t | commande2\t | commande3\t | commande4\t|\n");
printf("|----------------|---------------|---------------|--------------|\n");
for (j=0;j<N;j++)
{
printf("|%lf\t | %lf\t | %d\t\t | %lf\t|\n", commande[tableau_entier[j]].nb1, commande[tableau_entier[j]].nb2, commande[tableau_entier[j]].nb3, commande[tableau_entier[j]].nb4);
printf("|----------------|---------------|---------------|--------------|\n");
}
return 0;
}
int classement(stock commande_trier[], int N)
{
int i, j;
int tableau_entier[N];
int i_depart = 0;
int i_min = 0;
int i_encours;
for (i=0;i<N;i++)
{
tableau_entier[i] = -1;
}
/* on recherche l'indice de départ */
for (i=1;i<N;i++)
{
if (commande_trier[i].nb1 < commande_trier[i_depart].nb1)
{
i_depart = i;
}
}
i_encours = i_depart;
for (i=1;i<N;i++) /* on fait N-1 tours car on a déjà le départ */
{
/* on recherche le nouveau min */
/* on recherche le 1er indice i_min valide */
i_min = 0;
while (i_min == i_encours || tableau_entier[i_min] != -1)
{
i_min++;
}
/* on recherche le vrai min */
for(j=i_min+1;j<N;j++)
{
if (commande_trier[j].nb1 < commande_trier[i_min].nb1 && tableau_entier[j] < 0 && j != i_encours)
{
i_min = j;
}
}
/* on met à jour */
tableau_entier[i_encours] = i_min;
i_encours = i_min;
}
return tableau_entier[N];
} |
Partager