Vous êtes sur la page 1sur 7

2ème année/ Semestre S3 Pr.

HALKHAMS IMANE

Correction du TD1: Structures de données


Exercice 1 :
1-Créez une structure de données qui permet de stocker à la fois la valeur de l'abscisse (x) et
celle de l'ordonnée (y) d'un point (2D) et créez deux points P1 et P2.
struct Coordonnees
{
int x; // Abscisses
int y; // Ordonnées
}P1,P2;
2- Modifiez cette structure afin qu’elle permette de stocker les coordonnées 3D cette fois-ci.
struct Coordonnees
{
int x; // Abscisses
int y; // Ordonnées
int z ; // Hauteur
};

Exercice 2 :
1- Déclarez en même temps le type « personne » et les variables p1 et p2, qui permettent de
renseigner le numéro de la personne, sa taille, son poids et sa ville :

struct personne
{
int numero ;
float taille, poids ;
char ville[10] ;

} p1, p2 ;

2-Écrire les instructions pour donner à la "p1" les informations suivantes :


C'est une personne qui mesure 1.67 mètre et pèse 67.8 kg.
Son numéro d'identification est le 7234 et elle habite à Fes.
p1.numero = 7234 ;
p1.ville = 'Fes' ;
p1.taille = 1.67 ;
p1.poids = 67.8 ;
ou
struct personne p1= {7234, 1.67, 67.8, "fes" };

3. Écrire les instructions pour saisir et afficher les informations de "p1" à l'écran :

