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
| #include<stdio.h>
#define M 100
struct vl {
char Nom_Ville[20];
float superficie;
int nbre_habitants;
int nbre_entreprises;
int nbre_ecoles;
};
typedef struct vl Ville;
Ville T[M];
int i,n;
int remplir(Ville T[M])
{
do{
printf("Veuillez d'abord donner le nombre de ville : ");
scanf("%d", &n);
}
while(n<=0&&n>M);
for(i=0; i<n; i++) {
printf("Donnez le nom de la ville: "); getchar(); gets(T[i].Nom_Ville);
printf("Donnez la superficie: "); scanf("%f", &T[i].superficie);
printf("Donnez le population: "); scanf("%d", &T[i].nbre_habitants);
printf("Donnez le nombre d'entreprises: "); scanf("%d", &T[i].nbre_entreprises);
printf("Donnez le nombre d'ecoles: "); scanf("%d", &T[i].nbre_ecoles);
printf("\n\n");
fflush(stdin);
}
return n;
}
void affiche(Ville T[M])
{
for(i=0; i<n; i++) {
printf("Nom: %s. Superficie: %7.2f. Population: %d. Entreprises: %d. Ecoles: %d", T[i].Nom_Ville, T[i].superficie, T[i].nbre_habitants, T[i].nbre_entreprises, T[i].nbre_ecoles);
}
}
char petite(Ville T[M])
{
int index=0;
for(i=0; i<n; i++)
{
if (T[index].nbre_habitants > T[i].nbre_habitants)
index=i;
}
return T[index];
}
char riche(Ville T[M])
{
int index=0;
for(i=0; i<n; i++)
{
if (T[index].nbre_entreprises<T[i].nbre_entreprises)
index=i;
}
return T[index];
}
main()
{
int choix;
printf("Bienvenue:\nVeuillez tappez 1- Pour remplir le tableau. 2- Pour l'afficher. 3- Pour afficher la ville la plus riche. 4- Pour afficher la plus petite ville.");
scanf("%d", &choix);
switch (choix)
{
case 1:
remplir(T);
break;
case 2:
affiche(T);
break;
case 3:riche(T);affiche(T);
break;
case 4:petite(T);affiche(T);
break;
default : break;
}
return 0;
} |
Partager