Présentation PowerPoint C++ : Transp 1er Bac · 2020. 4. 22. · M. BENJELLOUN: 2019-2020 ++ - 108...

20
++- 106 M. BENJELLOUN : 2019-2020 U MONS Les Structures

Transcript of Présentation PowerPoint C++ : Transp 1er Bac · 2020. 4. 22. · M. BENJELLOUN: 2019-2020 ++ - 108...

Page 1: Présentation PowerPoint C++ : Transp 1er Bac · 2020. 4. 22. · M. BENJELLOUN: 2019-2020 ++ - 108 UMONS Un pas plus loin : Etudiants Médecins Ingénieurs Articles

++- 106M. BENJELLOUN : 2019-2020 UMONS

Les Structures

Page 2: Présentation PowerPoint C++ : Transp 1er Bac · 2020. 4. 22. · M. BENJELLOUN: 2019-2020 ++ - 108 UMONS Un pas plus loin : Etudiants Médecins Ingénieurs Articles

++- 107M. BENJELLOUN : 2019-2020 UMONS

Rappel

Variables :

int i, j, k, L;

string Nom1, Nom2, Nom3;

float F1, f2;

char ch1, ch5;

tab[0] tab[1] tab[2] tab[3]

i j k L

Nom1 Nom2 Nom3string tab[dim2]

int tab[4]

Un tableau est une collection de variables de même type, appelées éléments

Page 3: Présentation PowerPoint C++ : Transp 1er Bac · 2020. 4. 22. · M. BENJELLOUN: 2019-2020 ++ - 108 UMONS Un pas plus loin : Etudiants Médecins Ingénieurs Articles

++- 108M. BENJELLOUN : 2019-2020 UMONS

Un pas plus loin :

Etudiants

Médecins

Ingénieurs

Articles

Page 4: Présentation PowerPoint C++ : Transp 1er Bac · 2020. 4. 22. · M. BENJELLOUN: 2019-2020 ++ - 108 UMONS Un pas plus loin : Etudiants Médecins Ingénieurs Articles

++- 109M. BENJELLOUN : 2019-2020 UMONS

Id1Nom1Prenom1Date_Nais1…

Id2Nom2Prenom2Date_Nais2…

Id3Nom3Prenom3Date_Nais3

Id4Nom4Prenom4Date_Nais4

struct Etudiant { // tous les étudiants

int Id;string Nom;string Prenom;…

};

Les Structures

struct Article {

string nom;

int prix;

};

Etudiants

Articles

Page 5: Présentation PowerPoint C++ : Transp 1er Bac · 2020. 4. 22. · M. BENJELLOUN: 2019-2020 ++ - 108 UMONS Un pas plus loin : Etudiants Médecins Ingénieurs Articles

++- 110M. BENJELLOUN : 2019-2020 UMONS

Etudiant

Id

Nom

Prénom

Date_Nais

Email

Adresse

Enseigner

Année

Enseignant

Code.Enseig

Nom

Prénom

Statut

Email

…Matière

Code.Mat

Intitulé

ECTS

Modélisation de la Gestion des Étudiants FPMs

Les Structures

Page 6: Présentation PowerPoint C++ : Transp 1er Bac · 2020. 4. 22. · M. BENJELLOUN: 2019-2020 ++ - 108 UMONS Un pas plus loin : Etudiants Médecins Ingénieurs Articles

++- 111M. BENJELLOUN : 2019-2020 UMONS

Les Structures

Structure = ensemble de variables définissant un nouveau type sousun seul nom.

Les structures sont définies en utilisant le mot-clé struct.

struct Date {int jour;int mois;int an;

};

1 struct nom_de_la_structure {2 type NomChamp1 ;3 type NomChamp2 ;4 type NomChamp3 ;5 ...

6 } ;Déclarer des instances

struct Date paques, semaine[7]; // avec struct

Date noël, vacances[15]; // Sans struct

Date nouvel_an = { 1, 1, 2020 }; // Initialisation

Page 7: Présentation PowerPoint C++ : Transp 1er Bac · 2020. 4. 22. · M. BENJELLOUN: 2019-2020 ++ - 108 UMONS Un pas plus loin : Etudiants Médecins Ingénieurs Articles

++- 112M. BENJELLOUN : 2019-2020 UMONS

struct Date {int jour;int mois;int an;

};

struct Etudiant {char nom[30];string prenom;char *adresse;int numero;float Cotes[3];struct Date D_Nais;

};

Etudiant J_D = {"Dupont", "Jpp","rue de Houdain, 9, 7000 Mons",102,10.5, 11, 14.5,{ 15, 10, 2000 }

};

