EXERCICE XII
Les TRIS
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MAXMOTS 100
#define MAXCARS 51
void tri(char *t[],
int n);
void print(char
*t[], int n);
void main(void)
{ char
temp[MAXCARS];
char
*mots[MAXMOTS];
int i=0;
puts("Introduire
les mots.\n");
while(1)
{ printf("mot %d:",i);
if(gets(temp)== NULL) break;;
mots[i] = (char *)malloc(strlen(temp)+1);
if(mots[i])
strcpy(mots[i],temp);
else
{
printf("Pas assez de memoire\n");
exit(1);
}
i++;
}
tri(mots,i);
print(mots,i);
}
void print(char
*mots[], int n)
{ for (int i = 0; i < n; i++)
printf("\n%s",
mots[i]);
}
void tri(char *t[], int n)
{ for (;;)
{ int
i, ok = 1;
for (i = 1; i < n; i++)
if (strcmp(t[i - 1], t[i]) > 0)
{ char *w = t[i - 1];
t[i - 1] = t[i];
t[i] = w;
ok = 0;
}
if (ok)
return;
n--;
}
}
·
void triInsertion(char
*t[ ], int n);
·
void triSelection(char
*t[ ], int n);
#include
<stdio.h>
#include
<stdlib.h>
#include
<string.h>
#include
<ctype.h>
#include <conio.h>
#define STRMAX 80 // longueur
maximal des chaines
#define FMAX 200 //nombre maximal des voitures
typedef struct
{
char id[20];
char marque[50];
float annee;
float frais;
} car;
void newCar(car *db[]);
int NombreStr(car *db[]);
void showCar(car *db[]);
void sort_frais(car *db[]);
void main()
{ car
*data_b[FMAX];
do {
newCar(data_b);
printf("La prochaine? O/N:");
} while(toupper(getch())=='O');
printf("\nInformation des voitures au
debut\n");
showCar(data_b);
printf("Appuyer...");
getch();
sort_frais(data_b);
printf("\nInformation des voitures apres le
tri selon frais \n");
showCar(data_b);
printf("Appuyer...");
getch();
sort_marque_frais(data_b);
printf("\nInformation des
voitures apres le tri selon marque/frais \n");
showCar(data_b);
free (data_b);
}
int NombreStr(car *db[])
{
int count = 0;
while(db[count]
!= NULL && count < FMAX )
count++;
return count;
}
void newCar(car
*db[])
{ car carbuf;
char *buf=(char*)malloc(STRMAX);
int
i,sluj,j,broi,flag;
printf("\nNouvelle
voiture:\n");
printf("\n\tID: ");
fflush(stdin);
gets(buf);
strcpy(carbuf.id, buf);
printf("\tMarque:
");
gets(buf);
strcpy(carbuf.marque, buf);
printf("\tL'annee de production: ");
gets(buf);
carbuf.annee
= atof(buf);
printf("\tFrais - 100 km: ");
fflush(stdin);
gets(buf);
carbuf.frais = atof(buf);
i =
NombreStr(db);
if
(i >= FMAX)
{ printf("Pas de place!\n");
exit(1) ;
}
else
{
db[i] = (car*)malloc(sizeof(car));
strcpy(db[i]->id, carbuf.id);
strcpy(db[i]->marque, carbuf.marque);
db[i]->annee = carbuf.annee;
db[i]->frais = carbuf.frais;
}
}
void showCar(car *db[])
{ int i, nombre;
nombre = NombreStr(db);
if (nombre==0) {
printf("Rien a
afficher!");
printf("...");
getch();
}
for(i=0; i<nombre; i++)
{
printf("\t\t %s
\n",db[i]->marque );
printf("///////////////////////////////////////////////////\n");
printf("ID: %s\n",db[i]->id);
printf("Frais -
100km: %.2f\n",db[i]->frais);
printf("L'annee de
production: %.0f\n",db[i]->annee);
printf("///////////////////////////////////////////////////\n");
}
}
void sort_frais(car
*db[])
{ int done, cnt, i;
car *buf;
cnt = NombreStr(db);
do
{
done = 1;
for(i=0; i < cnt-1; i++)
{ if(db[i]->frais > db[i+1]->frais)
{ buf = db[i];
db[i] = db[i+1];
db[i+1] = buf;
done = 0;
}
}
}
while(done == 0);
}