Les types structurés Introduction à la programmation et algorithmique BTS IRIS 1ère année.
-
Upload
gaston-josse -
Category
Documents
-
view
111 -
download
1
Transcript of Les types structurés Introduction à la programmation et algorithmique BTS IRIS 1ère année.
![Page 1: Les types structurés Introduction à la programmation et algorithmique BTS IRIS 1ère année.](https://reader035.fdocument.pub/reader035/viewer/2022062417/551d9d7f497959293b8b85e4/html5/thumbnails/1.jpg)
Les types structurés
Introduction à la programmation et algorithmique
BTS IRIS 1ère
année
![Page 2: Les types structurés Introduction à la programmation et algorithmique BTS IRIS 1ère année.](https://reader035.fdocument.pub/reader035/viewer/2022062417/551d9d7f497959293b8b85e4/html5/thumbnails/2.jpg)
Structure Tableaux : structures de données dont
tous les éléments sont de même typeTypes structurés : structures de données
dont les éléments peuvent être de type différent
Les éléments qui composent un type structuré sont appelés champs
BTS IRIS 1ère année Le langage C++ 2
![Page 3: Les types structurés Introduction à la programmation et algorithmique BTS IRIS 1ère année.](https://reader035.fdocument.pub/reader035/viewer/2022062417/551d9d7f497959293b8b85e4/html5/thumbnails/3.jpg)
Types structurésPermet d’englober plusieurs éléments :
Type personne avec nom, prénom et âgeType classe avec un tableau de noms de matières, un tableau de notes et un tableau de personnes
…
Utilisation comme un type classique :variables de type structurétableaux d'éléments de type structurédéclaration avant sa 1ère utilisation
BTS IRIS 1ère année Le langage C++ 3
![Page 4: Les types structurés Introduction à la programmation et algorithmique BTS IRIS 1ère année.](https://reader035.fdocument.pub/reader035/viewer/2022062417/551d9d7f497959293b8b85e4/html5/thumbnails/4.jpg)
Définition du typestruct nom_structure
{
type1 nom_champ1;
type2 nom_champ2;
};
BTS IRIS 1ère année Le langage C++ 4
![Page 5: Les types structurés Introduction à la programmation et algorithmique BTS IRIS 1ère année.](https://reader035.fdocument.pub/reader035/viewer/2022062417/551d9d7f497959293b8b85e4/html5/thumbnails/5.jpg)
Exemple struct Personne
{
string nom;
string prenom;
int age;
};
BTS IRIS 1ère année Le langage C++ 5
![Page 6: Les types structurés Introduction à la programmation et algorithmique BTS IRIS 1ère année.](https://reader035.fdocument.pub/reader035/viewer/2022062417/551d9d7f497959293b8b85e4/html5/thumbnails/6.jpg)
Déclaration de variableDéclaration identique à celle d’un type
primitif
Syntaxe :nom_Structure id_Var;
Exemple :
Personne eleve;
BTS IRIS 1ère année Le langage C++ 6
![Page 7: Les types structurés Introduction à la programmation et algorithmique BTS IRIS 1ère année.](https://reader035.fdocument.pub/reader035/viewer/2022062417/551d9d7f497959293b8b85e4/html5/thumbnails/7.jpg)
Représentation
BTS IRIS 1ère année Le langage C++ 7
moi
age sexenom
string int char
![Page 8: Les types structurés Introduction à la programmation et algorithmique BTS IRIS 1ère année.](https://reader035.fdocument.pub/reader035/viewer/2022062417/551d9d7f497959293b8b85e4/html5/thumbnails/8.jpg)
Manipulation Manipulation au travers des champssauf pour affectation et paramètres,
il n'est pas possible de manipuler un enregistrement globalement (Comme pour les tableaux)
Exemple : pour afficher une variable de type structuré il faut afficher ses champs un par un
BTS IRIS 1ère année Le langage C++ 8
![Page 9: Les types structurés Introduction à la programmation et algorithmique BTS IRIS 1ère année.](https://reader035.fdocument.pub/reader035/viewer/2022062417/551d9d7f497959293b8b85e4/html5/thumbnails/9.jpg)
Accès au champsles champs d'une structure sont accessibles
à travers leur nom et l'opérateur '. '
Syntaxe :id_var.nom_champ;
représente la valeur mémorisée dans le champ nom_champ de la variable id_var de type structuré
BTS IRIS 1ère année Le langage C++ 9
![Page 10: Les types structurés Introduction à la programmation et algorithmique BTS IRIS 1ère année.](https://reader035.fdocument.pub/reader035/viewer/2022062417/551d9d7f497959293b8b85e4/html5/thumbnails/10.jpg)
Exemple struct Personne{string nom;
string prenom;int age;
};…Personne pers1, pers2;…cout << pers1.nom;cout << pers1.prenom;cout << pers1.age; …cout << pers2.nom;…
BTS IRIS 1ère année Le langage C++ 10
![Page 11: Les types structurés Introduction à la programmation et algorithmique BTS IRIS 1ère année.](https://reader035.fdocument.pub/reader035/viewer/2022062417/551d9d7f497959293b8b85e4/html5/thumbnails/11.jpg)
Champs le nom d'un champ est toujours précédé
du nom de la variable à laquelle il appartient
On ne peut pas trouver un nom de champ tout seul
variables comme les autres à qui on peut faire subir les mêmes opérations :AffectationSaisieAffichage…
BTS IRIS 1ère année Le langage C++ 11
![Page 12: Les types structurés Introduction à la programmation et algorithmique BTS IRIS 1ère année.](https://reader035.fdocument.pub/reader035/viewer/2022062417/551d9d7f497959293b8b85e4/html5/thumbnails/12.jpg)
#include<iostream>#include<string>using namespace std;
int main (void){
struct Personne{
string nom;int age;
};
Personne pers1, pers2;cout << "Entrez le nom puis l'age de la personne 1" << endl;cin >> pers1.nom >> pers1.age;cout << "Entrez le nom puis l'age de la personne 2" << endl;cin >> pers2.nom >> pers2.age;cout << "La différence d'âge entre " << pers1.nom << " et " << pers2.nom << " est de ";if(pers1.age > pers2.age)
cout << pers1.age - pers2.age << " ans ";else
cout << pers2.age - pers1.age << " ans ";return 0;
}
Exemple complet : différence d’âge
BTS IRIS 1ère année Le langage C++ 12
Définition localeles variables de type Personne ne peuvent être déclarées que dans la fonction main
![Page 13: Les types structurés Introduction à la programmation et algorithmique BTS IRIS 1ère année.](https://reader035.fdocument.pub/reader035/viewer/2022062417/551d9d7f497959293b8b85e4/html5/thumbnails/13.jpg)
#include<iostream>#include<string>using namespace std; struct Personne{
string nom;int age;char sexe;
};void affichePersonne(Personne p){
cout << "nom : " << p.nom << endl;cout << "age : " << p.age << endl;cout << "sexe : " << p.sexe << endl;
}int main (void) {
Personne pers;cout << "veuillez saisir les nom, age et sexe :";cin >> pers.nom;cin >> pers.age;cin >> pers.sexe;pers.age=pers.age+1;affichePersonne(pers);return 0;
}
Paramètre de fonction
BTS IRIS 1ère année Le langage C++ 13
Définition globaleles variables de type Personne peuvent être déclarées n’importe où dans le programme
![Page 14: Les types structurés Introduction à la programmation et algorithmique BTS IRIS 1ère année.](https://reader035.fdocument.pub/reader035/viewer/2022062417/551d9d7f497959293b8b85e4/html5/thumbnails/14.jpg)
#include<iostream>#include<string>using namespace std; struct Personne
{string nom;int age;char sexe;};
Personne saisirPersonne(void){
Personne p;cout << "veuillez saisir les nom, age et sexe :";cin >> p.nom;cin >> p.age;cin >> p.sexe;return p;
}int main (void){
Personne pers;pers=saisirPersonne();vieillir(pers);affichePersonne(pers);return 0;
}
Retour de fonction
BTS IRIS 1ère année Le langage C++ 14
retour de fonction de type structuré
retourner la valeur résultat
récupérer le résultat
![Page 15: Les types structurés Introduction à la programmation et algorithmique BTS IRIS 1ère année.](https://reader035.fdocument.pub/reader035/viewer/2022062417/551d9d7f497959293b8b85e4/html5/thumbnails/15.jpg)
Imbrication de typeUn champs d’un type structuré peut être une
variable de type structuréExemple :
Une date est composée de trois variables (jour, mois, année) indissociables => structure
On remplace l'âge de la personne par sa date de naissance
BTS IRIS 1ère année Le langage C++ 15
![Page 16: Les types structurés Introduction à la programmation et algorithmique BTS IRIS 1ère année.](https://reader035.fdocument.pub/reader035/viewer/2022062417/551d9d7f497959293b8b85e4/html5/thumbnails/16.jpg)
Exemple struct DateNaissance{int jour;int mois;int annee;
};
struct Personne{string nom;DateNaissance dateN;char sexe;
};
BTS IRIS 1ère année Le langage C++ 16
![Page 17: Les types structurés Introduction à la programmation et algorithmique BTS IRIS 1ère année.](https://reader035.fdocument.pub/reader035/viewer/2022062417/551d9d7f497959293b8b85e4/html5/thumbnails/17.jpg)
Manipulation Personne saisirPersonne(){Personne p;cout << "veuillez saisir les nom, date de naissance, et sexe :";cin >> p.nom;cin >> p.dateN.jour;cin >> p.dateN.mois;cin >> p.dateN.annee; cin >> p.sexe;return p;
}
BTS IRIS 1ère année Le langage C++ 17
![Page 18: Les types structurés Introduction à la programmation et algorithmique BTS IRIS 1ère année.](https://reader035.fdocument.pub/reader035/viewer/2022062417/551d9d7f497959293b8b85e4/html5/thumbnails/18.jpg)
Exemple completstruct Adresse{
int num;string rue;int cp;string ville;
}; struct Fournisseur{
string code_frs;string raison_sociale;Adresse ad_frs;int tel;
}; struct Produit{
string code;string lib;float paht;float pvht;float txtva;Fournisseur frs;
};
Produit p;//saisie des valeurcout<<"ville du fournisseur de "<<p.lib<<" :" << p.frs.ad_frs.ville…
BTS IRIS 1ère année Le langage C++ 18
![Page 19: Les types structurés Introduction à la programmation et algorithmique BTS IRIS 1ère année.](https://reader035.fdocument.pub/reader035/viewer/2022062417/551d9d7f497959293b8b85e4/html5/thumbnails/19.jpg)
Tableau d’enregistrementstruct Personne
{string nom;int age;char sexe;};
void affichePersonne(Personne);
Personne saisirPersonne();
int main (void){
const int nbPers=10;Personne pers;Personne groupe[nbPers];for(int i=0; i<nbPers; i++)
groupe[i]=saisirPersonne();for(int i=0; i<nbPers; i++)
affichePersonne(groupe[i]);return 0;
}
BTS IRIS 1ère année Le langage C++ 19
![Page 20: Les types structurés Introduction à la programmation et algorithmique BTS IRIS 1ère année.](https://reader035.fdocument.pub/reader035/viewer/2022062417/551d9d7f497959293b8b85e4/html5/thumbnails/20.jpg)
Représentation
BTS IRIS 1ère année Le langage C++ 20
nom sexe
0
1
2
3
4
5
6
…
Nom des champs
Indices du tableauEx : tab[0].nom
age