#include <stdio.h>
#include "struct.h"
int main()
{
printf ("saisissez votre numero");
scanf("%d",&p1.numero);
printf ("saisissez votre taille");
scanf("%f",&p1.taille);
printf ("saisissez votre poids ");
scanf("%f", &p1.poids);
printf ("saisissez votre ville");
scanf("%s",p1.ville);
printf("C'est une personne qui mesure %f metre et pese %f Kg, son numero d'identification
est le %d et elle habite a %s. ", p1.taille, p1.poids, p1.numero ,p1.ville);
return 0;
}
Exercice 3 :

#include<stdio.h>

typedef struct date date;


struct date
{
int jour ;
char mois[10] ;
int annee ;
} ;
date p1;

int main ()
{
printf ("saisissez votre date de naissance. Exemple 15 mars 1998");
printf ("jour");
scanf("%d",&p1.jour);
printf ("mois");
scanf("%s",p1.mois);
printf ("annee");
scanf("%d", &p1.annee);
printf("votre date de naissance est %d / %s /%d",p1.jour, p1.mois, p1.annee);
return 0; }

Exercice 4 :

1-
#include<stdio.h>

struct personne
{
int cin ;
char nom[10] ;
char prenom[10] ;

}p1,p2 ;
int main ()
{
printf ("saisissez votre CIN");
scanf("%d",&p1.cin);
//deuxième personne
printf ("saisissez votre CIN");
scanf("%d",&p2.cin);
if (p1.cin == p2.cin) //ou bien if (strcmp(p1.cin,P2.cin)==0)
printf("les deux personnes sont identiques");
else {
printf("les deux personnes sont differentes");}

2-a-

#include<stdio.h>

struct personne
{
int cin ;
char nom[10] ;
char prenom[10] ;

}p1,p2 ;

int main ()
{int i;
printf ("saisissez votre CIN");
scanf("%d",&p1.cin);
printf ("saisissez votre nom");
scanf("%s",p1.nom);
printf ("saisissez votre prenom");
scanf("%s", p1.prenom);
//deuxième personne
printf ("saisissez votre CIN");
scanf("%d",&p2.cin);
printf ("saisissez votre nom");
scanf("%s",p2.nom);
printf ("saisissez votre prenom");
scanf("%s", p2.prenom);
if (p1.cin==p2.cin )
for(i=0;i<=9;i++)
{
p1.nom[i]=p2.nom[i];
p1.prenom[i]=p2.prenom[i];
} // ou bien strcpy( p2.nom, p1.nom ); strcpy( p2.prenom, p1.prenom );
printf("les deux personnes sont %d,%s,%s et
%d,%s,%s",p1.cin,p1.nom,p1.prenom,p2.cin,p2.nom,p2.prenom);
return 0; }

2-b-

#include<stdio.h>

struct personne
{
int cin ;
char nom[10] ;
char prenom[10] ;

}p1,p2 ;

int main ()
{
printf ("saisissez votre CIN");
scanf("%d",&p1.cin);
printf ("saisissez votre nom");
scanf("%s",p1.nom);
printf ("saisissez votre prenom");
scanf("%s", p1.prenom);
//deuxième personne
printf ("saisissez votre CIN");
scanf("%d",&p2.cin);
printf ("saisissez votre nom");
scanf("%s",p2.nom);
printf ("saisissez votre prenom");
scanf("%s", p2.prenom);
if (p1.cin==p2.cin )
printf("La personne est déjà enregistrée");
else
printf("les deux personnes sont sauvegardees");
return 0; }

2-c-

#include<stdio.h>
struct personne
{
int cin ;
char nom[10] ;
char prenom[10] ;
int age ;

}p1,p2 ;

int main ()
{int a;
printf ("saisissez votre CIN");
scanf("%d",&p1.cin);
printf ("saisissez votre nom");
scanf("%s",p1.nom);
printf ("saisissez votre prenom");
scanf("%s", p1.prenom);
printf ("saisissez votre age");
scanf("%d",&p1.age);

if (p1.age<18 )
{a=18-p1.age;
printf("vous ne pourrez vous enregistrer que dans %d ans",a);}
else
printf("personne enregistree");
return 0; }

Exercice 5:

a-
struct client {
char nom[10];
char prenom[10];
float compte;
int num_compte;
};

int main() {
struct client liste_clients[3];}

b-

int main() {
int i, n_compte, choix;
float montant;
struct client liste_clients[3];
printf("saisissez les enregistrements de 3 clients\n");
for(i=0;i<3;i++)
{printf("numero du compte \t");
scanf ("%d",&liste_clients[i].num_compte);
printf("Nom du client \t");
scanf ("%s",&liste_clients[i].nom);
printf("Prenom du client \t");
scanf ("%s",&liste_clients[i].prenom);
printf("saisissez le solde du compte \t");
scanf ("%f",&liste_clients[i].compte);}
printf("Pour toute operation saisissez un numero de compte");
scanf("%d",&n_compte);
do {
printf("\n Quelle est loperation que vous souhaitez faire? \t Tapez 1 pour retirer de l'argent'
\t Tapez 2 pour consulter le solde\n ");
scanf ("%d",&choix);
switch (choix){case 1:
printf("Quel est le montant souhaite?");
scanf("%f", &montant);
for(i=0;i<3;i++){
if (liste_clients[i].compte<montant)
printf("solde insuffisant");
else if (liste_clients[i].num_compte==n_compte)
{
liste_clients[i].compte-=montant;
printf("les nouvelles informations sont :Numero du compte: %d\t Nom du client: %s\t
Prenom du client: %s\t compte du client: %f\n", liste_clients[i].num_compte,
liste_clients[i].nom, liste_clients[i].prenom, liste_clients[i].compte);}
}
break;
case 2:
for(i=0;i<3;i++){if (liste_clients[i].num_compte==n_compte)
printf ("Votre solde est de: %f",liste_clients[i].compte );}
break;
default:
printf("Veuillez saisir 1 ou 2");
}}
while (choix != 0);
return 0; }

Exercice 6:
#include <stdio.h>
struct client {
char nom[10];
char prenom[10];
int age;
float compte;
int num_compte;
};
int main() {
int i, age;
struct client liste_clients[3];
printf("saisissez les enregistrements de 3 clients\n");
for(i=0;i<3;i++)
{printf("numero du compte \t");
scanf ("%d",&liste_clients[i].num_compte);
printf("Nom du client \t");
scanf ("%s",&liste_clients[i].nom);
printf("Prenom du client \t");
scanf ("%s",&liste_clients[i].prenom);
printf("Age du client \t");
scanf ("%d",&liste_clients[i].age);
printf("saisissez le montant du compte \t");
scanf ("%f",&liste_clients[i].compte);}
printf("Quel est l'age maximum");
scanf ("%d",&age);
printf("Les donnees des clients:\n");
for(i=0;i<3;i++){if (liste_clients[i].age<age)
printf("Numero du compte: %d\t Nom du client: %s\t Prenom du client: %s\t compte
du client: %f\n", liste_clients[i].num_compte, liste_clients[i].nom, liste_clients[i].prenom,
liste_clients[i].compte);
}

return 0;
}

Vous aimerez peut-être aussi