Les membres sont accédés par le nom de l’instance, suivi de . , suivi du nom du membre

Les Structures

cout <<"nom = "<< J_D.nom;

cout <<"\n Cote 0 = "<< J_D.Cotes[0]<< endl;cout << " jour de naissance "<<J_D. D_Nais.jour;

Page 8: Présentation PowerPoint C++ : Transp 1er Bac · 2020. 4. 22. · M. BENJELLOUN: 2019-2020 ++ - 108 UMONS Un pas plus loin : Etudiants Médecins Ingénieurs Articles

++- 113M. BENJELLOUN : 2019-2020 UMONS

Student191000

Obé

Lix

191001

Astér

Ix

100009

Tint

In

101899

Tourne

Sol

struct Student {int Id;string Nom;string Prenom;

};

struct Student Tab[Nmax];Ou

Student Tab[Nmax];

Les Structures

Page 9: Présentation PowerPoint C++ : Transp 1er Bac · 2020. 4. 22. · M. BENJELLOUN: 2019-2020 ++ - 108 UMONS Un pas plus loin : Etudiants Médecins Ingénieurs Articles

++- 114M. BENJELLOUN : 2019-2020 UMONS

#include …

struct Article{ // globale

string nom;

int prix;

};

int main( ) {

Article T[5];

for (int i=0; i<5; i++)

{

cout << "Entrez le nom : ";

cin >> T[i].nom;

cout << "\nEntrez le prix ";

cin >> T[i].prix;

}

return 1;

}

nom1150

nom213

nom3439

nom499

Nom5

31

T[0] T[1] T[2] T[3] T[4]

Les Structures

Page 10: Présentation PowerPoint C++ : Transp 1er Bac · 2020. 4. 22. · M. BENJELLOUN: 2019-2020 ++ - 108 UMONS Un pas plus loin : Etudiants Médecins Ingénieurs Articles

++- 115M. BENJELLOUN : 2019-2020 UMONS

#include …

struct Article{ // globale

string nom;

int prix;

};

void Affiche(Article AR){

cout << "\n\t Nom = " << AR.nom << " Prix = " << AR.prix;

}

void main( ) {

Article A, T[5];

cout << "Entrez le nom : "; cin >> A.nom;

cout << " \nEntrez le prix "; cin >> A.prix;

Affiche(A);

for (int i=0; i<5; i++) {

cout << "Entrez le nom : "; cin >> T[i].nom;

cout << "\nEntrez le prix "; cin >> T[i].prix;

Affiche(T[i]);

}

}

Et si nom contenait un espace ?Exp. SAMSUNG Galaxy A51

Les Structures+ Fonction Affiche

// Afficher un Article

Page 11: Présentation PowerPoint C++ : Transp 1er Bac · 2020. 4. 22. · M. BENJELLOUN: 2019-2020 ++ - 108 UMONS Un pas plus loin : Etudiants Médecins Ingénieurs Articles

++- 116M. BENJELLOUN : 2019-2020 UMONS

#include …

struct Article{

string nom;

int prix;

};

void Affiche(Article AR) {

cout << "\n\t Nom = " << AR.nom

<< " Prix = " << AR.prix;

}

void Saisie(Article AR) {

cout << "Entrez le nom : "; cin >> AR.nom;

cout << "\nEntrez le prix :"; cin >> AR.prix;

}

void main( ) {

Article A;

Saisie(A);

Affiche(A);

}

? Une structure peut être passée, comme une autre variable, par valeur ou par adresse ?

???

Les Structures

Entrez le nom :

Entrez le prix :

+ Fonction Saisie

Smartphone

300

Affiche(A)

Page 12: Présentation PowerPoint C++ : Transp 1er Bac · 2020. 4. 22. · M. BENJELLOUN: 2019-2020 ++ - 108 UMONS Un pas plus loin : Etudiants Médecins Ingénieurs Articles

++- 117M. BENJELLOUN : 2019-2020 UMONS

Les Structures#include …

struct Article{

string nom;

int prix;

};

void Affiche(Article AR) {

cout << "\n\t Nom = " << AR.nom << " Prix = " << AR.prix;

}

void Saisie(Article AR){

cout << "Entrez le nom : ";

cin >> AR.nom;

cout << "\nEntrez le prix :";

cin >> AR.prix;

}

void main( ) {

Article A;

Saisie(A);

Affiche(A);

}

Entrez le nom : Smartphone

Entrez le prix : 300

Nom = ╠╠╠╠ Prix = -858993

void Saisie(Article &AR){

cout << "Entrez le nom : ";

cin >> AR.nom;

cout << "\nEntrez le prix :";

cin >> AR.prix;

}

