TP1
This commit is contained in:
33
TP1/1.1.cpp
Normal file
33
TP1/1.1.cpp
Normal file
@@ -0,0 +1,33 @@
|
||||
#include <iostream>
|
||||
|
||||
using namespace std;
|
||||
|
||||
int main()
|
||||
{
|
||||
int nbEnfants = 2;
|
||||
|
||||
setlocale(LC_ALL, "");
|
||||
|
||||
if (nbEnfants == 0)
|
||||
{
|
||||
cout << "Eh bien alors, vous n'avez pas d'enfant ?";
|
||||
}
|
||||
else
|
||||
{
|
||||
if (nbEnfants == 1)
|
||||
{
|
||||
cout << "Alors c'est pour quand le deuxième ?";
|
||||
}
|
||||
else
|
||||
{
|
||||
if (nbEnfants == 2)
|
||||
{
|
||||
cout << "Quels beaux enfants vous avez là !";
|
||||
}
|
||||
else
|
||||
{
|
||||
cout << "Bon, il faut arrêter de faire des gosses maintenant";
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
110
TP1/1.2.cpp
Normal file
110
TP1/1.2.cpp
Normal file
@@ -0,0 +1,110 @@
|
||||
#include <iostream>
|
||||
|
||||
using namespace std;
|
||||
|
||||
const int Nmax = 10;
|
||||
|
||||
void print_tab(int tab[], int n);
|
||||
|
||||
void print_help(int tab[], int n);
|
||||
|
||||
void insert_begin(int v, int tab[], int &n);
|
||||
|
||||
void insert_end(int v, int tab[], int &n);
|
||||
|
||||
void pop(int i, int tab[], int &n);
|
||||
|
||||
int main()
|
||||
{
|
||||
int tab[Nmax] = {5, 3, 11, 9, 2};
|
||||
int n = 5;
|
||||
int v;
|
||||
|
||||
char c;
|
||||
|
||||
setlocale(LC_ALL, "");
|
||||
|
||||
while (true)
|
||||
{
|
||||
print_help(tab, n);
|
||||
cout << "Votre choix : ";
|
||||
cin >> c;
|
||||
|
||||
switch (c)
|
||||
{
|
||||
case 'a':
|
||||
if (n >= Nmax)
|
||||
break;
|
||||
cout << "Entrez la valeur à ajouter : ";
|
||||
cin >> v;
|
||||
insert_begin(v, tab, n);
|
||||
|
||||
break;
|
||||
case 'b':
|
||||
if (n >= Nmax)
|
||||
break;
|
||||
cout << "Entrez la valeur à ajouter : ";
|
||||
cin >> v;
|
||||
insert_end(v, tab, n);
|
||||
|
||||
break;
|
||||
|
||||
case 'c':
|
||||
if (n < 2)
|
||||
break;
|
||||
pop(1, tab, n);
|
||||
|
||||
break;
|
||||
|
||||
case 'd':
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void print_tab(int tab[], int n)
|
||||
{
|
||||
cout << "[";
|
||||
for (int i = 0; i < n - 1; i++)
|
||||
{
|
||||
cout << tab[i] << ", ";
|
||||
}
|
||||
if (n > 0)
|
||||
cout << tab[n - 1];
|
||||
cout << "]";
|
||||
}
|
||||
|
||||
void print_help(int tab[], int n)
|
||||
{
|
||||
cout << "Tableau ";
|
||||
print_tab(tab, n);
|
||||
cout << endl;
|
||||
cout << "a) Ajouter au début du tableau une valeur introduite par l'utilisateur\n";
|
||||
cout << "b) Ajouter à la fin du tableau une valeur introduite par l'utilisateur\n";
|
||||
cout << "c) Supprimer la valeur de l'élément à la deuxième position du tableau\n";
|
||||
cout << "d) Quitter\n";
|
||||
}
|
||||
|
||||
void insert_begin(int v, int tab[], int &n)
|
||||
{
|
||||
for (int i = n - 1; i >= 0; i--)
|
||||
{
|
||||
tab[i + 1] = tab[i];
|
||||
}
|
||||
tab[0] = v;
|
||||
n++;
|
||||
}
|
||||
|
||||
void insert_end(int v, int tab[], int &n)
|
||||
{
|
||||
tab[n++] = v;
|
||||
}
|
||||
|
||||
void pop(int i, int tab[], int &n)
|
||||
{
|
||||
for (; i < n - 1; i++)
|
||||
{
|
||||
tab[i] = tab[i + 1];
|
||||
}
|
||||
n--;
|
||||
}
|
||||
62
TP1/1.3.cpp
Normal file
62
TP1/1.3.cpp
Normal file
@@ -0,0 +1,62 @@
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <iomanip>
|
||||
|
||||
using namespace std;
|
||||
|
||||
int EntreeEtudiant(string &mat, int &Ea, int &Eb);
|
||||
|
||||
int main()
|
||||
{
|
||||
int n;
|
||||
|
||||
setlocale(LC_ALL, "");
|
||||
|
||||
do
|
||||
{
|
||||
cout << "Entrez le nombre d'étudiants : ";
|
||||
cin >> n;
|
||||
} while (n < 0 || n > 10);
|
||||
|
||||
// tableaux alloués dynamiquements aussi possible statiquement
|
||||
// const int Nmax = 10;
|
||||
// avec string TabMat[Nmax];
|
||||
string *TabMat = new string[n];
|
||||
int *TabEa = new int[n];
|
||||
int *TabEb = new int[n];
|
||||
int Moy;
|
||||
|
||||
for (int i = 0; i < n; i++)
|
||||
{
|
||||
cout << "--- Etudiant " << i << " ---\n";
|
||||
Moy = EntreeEtudiant(TabMat[i], TabEa[i], TabEb[i]);
|
||||
// cout << "Entrez le matricule de l'étudiant (" << (i + 1) << ") : ";
|
||||
// cin >> TabMat[i];
|
||||
// cout << "Entrez la note Ea (" << (i + 1) << ") : ";
|
||||
// cin >> TabEa[i];
|
||||
// cout << "Entrez la note Eb (" << (i + 1) << ") : ";
|
||||
// cin >> TabEb[i];
|
||||
|
||||
cout << "Somme de Ea et Eb : " << Moy << "\n";
|
||||
cout << "Moyenne de Ea et Eb : " << setprecision(2)
|
||||
<< (Moy / 2) << "\n";
|
||||
}
|
||||
|
||||
// inutile car fin du programme
|
||||
// et la mémoire sera libérée par l'OS
|
||||
delete TabMat;
|
||||
delete TabEa;
|
||||
delete TabEb;
|
||||
}
|
||||
|
||||
int EntreeEtudiant(string &mat, int &Ea, int &Eb)
|
||||
{
|
||||
cout << "Entrez le matricule de l'étudiant : ";
|
||||
cin >> mat;
|
||||
cout << "Entrez la note Ea : ";
|
||||
cin >> Ea;
|
||||
cout << "Entrez la note Eb : ";
|
||||
cin >> Eb;
|
||||
|
||||
return (Ea + Eb);
|
||||
}
|
||||
43
TP1/1.4.cpp
Normal file
43
TP1/1.4.cpp
Normal file
@@ -0,0 +1,43 @@
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <iomanip>
|
||||
|
||||
using namespace std;
|
||||
|
||||
void Saisie(const int dim, int &S, int A[]);
|
||||
void Affichage(int S, const int A[]);
|
||||
|
||||
void main()
|
||||
{
|
||||
const int dim = 7;
|
||||
int i, A[dim], S = 0;
|
||||
|
||||
Saisie(dim, S, A);
|
||||
Affichage(dim, A);
|
||||
|
||||
/*S = 0;
|
||||
for (i = 0; i < 7;i++) {
|
||||
S += A[i];
|
||||
}*/
|
||||
cout << "\nLa somme est = " << S << "\n";
|
||||
}
|
||||
|
||||
void Saisie(const int dim, int &S, int A[])
|
||||
{
|
||||
cout << "Saisie:\n";
|
||||
for (int i = 0; i < dim; i++)
|
||||
{
|
||||
cout << "Entrez A[" << i << "]= ";
|
||||
cin >> A[i];
|
||||
S += A[i];
|
||||
}
|
||||
}
|
||||
|
||||
void Affichage(const int dim, const int A[])
|
||||
{
|
||||
cout << "Affichage:\n";
|
||||
for (int i = 0; i < dim; i++)
|
||||
{
|
||||
cout << "A[" << i << "]=" << A[i] << "\n";
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user