void main( ) {

Article A;

Saisie(A);

Affiche(A);

}

Entrez le nom : Smartphone

Entrez le prix : 300

Nom = Smartphone Prix = 300

Article Saisie( ){

Article AR;

cout << "Entrez le nom : ";

cin >> AR.nom;

cout << "\nEntrez le prix :";

cin >> AR.prix;

return AR;

}

void main( ) {

Article A;

A= Saisie( );

Affiche(A);

}Entrez le nom : Smartphone

Entrez le prix : 300

Nom = Smartphone Prix = 300

Page 13: Présentation PowerPoint C++ : Transp 1er Bac · 2020. 4. 22. · M. BENJELLOUN: 2019-2020 ++ - 108 UMONS Un pas plus loin : Etudiants Médecins Ingénieurs Articles

++- 118M. BENJELLOUN : 2019-2020 UMONS

struct E {int x;int y ;

} ;

#include …

void fonct (E *a) {

*a.x=1 ; *a.y=2;

}

void main(void){

struct E V;

V.x=V.y=0;fonct (&V);

cout << V.x << V.y;}

1 2

#include …

void fonct (E a) {

a.x=1 ; a.y=2;

}

void main(void){

struct E V;

V.x=V.y=0;

fonct (V);

cout << V.x << V.y;

}

0 0

#include …

E fonct (E a) {

a.x=1 ; a.y=2;

return a;

}

void main(void) {

struct E V;

V.x=V.y=0;

V = fonct (V);

cout << V.x << V.y;}

1 2

#include …

void fonct (E &a) {

a.x=1 ; a.y=2;

}

void main(void){

struct E V;

V.x=V.y=0;fonct (V);

cout << V.x << V.y;}

1 2

Les Structures

Page 14: Présentation PowerPoint C++ : Transp 1er Bac · 2020. 4. 22. · M. BENJELLOUN: 2019-2020 ++ - 108 UMONS Un pas plus loin : Etudiants Médecins Ingénieurs Articles

++- 119M. BENJELLOUN : 2019-2020 UMONS

Passer des tableaux aux fonctions

#include …

void Modif(int a[]){

a[0] = 5;

a[1] = 6;

}

void main() {

int p[2] = { 1, 2 };

cout <<p[0] <<" ; "<<p[1];

Modif(p);

cout <<p[0] <<" ; "<<p[1];

}

1 ; 2

5 ; 6

#include …

void Modif(struct E a[]){

a[0].x = a[0].y = 5;

a[1].x = a[1].y = 6;

}

void main( ) {

struct E p[2];

p[0].x= p[0].y= p[1].x= p[1].y = 0 ;

Modif(p);

cout << p[0].x << " ; "<< p[0].y;

cout << p[1].x << " ; "<< p[1].y;

}

Les Structures

6 ; 6

5 ; 5

struct E {int x;int y ;

} ;

Page 15: Présentation PowerPoint C++ : Transp 1er Bac · 2020. 4. 22. · M. BENJELLOUN: 2019-2020 ++ - 108 UMONS Un pas plus loin : Etudiants Médecins Ingénieurs Articles

++- 120M. BENJELLOUN : 2019-2020 UMONS

Nom1

5

Nom4

9

Nom5

7

Article Tab[Cste];

void Saisie(Article Tab[], int N) {for(int i=0; i<N; i++) { // plusieurs articles

cout << "\n le nom de l article: "

cin >> Tab[i].nom;cout << "\n le prix de l article: ";

cin >> Tab[i].prix;}

}

Manipulation d’un tableau

Les Structures

+ Fonction Saisie

// plusieurs articles

struct Article {string nom;int prix;

};

Page 16: Présentation PowerPoint C++ : Transp 1er Bac · 2020. 4. 22. · M. BENJELLOUN: 2019-2020 ++ - 108 UMONS Un pas plus loin : Etudiants Médecins Ingénieurs Articles

++- 121M. BENJELLOUN : 2019-2020 UMONS

1- Saisie et affichage:Est constitué de deux fonctions :

Saisie (…...) ; // de type voidDans cette fonction, on demandera et on testera N <= Nmax pour initialiser

le tableau (par exemple 5) et on effectuera la saisie.Affichage (…….) ;

Manipulation d’un tableau

Les Structures

+ Fonction Saisie

void Saisie(Article Tab[], int &N) {

… N?? // tester

for(int i=0; i<N; i++) {cout << "\n le nom de l article: "cin >> Tab[i].nom;cout << "\n le prix de l article: ";cin >> Tab[i].prix;

}

}

struct Article {string nom;int prix;

};

Page 17: Présentation PowerPoint C++ : Transp 1er Bac · 2020. 4. 22. · M. BENJELLOUN: 2019-2020 ++ - 108 UMONS Un pas plus loin : Etudiants Médecins Ingénieurs Articles

++- 122M. BENJELLOUN : 2019-2020 UMONS

#include <iostream>#include <string>using namespace std;

struct Article {string nom;int prix;

};

void Affiche(Article AR[], int N) {for (int i = 0; i<N; i++) {

cout << "\n\t Nom = " << AR[i].nom << " Prix = " << AR[i].prix;}

}

void Saisie(Article AR[], int &N) {N = 3; // initialisation de Nfor (int i = 0; i<N; i++) {

cout << "Entrez le nom : "; cin >> AR[i].nom;cout << "Entrez le prix :"; cin >> AR[i].prix;

}}

void main(void) {Article T[5]; // Type Articleint N ; // Type intSaisie(T, N); // plusieurs articlesAffiche(T, N); // plusieurs articles

cout << endl;

system("pause");}

Les Structures

Et si nom contenait un espace ?Exp. SAMSUNG Galaxy A51

Entrez le nom : nom2

Entrez le prix : 22

Entrez le nom : nom1

Entrez le prix : 11

Entrez le nom : nom3

Entrez le prix : 33

Nom = nom2 Prix = 22

Nom = nom1 Prix = 11

Nom = nom3 Prix = 33

Page 18: Présentation PowerPoint C++ : Transp 1er Bac · 2020. 4. 22. · M. BENJELLOUN: 2019-2020 ++ - 108 UMONS Un pas plus loin : Etudiants Médecins Ingénieurs Articles

++- 123M. BENJELLOUN : 2019-2020 UMONS

Tri à bullesbubble sort

void TriaBulles (int x[], int N) {int i, perm=1, tmp;

while (perm==1) {perm =0;for (i=0;i<N-1;i++) {

if(x[i] > x[i+1]) {tmp = x[i];x[i] = x[i+1];x[i+1] = tmp;perm = 1;

}}

}}

// Boucles imbriquées

Les Structures

void TriaBulles (Article x[], int N) {int i, perm=1;Article tmp; while (perm==1) {

perm =0;for (i=0;i<N-1;i++) {

if(x[i].nom > x[i+1].nom) {tmp = x[i];x[i] = x[i+1];x[i+1] = tmp;perm = 1;

}}

}}

Page 19: Présentation PowerPoint C++ : Transp 1er Bac · 2020. 4. 22. · M. BENJELLOUN: 2019-2020 ++ - 108 UMONS Un pas plus loin : Etudiants Médecins Ingénieurs Articles

++- 124M. BENJELLOUN : 2019-2020 UMONS

nom1

prix1

nom2

prix2

nom3

prix3

nom4

prix4…

Tab[0] Tab[1] Tab[2] Tab[3]

struct Article Tmp;

Tmp = Tab[0];

Tab[0] = Tab[1];

Tab[1] = Tmp;

Tmp.nom = Tab[0].nom;

Tab[0].nom = Tab[1].nom;

Tab[1].nom = Tmp.nom;

Tmp.prix = Tab[0].prix;

Tab[0].prix = Tab[1].prix;

Tab[1].prix = Tmp.prix;

+

Les Structures

Page 20: Présentation PowerPoint C++ : Transp 1er Bac · 2020. 4. 22. · M. BENJELLOUN: 2019-2020 ++ - 108 UMONS Un pas plus loin : Etudiants Médecins Ingénieurs Articles

++- 125M. BENJELLOUN : 2019-2020 UMONS

#include <iostream>#include <string>using namespace std;

struct Article {string nom;int prix;

};

void Affiche(Article AR[], int N) {for (int i = 0; i<N; i++) {

cout << "\n\t Nom = " << AR[i].nom << " Prix = " << AR[i].prix;}

}

void Saisie(Article AR[], int &N) {N = 3; // initialisation de Nfor (int i = 0; i<N; i++) {

cout << "Entrez le nom : "; cin >> AR[i].nom;cout << "Entrez le prix :"; cin >> AR[i].prix;

}}

void TriaBulles (Article x[], int N) { … … }

void main(void) {Article T[5];int N ;Saisie(T, N); // plusieurs articlesTriaBulles(T, N);Affiche(T, N); // plusieurs articles

system("pause");}

Les Structures

Entrez le nom : nom2

Entrez le prix : 22

Entrez le nom : nom1

Entrez le prix : 11

Entrez le nom : nom3

Entrez le prix : 33

Nom = nom1 Prix = 11

Nom = nom2 Prix = 22

Nom = nom3 Prix = 33

nom1 < nom2 < nom3